繁体   English   中英

使用MVC4进行身份验证后存储用户数据

[英]Storing user data after authentication with MVC4

我正在使用ASP.NET MVC4 / Razor创建简单的登录名。 我需要自定义逻辑,因此,到目前为止,这里是:

public class User
   {
      [Required]
      [Display(Name = "User name")]
      public string UserName { get; set; }

      [Required]
      [DataType(DataType.Password)]
      [Display(Name = "Password")]
      public string Password { get; set; }

      public int UserID { get; set; }
      public DateTime LastLogin { get; set; }

      public bool IsValid(string username, string password)
      {
         if(MyCustomLogic.isValidUser(username, password)){
            // Set other variables
            return true;
         } else {
            return false;
         }
      }
   }

在控制器中:

public ActionResult Login(Models.User user)
{
   if (ModelState.IsValid)
   {
      if (user.IsValid(user.UserName, user.Password))
      {
         FormsAuthentication.SetAuthCookie(user.UserName, true);
         // This line is in question
      }
   }
   return View(user);
}

我想存储Model.User以便可以在View中永久访问它。 将其存储在Session变量中似乎是显而易见的选择,但这将独立于Authentication Cookie过期。

我想您是在问,如何存储用户的后续请求? 约定是创建您自己的IPrincipal子类,您可以在其中存储所需的任何内容,并将HttpContext.Current.User设置为此实例。 这个问题有充分的例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM