前言 最近需要開始撰寫 ASP.Net MVC,目前遇到Form驗證實例會有自動驗證登入,藉由這次機會來展現身手。
功能主要紀錄使用者資訊,他會有一組Cookies暫存,內部值會經過加密編碼、解密。 如果第一次使用這個功能,千萬記住不要嘗試把密碼填入當中。
使用方式從 Client 點入登入網頁,這時Controllers可以進行驗證判斷,若有取得ticket成功會轉到首頁或是會員頁面;若驗證失敗則重新登入。
Authorize 身分驗證 確保不讓外界隨意登入,需要再 Controllers上方輸入 Authorize,如果沒有登入成功會有訊息提示。
1 2 3 4 5 6 7 8  namespace  ExampleForm.Controllers  {     [Authorize() ]     public  class  MainController  : Controller      {         return  View();     } } 
 
WebConfig 設定 這邊需要加入錯誤導向頁面設定。
1 2 3 4 5 <system.web>     <authentication mode="Forms">         <forms name="Demo_Site" loginUrl="Login/Index" cookieless="UseCookies" timeout="2880"/>     </authentication> </system.web> 
 
假設Login進去的時候,我會用下方方式存取一遍。UserData 則為存取相關資訊,例如 : email、電話等,通常存放不太重要的資料。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 private  void  Set_FormAuthenticationUser (){          FormsAuthenticationTicket  ticket = new  FormsAuthenticationTicket(     version: 1 ,     name: pUsrCode.ToString(),      issueDate: DateTime.UtcNow,     expiration: DateTime.UtcNow.AddMinutes(30 ),     isPersistent: true ,     userData: "" ,      cookiePath: FormsAuthentication.FormsCookiePath);     var  encryptedTicket = FormsAuthentication.Encrypt(ticket);      var  cookie = new  HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);     Response.Cookies.Add(cookie);     Response.Cookies.Add(new  HttpCookie("usr_code" , pUsrCode));           } 
 
驗證 (Reload Page) 減少登入次數,大多都會有記憶天數(7天、14天),下方直接展示使用方式。 [Demo_Site] 是指Config設定,form標示的名稱。
這function使用概念,驗證是否有這角色,如果各位 FormsAuthenticationTicket 的 Name 用 UID 處理可以不用轉Json方式取得資料。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 private  bool  IsFormAuthrizeUser (){     bool  isFormAuthrizeUser = false ;          if  (Request.Cookies.AllKeys.Where(x => x == "Demo_Site" ).Any())     {                  var  decr = FormsAuthentication.Decrypt(Request.Cookies["Demo_Site" ].Value);                  string  json = decr.UserData;         if  (!string .IsNullOrEmpty(json))         {                          DataTable resultData = JsonConvert.DeserializeObject<DataTable>($"[{json} ]" );             Set_UserBasic();             isFormAuthrizeUser = true ;         }     }     return  isFormAuthrizeUser; } 
 
登出 (Log Out) 登出相對簡單,只需要使用 FormsAuthentication.SignOut() 。提醒 : 記得要回到Login頁面喔~!
1 2 3 4 public  ActionResult Index () {    FormsAuthentication.SignOut();     return  Redirect("/Login" ); } 
 
參考文件 
ASP.NET MVC FormsAuthenticationTicket 驗證  
UserData property of FormsAuthenticationTicket is empty despite being set