在不启用角色管理的情况下,实现Forms身份验证

 

1.web.config配置

2.login.aspx中验证方法中使用FormsAuthentication

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        if (Login1.UserName == "admin" && Login1.Password == "123456")
        {
            FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);
        }
    }

 

//我的webform中使用login控件

3.启用角色管理,在global.asax的

void Application_AuthenticateRequest(object sender, EventArgs e)
    {

  if (User != null &&User.Identity.IsAuthenticated)
        {


            if (System.Web.Security.Roles.Enabled == False)
            {

                FormsIdentity id = User.Identity as FormsIdentity;
                string[] roles ={ "admin" };
                GenericPrincipal principal = new GenericPrincipal(id, roles);
                // 将新的主体对象附加到当前的 HttpContext 对象
                HttpContext.Current.User = principal;
            }
        }

}

 

注意:如果想从其它数据源获得roles,可在登陆验证时用FormsAuthenticationTicket自定义User.Identity,

 

当然也可直接从数据源获得.^^网上有介绍

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值