0

由于我们在 FormsAuthentication 中没有 UserId 所以我想通过它。这是我的代码

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");
            }

现在问题是user.Id始终为 0FormsAuthentication.SetAuthCookie()

我该如何设置?

4

1 回答 1

1
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");
            }
于 2014-11-27T10:59:11.953 回答