在 MVC4 FormsAuthentication中传递带有用户名的 UserId



因为我们在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总是 0 FormsAuthentication.SetAuthCookie()

如何设置?

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

相关内容

  • 没有找到相关文章

最新更新