国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net 基于forms驗(yàn)證的目錄角色權(quán)限的實(shí)現(xiàn)

2020-01-18 01:03:42
字體:
供稿:網(wǎng)友
但是我在使用過程中,發(fā)現(xiàn)針對角色的控制并不是那么容易,通過在網(wǎng)上查找資料,終于解決這個問題。下面將主要的注意事項(xiàng)列出來。
1、配置文件中,角色的allow項(xiàng)要放在deny項(xiàng)的前面,users要配置為*,而不是?

代碼
復(fù)制代碼 代碼如下:

<location path="Doctors">
<system.web>
<authorization>
<allow roles="doctors"/> //這個在前
<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),就可以了。
代碼打包
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 剑河县| 大同县| 腾冲县| 尚志市| 澄城县| 会同县| 西和县| 东阿县| 登封市| 青田县| 宜都市| 白玉县| 黔江区| 黄龙县| 海丰县| 龙江县| 安龙县| 昭平县| 平顺县| 来安县| 河西区| 青田县| 平武县| 定襄县| 蓬莱市| 麻栗坡县| 肇源县| 奉贤区| 清徐县| 辽中县| 抚顺县| 商南县| 武汉市| 邹平县| 涿鹿县| 尉氏县| 保定市| 高青县| 濮阳县| 安吉县| 舞阳县|