asp.net 基于forms验证的目录角色权限的实现

所属分类: 网络编程 / ASP.NET 阅读数: 1146
收藏 0 赞 0 分享
但是我在使用过程中,发现针对角色的控制并不是那么容易,通过在网上查找资料,终于解决这个问题。下面将主要的注意事项列出来。
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;

大致弄清这三点,就可以了。
代码打包
更多精彩内容其他人还在看

Asp.net中的mail的发送

Asp.net中的mail的发送
收藏 0 赞 0 分享

用ASP.Net实现文件的在线压缩和解压缩

用ASP.Net实现文件的在线压缩和解压缩
收藏 0 赞 0 分享

ASP.NET中文件上传下载方法集合

ASP.NET中文件上传下载方法集合
收藏 0 赞 0 分享

ASP.NET通过Remoting service上传文件

ASP.NET通过Remoting service上传文件
收藏 0 赞 0 分享

ASP.NET2.0服务器控件之Render方法

ASP.NET2.0服务器控件之Render方法
收藏 0 赞 0 分享

ASP.NET2.0 WebRource,开发微调按钮控件

ASP.NET2.0 WebRource,开发微调按钮控件
收藏 0 赞 0 分享

ASP.NET2.0新特性概述

ASP.NET2.0新特性概述
收藏 0 赞 0 分享

介绍几个ASP.NET中容易忽略但却很重要的方法函数

介绍几个ASP.NET中容易忽略但却很重要的方法函数
收藏 0 赞 0 分享

asp.net2.0如何加密数据库联接字符串

asp.net2.0如何加密数据库联接字符串
收藏 0 赞 0 分享

用.NET 2.0压缩/解压功能处理大型数据

用.NET 2.0压缩/解压功能处理大型数据
收藏 0 赞 0 分享
查看更多