.NET Forms身份验证
生活随笔
收集整理的這篇文章主要介紹了
.NET Forms身份验证
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
.NET表單身份驗(yàn)證
ASP.NET Forms 身份驗(yàn)證的簡(jiǎn)單實(shí)現(xiàn):1)在Web.config文件中配置應(yīng)用程序使用 Forms 身份驗(yàn)證;2)創(chuàng)建登陸頁(yè)面,將用戶身份驗(yàn)證票證添加到Cookie集合。?????? 1.配置文件中設(shè)置為Forms驗(yàn)證
?? <authentication?mode="Forms">
??????<forms?loginUrl="NetFromwork/FormLogin.aspx"?defaultUrl="NetFromwork/FormLogin.aspx"?name=".ASPNETFORMSTEST"?protection="All"?timeout="20"?></forms>
???</authentication>
???<authorization>
??????<deny?users="?"/>
???</authorization>
?????? 2.驗(yàn)證用戶合法后,將身份驗(yàn)證票證寫入Cookie集合
????string?userName?=?"Jimmy";
????//Create?ticket
????FormsAuthenticationTicket?ticket?=?new?FormsAuthenticationTicket(1,?userName,?DateTime.Now,?DateTime.Now.AddMinutes(1),?false,?"");
????//Encrypt?the?ticket.
????String?encTicket?=?FormsAuthentication.Encrypt(ticket);
????//Create?the?cookie.
????Response.Cookies.Add(new?HttpCookie(FormsAuthentication.FormsCookieName,?encTicket));
????//Redirect?back?to?default?or?original?URL.
????FormsAuthentication.RedirectFromLoginPage(userName,?true);
?????? 上述代碼已經(jīng)實(shí)現(xiàn)了簡(jiǎn)單的Forms身份驗(yàn)證功能。接下來(lái)我們看看票據(jù)信息構(gòu)造函數(shù)FormsAuthenticationTicket()和用戶重定向函數(shù)RedirectFromLoginPage()的原型:
函數(shù)FormsAuthenticationTicket()原型
????????//
????????//?摘要:
????????//?????使用?cookie?名、版本、目錄路徑、發(fā)布日期、過(guò)期日期、持久性以及用戶定義的數(shù)據(jù)初始化?System.Web.Security.FormsAuthenticationTicket
????????//?????類的新實(shí)例。
????????//
????????//?參數(shù):
????????//???version:
????????//?????票證的版本號(hào)。
????????//
????????//???name:
????????//?????與身份驗(yàn)證票關(guān)聯(lián)的用戶名。
????????//
????????//???userData:
????????//?????存儲(chǔ)在票證中的用戶特定的數(shù)據(jù)。
????????//
????????//???isPersistent:
????????//?????如果票證將存儲(chǔ)在持久性?Cookie(跨瀏覽器會(huì)話保存),則為?true;否則為?false。如果該票證存儲(chǔ)在?URL?中,將忽略此值。
????????//
????????//???issueDate:
????????//?????票證發(fā)出時(shí)的本地日期和時(shí)間。
????????//
????????//???cookiePath:
????????//?????票證存儲(chǔ)在?Cookie?中時(shí)的路徑。
????????//
????????//???expiration:
????????//?????票證過(guò)期時(shí)的本地日期和時(shí)間。
????????public?FormsAuthenticationTicket(int?version,?string?name,?DateTime?issueDate,?DateTime?expiration,?bool?isPersistent,?string?userData,?string?cookiePath);
函數(shù)RedirectFromLoginPage()原型
?//
????????//?摘要:
????????//?????將經(jīng)過(guò)身份驗(yàn)證的用戶重定向回最初請(qǐng)求的?URL?或默認(rèn)?URL。
????????//
????????//?參數(shù):
????????//???userName:
????????//?????經(jīng)過(guò)身份驗(yàn)證的用戶名。
????????//
????????//???createPersistentCookie:
????????//?????若要?jiǎng)?chuàng)建持久?Cookie(跨瀏覽器會(huì)話保存的?Cookie),則為?true;否則為?false。
????????public?static?void?RedirectFromLoginPage(string?userName,?bool?createPersistentCookie);
?????? 論壇常見(jiàn)的登陸有效期功能是怎么實(shí)現(xiàn)的呢?是通過(guò)設(shè)置哪些參數(shù)實(shí)現(xiàn)的呢?不禁有些迷茫,涉及到登陸有效期的參數(shù)有如下幾個(gè):函構(gòu)造數(shù)FormsAuthenticationTicket()中的isPersistent和expiration,函數(shù)RedirectFromLoginPage()中的createPersistentCookie,配置文件中還有timeout屬性,究竟要怎么設(shè)置才能實(shí)現(xiàn)“登陸一小時(shí),一天,一個(gè)月,永久,瀏覽器進(jìn)程”功能呢?
?????? 下面談?wù)勎覍?duì)這些參數(shù)的理解,通過(guò)查找網(wǎng)上資料,MSDN和程序測(cè)試而來(lái),有理解錯(cuò)誤的地方歡迎大家指正。
???????1.關(guān)于持久Cookie,并不是說(shuō)isPersistent=True或者createPersistentCookie=True時(shí),就能夠保持永久登陸狀態(tài),持久Cookie解釋成跨瀏覽器會(huì)話保存的Cookie更合適。當(dāng)設(shè)置為True時(shí),不同瀏覽器進(jìn)程之間可以共享該Cookie,也就是說(shuō)在一個(gè)IE中登陸后,在另一個(gè)IE中打開還保持登陸狀態(tài)。
?????? 2.關(guān)于函數(shù)RedirectFromLoginPage(),MSDN上解釋是這樣的“如果 CookiesSupported 屬性為 true,并且 ReturnUrl 變量位于當(dāng)前應(yīng)用程序中,或者 EnableCrossAppRedirects 屬性為 true,則 RedirectFromLoginPage 方法將發(fā)出身份驗(yàn)證票證并使用 SetAuthCookie 方法將其置于默認(rèn) Cookie 中。”
???????可見(jiàn),在執(zhí)行函數(shù)RedirectFromLoginPage()時(shí),會(huì)覆蓋掉FormsAuthenticationTicket實(shí)例的票據(jù)信息,所以第二部分代碼中,不該調(diào)用該函數(shù),而應(yīng)該用下面的代碼代替:
????string?userName?=?"Jimmy";
????//Create?ticket
????FormsAuthenticationTicket?ticket?=?new?FormsAuthenticationTicket(1,?userName,?DateTime.Now,?DateTime.Now.AddMinutes(1),?false,?"");
????//Encrypt?the?ticket.
????String?encTicket?=?FormsAuthentication.Encrypt(ticket);
????//Create?the?cookie.
????Response.Cookies.Add(new?HttpCookie(FormsAuthentication.FormsCookieName,?encTicket));
????//Redirect?back?to?default?or?original?URL.
????Response.Redirect(FormsAuthentication.GetRedirectUrl(userName,true?));? //代替RedirectFormLoginPage
?????? 3.關(guān)于登陸有效時(shí)間,涉及到登陸時(shí)間的只有構(gòu)造函數(shù)FormsAuthenticationTicket()的參數(shù)expiration票據(jù)過(guò)期時(shí)間和配置文件中的timeout屬性了,這兩個(gè)有什么關(guān)系呢?----票據(jù)的有效時(shí)間是由expiration屬性決定的,而timeout只有當(dāng)expiration屬性沒(méi)有顯示設(shè)置的時(shí)候才生效。 摘自:http://www.cnblogs.com/freshman0216/archive/2008/06/17/1224215.html 參考:http://www.cnblogs.com/liuqhui/archive/2008/07/20/1246987.html
轉(zhuǎn)載于:https://www.cnblogs.com/besuccess/articles/3487535.html
總結(jié)
以上是生活随笔為你收集整理的.NET Forms身份验证的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Xbox上惊现低劣《战神》山寨游戏:画面
- 下一篇: Visual Studio 常用快捷键