ASP.NET Forms身份認(rèn)證
asp.net程序開(kāi)發(fā),用戶根據(jù)角色訪問(wèn)對(duì)應(yīng)頁(yè)面以及功能。
項(xiàng)目結(jié)構(gòu)如下圖:
根目錄 Web.config 代碼:
<?xml version="1.0" encoding="utf-8"?> <!-- 有關(guān)如何配置 ASP.NET 應(yīng)用程序的詳細(xì)消息,請(qǐng)?jiān)L問(wèn) http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <authentication mode="Forms"> <forms loginUrl="login.aspx"></forms> </authentication> <!--<authorization> <allow users="*"></allow> </authorization>--> </system.web> </configuration>
admin文件夾中 Web.config 代碼:
<?xml version="1.0"?> <configuration> <system.web> <authorization> <allow roles="admin" /> <deny users="*"/> </authorization> </system.web> </configuration>
teacher文件夾中 Web.config 代碼:
<?xml version="1.0"?> <configuration> <system.web> <authorization> <allow roles="teacher" /> <deny users="*"/> </authorization> </system.web> </configuration>
student文件夾中 Web.config 代碼:
<?xml version="1.0"?> <configuration> <system.web> <authorization> <allow roles="student" /> <deny users="*"/> </authorization> </system.web> </configuration>
Login.aspx中登錄成功后設(shè)置Cookie,設(shè)置Cookie代碼:
protected void SetLoginCookie(string username, string roles) { System.Web.Security.FormsAuthentication.SetAuthCookie(username, false); System.Web.Security.FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddDays(1), false, roles, "/"); string hashTicket = FormsAuthentication.Encrypt(ticket); HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket); HttpContext.Current.Response.SetCookie(userCookie); }
Global.asax 中進(jìn)行身份驗(yàn)證:
protected void Application_AuthenticateRequest(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; HttpContext ctx = app.Context; //獲取本次Http請(qǐng)求的HttpContext對(duì)象 if (ctx.User != null) { if (ctx.Request.IsAuthenticated == true) //驗(yàn)證過(guò)的一般用戶才能進(jìn)行角色驗(yàn)證 { System.Web.Security.FormsIdentity fi = (System.Web.Security.FormsIdentity)ctx.User.Identity; System.Web.Security.FormsAuthenticationTicket ticket = fi.Ticket; //取得身份驗(yàn)證票 string userData = ticket.UserData;//從UserData中恢復(fù)role信息 string[] roles = userData.Split(','); //將角色數(shù)據(jù)轉(zhuǎn)成字符串?dāng)?shù)組,得到相關(guān)的角色信息 ctx.User = new System.Security.Principal.GenericPrincipal(fi, roles); //這樣當(dāng)前用戶就擁有角色信息了 } } }
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
Entity Framework使用DbModelBuilder API創(chuàng)建表結(jié)構(gòu)
這篇文章介紹了Entity Framework使用DbModelBuilder API創(chuàng)建表結(jié)構(gòu)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03asp.net中Null在從數(shù)據(jù)庫(kù)讀取的時(shí)候的一點(diǎn)點(diǎn)小技巧
我們先看下面的一段代碼,這段代碼其實(shí)很平常,也是我們平時(shí)做項(xiàng)目很常用的一段2012-04-04asp.net網(wǎng)站開(kāi)發(fā)包wq.dll打包下載
這個(gè)wq.dll主要是用來(lái)給Web群和C#聯(lián)盟群及GUI群的朋友使用的,其它群和使用控件開(kāi)發(fā)web的朋友可以直接無(wú)視,這個(gè)封裝好的包是一個(gè)基礎(chǔ)開(kāi)發(fā)包,可以輕松的幫你完成一些小型網(wǎng)站的開(kāi)發(fā),支持.Net Framework2.0(及以上平臺(tái))。2009-10-10.Net?Core?使用?TagProvider?與?Enricher?豐富日志的操作代碼
這篇文章主要介紹了.Net?Core?使用?TagProvider?與?Enricher?豐富日志的操作,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03asp.net網(wǎng)站的404錯(cuò)誤頁(yè)面的正確設(shè)置方法
asp.net網(wǎng)站的404錯(cuò)誤頁(yè)面的正確設(shè)置方法,需要的朋友可以參考下。2010-05-05ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)項(xiàng)目框架(二)
這篇文章主要介紹了ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)項(xiàng)目框架,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09asp.net中通過(guò)ALinq讓Mysql操作變得如此簡(jiǎn)單
當(dāng)大家已經(jīng)習(xí)慣了使用.net 去操作SQL Server,有多少人曾經(jīng)嘗試過(guò)使用.net 去操作Mysql數(shù)據(jù)庫(kù)!在.net 的光環(huán)下,Mysql是顯得如此微不足道!但是Mysql的開(kāi)源又是如此具有誘惑。2011-07-07.Net?Core應(yīng)用增強(qiáng)型跨平臺(tái)串口類庫(kù)CustomSerialPort()詳解
本文詳細(xì)講解了.Net?Core應(yīng)用增強(qiáng)型跨平臺(tái)串口類庫(kù)CustomSerialPort(),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01ASP.NET MVC擴(kuò)展HtmlHelper方法
這篇文章介紹了ASP.NET MVC擴(kuò)展HtmlHelper的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03