asp.net 基于forms验证的目录角色权限的实现
董佩永,网上购买家具,139高清电影网
但是我在使用过程中,发现针对角色的控制并不是那么容易,通过在网上查找资料,终于解决这个问题。下面将主要的注意事项列出来。
1、配置文件中,角色的allow项要放在deny项的前面,users要配置为*,而不是?
代码
<location path="doctors">
<system.web>
<authorization>
<allow roles="doctors"/> //这个在前
<deny users="*"/>
</authorization>
</system.web>
</location>
2、将角色写入票据
代码
string role="doctors";
formsauthenticationticket ticket = new formsauthenticationticket(1, username, datetime.now, datetime.now.addminutes(30), false, role, "/");//建立身份验证票对象
string hashticket = formsauthentication.encrypt(ticket);//加密序列化验证票为字符串
httpcookie usercookie = new httpcookie(formsauthentication.formscookiename, hashticket);
//生成cookie
response.cookies.add(usercookie);//输出cookie
response.redirect("");//重定向到用户申请的初始页面
3、身份票据并没有直接提供对role的直接支持,需要在application_authenticaterequest中对role进行解析
代码
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;
大致弄清这三点,就可以了。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
网友评论