|
1、配置文件中,角色的allow項(xiàng)要放在deny項(xiàng)的前面,users要配置為*,而不是?
代碼
復(fù)制代碼 代碼如下:
<location path="Doctors">
<system.web>
<authorization>
<allow roles="doctors"/> //這個(gè)在前
<deny users="*"/>
</authorization>
</system.web>
</location>
2、將角色寫入票據(jù)
代碼
復(fù)制代碼 代碼如下:
string role="doctors";
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(30), false, role, "/");//建立身份驗(yàn)證票對象
string HashTicket = FormsAuthentication.Encrypt(Ticket);//加密序列化驗(yàn)證票為字符串
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);
//生成Cookie
Response.Cookies.Add(UserCookie);//輸出Cookie
Response.Redirect("");//重定向到用戶申請的初始頁面
3、身份票據(jù)并沒有直接提供對role的直接支持,需要在Application_AuthenticateRequest中對role進(jìn)行解析
代碼
復(fù)制代碼 代碼如下:
string[] roles = authTicket.UserData.Split(new char[] { '|' });
FormsIdentity id = new FormsIdentity(authTicket);
System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles);
Context.User = principal;
大致弄清這三點(diǎn),就可以了。
代碼打包
AspNet技術(shù):asp.net 基于forms驗(yàn)證的目錄角色權(quán)限的實(shí)現(xiàn),轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。