ASP.NET Forms身份認(rèn)證
asp.net程序開發(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)文章
ASP.NET JSON字符串與實(shí)體類的互轉(zhuǎn)換示例代碼
本篇文章主要是對(duì)ASP.NET JSON字符串與實(shí)體類的互轉(zhuǎn)換的示例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
使用 Visual Studio 的“代碼度量值”來(lái)改進(jìn)代碼質(zhì)量
代碼度量是一組軟件度量值,使開發(fā)人員可以更好地了解他們正在開發(fā)的代碼.這篇文章主要介紹了通過(guò) Visual Studio 的“代碼度量值”來(lái)改進(jìn)代碼質(zhì)量,需要的朋友可以參考下2017-11-11
ADO調(diào)用分頁(yè)查詢存儲(chǔ)過(guò)程的實(shí)例講解
下面小編就為大家分享一篇ADO調(diào)用分頁(yè)查詢存儲(chǔ)過(guò)程的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
asp.net中一個(gè)linq分頁(yè)實(shí)現(xiàn)代碼
asp.net中一個(gè)linq分頁(yè)實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-12-12
仿vs實(shí)現(xiàn)WPF好看的進(jìn)度條
由于WPF自帶的進(jìn)度條其實(shí)不怎么好看,而且沒(méi)啥視覺(jué)效果。下面給大家分享的是仿VS的進(jìn)度條效果的代碼,有需要的小伙伴可以參考下。2015-06-06

