基于角色的身份验证2

前边一个有把在 global 中通过用户名到数据库去验证,这个中是直接在用户登陆时把用户角色存在 cookie 中,在 global 中去进行验证,可能比前一个要效率高吧....

 
web.config
 
None.gif < authentication  mode ="Forms" >  
None.gif     
< forms  name =".test"  loginUrl ="bb.aspx"  timeout ="30"  path ="/" ></ forms >
None.gif
None.gif      
</ authentication >
None.gif    
< authorization >
None.gif      
None.gif            
None.gif            
< deny  users ="?" ></ deny >
None.gif    
</ authorization >

User.xml
 
None.gif <? xml version="1.0" encoding="utf-8"  ?>  
None.gif
< roles >
None.gif  
< user
None.gif    
name ="Bob"
None.gif    roles
="Sales"   />
None.gif  
< user
None.gif    
name ="Jane"
None.gif    roles
="Supervisor,Sales"   />
None.gif
</ roles >
None.gif

  bb.aspx
 
None.gif private   void  Button1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {   
InBlock.gif
InBlock.gif            
if (confirm(this.TextBox1 .Text ,this.TextBox2.Text))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                 
string Roles=this.get_Role (this.TextBox1.Text );
InBlock.gif                 
InBlock.gif
InBlock.gif                FormsAuthenticationTicket Ticket 
= new FormsAuthenticationTicket (1,this.TextBox1 .Text ,DateTime.Now, DateTime.Now.AddMinutes(30), false,Roles,"/") ; //建立身份验证票对象
InBlock.gif
               string HashTicket = FormsAuthentication.Encrypt (Ticket) ; //加密序列化票为字符串
InBlock.gif

InBlock.gif            HttpCookie UserCookie 
= new HttpCookie(FormsAuthentication.FormsCookieName,HashTicket) ;
InBlock.gif            
//生成Cookie
InBlock.gif
            Context.Response.Cookies.Add (UserCookie) ; //输出Cookie
InBlock.gif
            Context.Response.Redirect (Context.Request["ReturnUrl"]) ; // 重定户申请的初始页面
InBlock.gif
      
ExpandedSubBlockEnd.gif}

InBlock.gif                     
InBlock.gif            
ExpandedBlockEnd.gif        }
 
None.gif
None.gif        
public   bool  confirm( string  name, string  pass)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            DataSet ds;
InBlock.gif            ds
=new DataSet ();
InBlock.gif            ds.ReadXml (MapPath(
"User.xml"));
InBlock.gif            DataTable dt
=ds.Tables[0];
InBlock.gif              
InBlock.gif            DataRow[] dr
=dt.Select("name='"+name+"'");
InBlock.gif
InBlock.gif            
if (dr.Length >0)
InBlock.gif                
return true;
InBlock.gif                
else
InBlock.gif                
return false;
InBlock.gif              
InBlock.gif             
ExpandedBlockEnd.gif        }
 
None.gif
None.gif        
public    string   get_Role( string  name)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif             DataSet ds;
InBlock.gif             ds
=new DataSet ();
InBlock.gif             ds.ReadXml (MapPath(
"User.xml"));
InBlock.gif              DataTable dt
=ds.Tables[0];
InBlock.gif              
InBlock.gif                DataRow[] dr
=dt.Select("name='"+name+"'");
InBlock.gif              
InBlock.gif            
InBlock.gif             
return dr[0][1].ToString ();
InBlock.gif               
InBlock.gif              
InBlock.gif              
InBlock.gif             
ExpandedBlockEnd.gif        }


Global.asax

None.gif protected   void  Application_AuthenticateRequest(Object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif           System.Web .HttpApplication  app
=((HttpApplication)sender);
InBlock.gif           System.Web.HttpContext  ctx
=app.Context;
InBlock.gif            
if (ctx.Request .IsAuthenticated)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Web .Security.FormsIdentity id
=((FormsIdentity)ctx.User .Identity) ;
InBlock.gif                System.Web .Security.FormsAuthenticationTicket  ticket
=id.Ticket ;
InBlock.gif                 
string [] Role=ticket.UserData .Split(',');
InBlock.gif                 ctx.User 
=new System.Security.Principal.GenericPrincipal(id ,Role);
InBlock.gif
ExpandedSubBlockEnd.gif             }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif        }


default.aspx

 

None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
InBlock.gif            
if (User.IsInRole("Sales"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Response .Write ("Seles");
InBlock.gif                 
ExpandedSubBlockEnd.gif             }

InBlock.gif
InBlock.gif            
if (User.IsInRole("Supervisor"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Response .Write ("Supervisor");
InBlock.gif                 
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            
ExpandedBlockEnd.gif                }



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值