今天要來講的是ASP.NET的帳號登入登出的驗證功能,如何使用表單驗證來完成會員登入,因為怕之後有可能會忘記所以來紀錄一下這篇。若講解的有錯誤,麻煩務必糾正,感謝。
<authentication mode ="Forms" >
     <forms cookieless="UseCookies" timeout="1440" loginUrl="Login.aspx"></forms>
</authentication>
public class userinformation
    {
        public Int32 id { get; set; }//主Key
        public string name { get; set; }//暱稱
        public string username { get; set; }//使用者名稱
        public string mail { get; set; } //信箱 email
        public string permission { get; set; }//權限
        public string photo { get; set; } //照片
    }
void SetAuthenTicket(string userData, string userId)
        {
          //宣告一個驗證票
          FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,//版本
          userId,//使用者名稱
          DateTime.Now,//發行時間
          DateTime.Now.AddHours(3),//有效時間
          false,  //是否將 Cookie 設定成 Session Cookie,如果是則會在瀏覽器關閉後移除。
          userData) //使用者資訊(可以想成備註欄);
          
                //加密驗證票
                string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                
                //建立Cookie
                HttpCookie authenticationcookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                authenticationcookie.Expires=DateTime.Now.AddHours(3);
                
                //將Cookie寫入回應
                Response.Cookies.Add(authenticationcookie);
          }
//假如DataTable從資料庫有找到資料
 if (user.Rows.Count > 0)
    {
//創類別物件
userinformation Userinformation =new userinformation();
//替類別存入內容(下面是用DataTable從資料庫取得資料)
               Userinformation.username = user.Rows[0]["username"].ToString();
               Userinformation.name = user.Rows[0]["name"].ToString();
               Userinformation.mail = user.Rows[0]["mail"].ToString();
               Userinformation.permission= user.Rows[0]["permission"].ToString();
               Userinformation.photo = user.Rows[0]["photo"].ToString();
//將物件序列化成字串
               string userData = JsonConvert.SerializeObject(Userinformation);
               
//副程式SetAuthenTicket 創立一張驗證票跟存入Cookie
               SetAuthenTicket(userData, username.Value);//使用者資訊和使用者名稱
               
//跳轉至登入後的頁面
               Response.Redirect("RayWeb.aspx");
  }
  
//登入失敗
   else
           {
           
           //某個Label顯示登入失敗
               Label1.Text = "登入失敗";
           }               
               
//確認使用者是否有驗證票,現在是否登入,假如沒有就跳回Login頁面。
            
                if (!HttpContext.Current.User.Identity.IsAuthenticated)/
                {
                
                Response.Redirect("Login.aspx");
                }
                
 //要取得驗證票內所存的使用者的資料,先將UserData反序列化成物件才能控制
   
                //取得UserData
                
                string strUserData =
                ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
                
                //反序列化為物件,其名為Myperson
                
                userinformation  Myperson=
                JsonConvert.DeserializeObject<userinformation>(strUserData);
                
                //取得票卷上的username
                Label1.Text = HttpContext.Current.User.Identity.Name;
                
                //取得UserData內的使用者資訊
                Label2.Text =Myperson.username; 
                
1.IsAuthenticated: 指是否已驗證使用者。
2.FormsIdentity:表示使用表單驗證的已驗證使用者識別。
簡單來說,一開始先創一個類別用來儲存從資料庫取得的登入使用者資料,並將它轉為字串存進驗證票的備註欄XD
接下來登入後,要取得備註欄資料(使用者資訊),再將字串轉回物件,該物件的範本就用一開始創好專門儲存使用者資訊的類別來接取使用者資訊,然後就可以使用了。
創一個SignOut的Web表單
 //登出表單驗證票卷
 FormsAuthentication.SignOut();
 
 //跳轉回使用者登入頁面
 Response.Redirect("Login.aspx");
1.FormsAuthentication:管理表單驗證服務。
使用表單驗證,用Cookie儲存有使用者資訊且加密的入場(登入)驗證票,入園後(登入後)需要資訊時再從票中取出使用,非常方便。
若以上有任何問題,麻煩務必糾正指教及建議,感謝><
參考資料:
Web API 開發心得 (4) - 使用 FormsAuthentication 進行 API 授權驗證。
asp.net 2.0 下的表單驗證Cookieless屬性。
Forms 驗證與授權。