2010年10月11日 星期一

關於FormsAuthentication實作範例

FormsAuthentication可以實作網站裡面的自動登入,而主要的流程是

判斷帳號是否正確=>產生Ticket=>加密=>放進cookie

而使用cookie的流程
判斷cookie有效性=>抓取cookie=>解密=>取出cookie資料

講那麼多,看範例先↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓



            //ValidateUser是判斷帳號及密碼的method在此不說明

if (ValidateUser(txtUserName.Value, txtUserPass.Value))

            {

                //要產生的cookie票

                FormsAuthenticationTicket tkt;

                string cookiestr;

                HttpCookie ck;

                //FormsAuthenticationTicket裡面需要放的東西有版本、使用者名稱、時間、有效時間、有效性、使用者資料

                tkt = new FormsAuthenticationTicket(1, txtUserName.Value,

DateTime.Now, DateTime.Now.AddMinutes(30), chkPersistCookie.Checked,
"your custom data");

                //加密

                cookiestr = FormsAuthentication.Encrypt(tkt);

                //建立cookie

                ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);

                //如有選擇自動登入的CheckBox

                if (chkPersistCookie.Checked)

                    //cookie的有效時間會等於FormsAuthenticationTicket裡面的有效時間

                    ck.Expires = tkt.Expiration;

                //cookie的路徑 = 表單驗證 Cookie 的路徑

                ck.Path = FormsAuthentication.FormsCookiePath; 

                Response.Cookies.Add(ck);

                string strRedirect; 

                strRedirect = Request["ReturnUrl"]; 

                if (strRedirect == null)

                    strRedirect = "default.aspx"; Response.Redirect(strRedirect, true);

            }

            else

                Response.Redirect("logon.aspx", true);

        }



接下來抓取cookie



 Welcome.InnerHtml = "Hello, " +

            Server.HtmlEncode(User.Identity.Name);

        //FormsIdentity會直接解密cookie

       FormsIdentity id = (FormsIdentity)User.Identity;

       FormsAuthenticationTicket ticket = id.Ticket;

 

       cookiePath.Text = ticket.CookiePath;

       expireDate.Text = ticket.Expiration.ToString();

       expired.Text = ticket.Expired.ToString();

       isPersistent.Text = ticket.IsPersistent.ToString();

       issueDate.Text = ticket.IssueDate.ToString();

       name.Text = ticket.Name;

       userData.Text = ticket.UserData;

       version.Text = ticket.Version.ToString();



至於資料取出來之後要做甚麼事情,就看各位怎麼做了


//登出 會直接刪掉cookie
FormsAuthentication.SignOut();
Response.Write("Logged out - cookie deleted.");



來談一下FormsAuthentication的加密吧

ASP.Net 的 forms authentication就跟一般其他的驗證登入程式一樣,要先輸入自己密碼,然後判
斷,處理,再受權身份,最後轉頁‧ 不過 ASP.Net 卻是使用 cookie去儲存使用者登入資料的‧ 也許您
會認為這很不安全,設計者反而會自動完全依賴 cookie (在這邊,cookie 是使用者登入網頁時所
需的"ticket")‧ 但在預設,ASP.Net forms authentication 卻會對這一個 cookie 資料做三
重 DES(Encryption) 或 DES 加密,而且也還會在建立cookie 時在緩衝區中串連驗證金鑰與 Cookie 資
料,並計算成MAC(Machine Authentication Check) 然後放到 cookie 裡去,這樣就可以隨時檢查資料
的內容是否有被更改‧
(轉至http://www.study-area.org/coobila/tutorial_461.html)

web.config裡面設置key

<machineKey validationKey="1E7A0332AD3930B95A82A1BFF82B041791366BD14DE8
390B0B7EF3E7B05"
decryptionKey="702BD19727054E63574" validation="SHA1"/>

我記得我有在一篇文章有看到
authentication的預設加密是抓取iis裡面的伺服器名稱,而每一
次產生的cookie都不相同,我在想應該是還有加入其他的值下去做加密的運算,
FormsAuthentication到這告一段落,如有新的資料我會在補上,有興趣的可以在此留言討論
,或是有打錯的地方,也請糾正。


沒有留言: