基于角色(Role-Based)的表单验证

 

要求:
using System.Web.Security
using System.Security.Principal

[Principal]:主要的(这里怎样翻译呢??)
==================================

目录

None.gif +admin1
None.gif -default.aspx
None.gif -web.config //web.config#1
None.gif+admin2
None.gif -default.aspx
None.gif -web.config//web.config#2
None.gif+bin
None.gif-web.config//web.config#root
None.gif-login.aspx

 

==========================
目的:
admin1文件夹:只有role是administrator可以访问.
admini2文件夹:只有role是controler可以访问.

帐号,密码,角色存储在特定数据库中.

本例目的(其他道理相同):
caca是administrator
wawa是controler
所以caca可以访问admin1,不能访问admin2;wawa反之.

==========================
配置:
(1)web.config#root

None.gif <? xml version="1.0" encoding="utf-8" ?>
None.gif
< configuration >
None.gif 
< system .web >
None.gif  
< authentication  mode ="Forms" >
None.gif   
< forms  name ="authenticationcookie"  
loginUrl
="login.aspx"  protection ="All"  path ="/"  timeout ="40" />
None.gif  
</ authentication >
None.gif 
</ system.web >
None.gif
</ configuration >
None.gif

(2)web.config#1

None.gif <? xml version="1.0" encoding="utf-8" ?>
None.gif
< configuration >
None.gif 
< system .web >
None.gif  
< authorization >
None.gif   
< allow  roles ="administrator" />
None.gif   
< deny  users ="*" />
None.gif  
</ authorization >
None.gif 
</ system.web >
None.gif
</ configuration >
None.gif

(3)web.config#2

None.gif <? xml version="1.0" encoding="utf-8" ?>
None.gif
< configuration >
None.gif 
< system .web >
None.gif  
< authorization >
None.gif   
< allow  roles ="controler" />
None.gif   
< deny  users ="*" />
None.gif  
</ authorization >
None.gif 
</ system.web >
None.gif
</ configuration >
None.gif

==========================
关键代码:
(1)login.aspx

None.gif < script language = c# runat = server >
None.gif
private   void  signin(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
string aRole="guest";
InBlock.gif 
if(tbName.Text=="caca")aRole="administrator";
InBlock.gif 
if(tbName.Text=="wawa")aRole="controler";
InBlock.gif
InBlock.gif 
InBlock.gif
//建立role-based认证票据(我认为本质是cookie)
InBlock.gif
 FormsAuthenticationTicket authTicket = new  FormsAuthenticationTicket(
InBlock.gif             
1// version(版本?)
InBlock.gif
             tbName.Text, // user name(可能是生成票据验证cookie的名称)
InBlock.gif
             DateTime.Now, // creation(票据产生时间)
InBlock.gif
             DateTime.Now.AddMinutes(40),// Expiration(票据cookie失效时间)
InBlock.gif
             false// Persistent(这个应该是票据的保留时间)
InBlock.gif
            aRole ); // User data(角色)
InBlock.gif
//修改票据cookie,使其加密(本质是写入一个与票据cookie同名的新cookie)
InBlock.gif
 string encryptedTicket = FormsAuthentication.Encrypt(authTicket); 
InBlock.gif HttpCookie authCookie 
= new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);
//在保存这个Cookie之前,需要设定它的有效时间
//authCookie.Expires=DateTime.Now.AddDays(3);
InBlock.gif Response.Cookies.Add(authCookie); 
InBlock.gif
//返回所请求的URL
InBlock.gif
 Response.Redirect( FormsAuthentication.GetRedirectUrl(tbName.Text, false ));
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
private   void  signout(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//注销票据
InBlock.gif
 FormsAuthentication.SignOut();
ExpandedBlockEnd.gif}

None.gif
</ script >
None.gif
None.gif

 

None.gif < html >
None.gif
< head >
None.gif
< title > LogIn </ title >
None.gif
</ head >
None.gif
< body >
None.gif
< form  runat =server>
None.gif
Name:<asp:textbox runat =server  id =tbName/>[caca/wawa]
None.gif
<asp:button runat =server  text =LogIn  onclick =signin/>
None.gif
<asp:button runat =server  text =SignOut  onclick =signout/>
None.gif
<hr >
None.gif
< asp:label  runat =server  id =lblMessage/>
None.gif
</form >
None.gif
</ body >
None.gif
</ html >
None.gif

(2)Global.asax

None.gif <%  @ import  namespace = System.Security.Principal  %>
None.gif
<%  @ import  namespace = System.Security  %>  
None.gif
< script language = c# runat = server >
None.gif
protected   void  Application_AuthenticateRequest(Object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif
InBlock.gif
// Extract the forms authentication cookie(还原加密的票据)
InBlock.gif
 string cookieName = FormsAuthentication.FormsCookieName;
InBlock.gif HttpCookie authCookie 
= Context.Request.Cookies[cookieName];
InBlock.gif 
if(null == authCookie)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif   
// There is no authentication cookie.
InBlock.gif
   return;
ExpandedSubBlockEnd.gif }
 
InBlock.gif FormsAuthenticationTicket authTicket 
= null;
InBlock.gif 
try
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif     authTicket 
= FormsAuthentication.Decrypt(authCookie.Value);
ExpandedSubBlockEnd.gif }

InBlock.gif 
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif     
// Log exception details (omitted for simplicity)
InBlock.gif
     return;
ExpandedSubBlockEnd.gif }

InBlock.gif 
if (null == authTicket)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif     
// Cookie failed to decrypt.
InBlock.gif
     return
ExpandedSubBlockEnd.gif }

InBlock.gif 
// When the ticket was created, the UserData property was assigned a
InBlock.gif 
// pipe delimited string of role names.(票据已经还原,提取票据的UserData即为验证用户的role)
ExpandedSubBlockStart.gifContractedSubBlock.gif
 string[] roles = authTicket.UserData.Split(new char[]dot.gif{'|'});
InBlock.gif
InBlock.gif 
// Create an Identity object
InBlock.gif
 FormsIdentity id = new FormsIdentity( authTicket ); 
InBlock.gif 
// This principal will flow throughout the request.
InBlock.gif
 GenericPrincipal principal = new GenericPrincipal(id, roles);
InBlock.gif 
// Attach the new principal object to the current HttpContext object
InBlock.gif
 Context.User = principal;
InBlock.gif

ExpandedBlockEnd.gif
}

None.gif
</ script >
None.gif
None.gif

===========================
参考:
(1)Building Secure Microsoft ASP.NET Applications:
Authentication, Authorization, and Secure Communication by Microsoft Corporation  
ISBN:0735618909
Microsoft Press
(2)MSDN
===========================
下载参考代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值