基于Forms-Roles认证方式目录权限配置的学习

一直以来网站管理后台的验证代码都是自己在写,这两天学习了一下ASP.NET的基于FORMS认证的方式,只要统一配置就可以,不用每个管理页面都加入验证代码,非常方便实用.
举个例子:
网站结构包括:
Admin(目录)
    +index.aspx
Default.aspx
Web.config
Global.asax
我们要多Web.config进行配置,实现管理员可以访问Admin目录,其他角色都不可以访问.

< authentication mode = " Forms " >
            
< forms loginUrl = " default.aspx "  protection = " All "  requireSSL = " false " ></ forms >
        
</ authentication >
        
< authorization >
            
< allow users = " * " />
        
</ authorization >

再对Admin目录设置访问权限.
     < location path = " admin " >
        
< system.web >
            
< authorization >
        
< allow roles = " managers " />
        
< deny users = " * " />
            
</ authorization >
        
</ system.web >
    
</ location >

下面就是登录用户的代码了,这里有两段代码,一个是管理员登录,一个是普通用户登录
     protected   void  Button1_Click( object  sender, EventArgs e)
    
{
        
string role = "managers";

        FormsAuthenticationTicket authTickets 
= new FormsAuthenticationTicket(1"Lordz", DateTime.Now, DateTime.Now.AddYears(1), false, role);

        
string encryptedTicket = FormsAuthentication.Encrypt(authTickets);

        HttpCookie authCookie 
= new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

        authCookie.Expires 
= authTickets.Expiration;

        HttpContext.Current.Response.Cookies.Add(authCookie);


        
string returnUrl = Request.QueryString["ReturnUrl"];
        
if (returnUrl == null) returnUrl = "admin/index.aspx";

        Response.Redirect(returnUrl);
    }

    
protected   void  Button2_Click( object  sender, EventArgs e)
    
{
        
string role = "User";

        FormsAuthenticationTicket authTickets 
= new FormsAuthenticationTicket(1"Lordz", DateTime.Now, DateTime.Now.AddYears(1), false, role);

        
string encryptedTicket = FormsAuthentication.Encrypt(authTickets);

        HttpCookie authCookie 
= new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

        authCookie.Expires 
= authTickets.Expiration;
        HttpContext.Current.Response.Cookies.Add(authCookie);

        
string returnUrl = Request.QueryString["ReturnUrl"];
        
if (returnUrl == null) returnUrl = "admin/index.aspx";

        Response.Redirect(returnUrl);
    }


其实只有定义role这个变量的时候是不一样,其他地方代码都相同
注意:我上面的代码是为了方便测试,没有写对用户的验证,这个要大家自己根据实际情况来写了.

还有一个重要的地方,需要在 Global.asax里添加如下代码
     protected   void  Application_AuthenticateRequest(Object sender, EventArgs e)
    
{

        
// Extract the forms authentication cookie
        string cookieName = FormsAuthentication.FormsCookieName;
        HttpCookie authCookie 
= Context.Request.Cookies[cookieName];
        
if (null == authCookie)
        
{
            
// There is no authentication cookie.
            return;
        }

        FormsAuthenticationTicket authTicket 
= null;
        
try
        
{
            authTicket 
= FormsAuthentication.Decrypt(authCookie.Value);
        }

        
catch (Exception ex)
        
{
            
// Log exception details (omitted for simplicity)
            return;
        }

        
if (null == authTicket)
        
{
            
// Cookie failed to decrypt.
            return;
        }

        
// When the ticket was created, the UserData property was assigned a
        
// pipe delimited string of role names.
        string[] roles = authTicket.UserData.Split(new char[] '|' });

        
// Create an Identity object
        FormsIdentity id = new FormsIdentity(authTicket);
        
// This principal will flow throughout the request.
        System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles);
        
// Attach the new principal object to the current HttpContext object
        Context.User = principal;



    }

大家可以设置断点来看看上面代码是在上面时候被执行,目的是在用户得到认证以后赋予其角色,具体的解释网上面有很多,大家可以搜索一下,我也不是很明白为什么要这段,知道的朋友可以说一下,呵呵

附上代码 /Files/lordz/WebSite1.rar

学习之用,高手勿笑.

转载于:https://www.cnblogs.com/lordz/archive/2007/10/14/924106.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值