Forms验证中的roles[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

4,第四步,修改web.config
<configuration>
<system.web>
<authentication mode="Forms">
<forms name="MYWEBAPP.ASPXAUTH"
loginUrl="login.aspx"
protection="All"
path="/"/>
</authentication>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
<location path="admins">
<system.web>
<authorization>
<!-- Order and case are important below -->
<allow roles="Administrator"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="users">
<system.web>
<authorization>
<!-- Order and case are important below -->
<allow roles="User"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
</configuration>

5,测试,在应用程序下新建两个目录admins和users,分别在他们的目录下放个default.aspx,上面随便写些什么东西,把其中的一个default.aspx设置问起始页(在vs2003环境下),如果你输入名字pwq和密码是不能够进入admins目录下的,因为这个用户不属于Administrator角色!

 我们来看下Forms身份验证基本原理:
一 身份验证
要采用Forms身份验证,先要在应用程序根目录中的Web.config中做相应的设
置:
<authentication mode="Forms">
    <forms name=".ASPXAUTH" loginUrl="login.aspx" timeout="30"
path="/"/>
</authentication>
其中<authentication mode="Forms"> 表示本应用程序采用Forms验证方
式。
<forms>标签中的name表示指定要用于身份验证的Cookie。默认是.ASPXAUTH,其实你可以用任何名字,这也就是你在本地硬盘上看到的cookie里面的前面的几个字.
Forms的验证过程如下:1,生成身份验证票,2,加密身份验证票.3,写回客户端,4,浏览器重新定向.其实这一系列的动作如果我们不用roles的话都是通过FormsAuthentication.RedirectFromLoginPage方法来完成了这一系列的工作任务.但是既然我们要使用roles授权,我们就不能够使用这个方法,而要分开来,一步步完成.
首先是创建身份验证票,首先我们看看FormsAuthenticationTicket类的一个构造函数:
public FormsAuthenticationTicket(
int version, //设为1
string name, //用户标示
DateTime issueDate, //Cookie 的发出时间, 设置为 DateTime.Now
DateTime expiration, //过期时间
bool isPersistent, //是否持久性(根据需要设置,若是设置为持久性,在发出
cookie时,cookie的Expires设置一定要设置)
string userData, //这里用上面准备好的用逗号分割的role字符串
string cookiePath // 设为”/”,这要同发出cookie的路径一致,因为刷新cookie
要用这个路径
);
最后个参数可以省略
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket
(1,”kent”,DateTime.Now, DateTime.Now.AddMinutes(30), false,UserRoles)

 我们来看下Forms身份验证基本原理:
一 身份验证
要采用Forms身份验证,先要在应用程序根目录中的Web.config中做相应的设
置:
<authentication mode="Forms">
    <forms name=".ASPXAUTH" loginUrl="login.aspx" timeout="30"
path="/"/>
</authentication>
其中<authentication mode="Forms"> 表示本应用程序采用Forms验证方
式。
<forms>标签中的name表示指定要用于身份验证的Cookie。默认是.ASPXAUTH,其实你可以用任何名字,这也就是你在本地硬盘上看到的cookie里面的前面的几个字.
Forms的验证过程如下:1,生成身份验证票,2,加密身份验证票.3,写回客户端,4,浏览器重新定向.其实这一系列的动作如果我们不用roles的话都是通过FormsAuthentication.RedirectFromLoginPage方法来完成了这一系列的工作任务.但是既然我们要使用roles授权,我们就不能够使用这个方法,而要分开来,一步步完成.
首先是创建身份验证票,首先我们看看FormsAuthenticationTicket类的一个构造函数:
public FormsAuthenticationTicket(
int version, //设为1
string name, //用户标示
DateTime issueDate, //Cookie 的发出时间, 设置为 DateTime.Now
DateTime expiration, //过期时间
bool isPersistent, //是否持久性(根据需要设置,若是设置为持久性,在发出
cookie时,cookie的Expires设置一定要设置)
string userData, //这里用上面准备好的用逗号分割的role字符串
string cookiePath // 设为”/”,这要同发出cookie的路径一致,因为刷新cookie
要用这个路径
);
最后个参数可以省略
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket
(1,”kent”,DateTime.Now, DateTime.Now.AddMinutes(30), false,UserRoles)

然后加密:

string hashTicket = FormsAuthentication.Encrypt(ticket);
//设置验证票cookie,第一个参数为cookie的名字,第二个参数为cookie的值也就是加密后的票
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,hashTicket);
//设置cookie的有效期是一个礼拜
cookie.Expires = DateTime.Now.AddDays(7);
//把cookie加进Response对象发生到客户端
Response.Cookies.Add(cookie);
//得到请求的url
string requestUrl = FormsAuthentication.GetRedirectUrl(FormsAuthentication.FormsCookieName,false);
//不要使用FormsAuthentication.RedirectFromLoginPage方法,因为这个方法会重写cookie
//重新定向到请求的url
Response.Redirect(requestUrl);

以后的各个页面中通过HttpContext.Current.User.Identity.Name判断用户标识,
  HttpContext.Current.User.IsInRole("Admin")判断用户是否属于某一角色(或某一组)

本文关键:Forms验证中的roles
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top