簡體   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