Passing UserId with UserName in MVC4 FormsAuthentication

529 views Asked by At

As we do not have UserId in FormsAuthentication So i want to pass with it.Here is my code

public bool IsValid(string email, string password)
    {
        bool IsValid = false;
        var user = db.Users.FirstOrDefault(u => u.Email == email);

        if (user != null)
        {

            if (user.Password == EncryptPassword(password))
            {
                IsValid = true;

            }

        }
        return IsValid;
    }
      if (user.IsValid(user.Email, user.Password))
            {

                FormsAuthentication.SetAuthCookie(user.Email + "|" + user.Id, true);
                return RedirectToAction("Index", "TestValidUser");
            }

Now Issue is that user.Id is always 0 in FormsAuthentication.SetAuthCookie()

How I can set it?

1

There are 1 answers

4
sakir On BEST ANSWER
int userId=0;


public bool IsValid(string email, string password,out int userId)
    {

    if (user.Password == EncryptPassword(password))
            {
                IsValid = true;

               userId= user.Id
            }
}

if (user.IsValid(user.Email, user.Password,userId))
            {

                FormsAuthentication.SetAuthCookie(user.Email + "|" + userId, true);
                return RedirectToAction("Index", "TestValidUser");
            }