当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > Asp.net中Forms验证的角色验证授权(二)

ASP.NET
Web服务器控件:LinkButton控件
Web服务器控件:ListBox控件
Web服务器控件:ListItem控件
Web服务器控件:Literal控件
Web服务器控件:Panel控件
Web服务器控件:PlaceHolder控件
Web服务器控件:RadioButton控件
Web服务器控件:RadioButtonList控件
Web服务器控件:BulletedList控件
Web服务器控件:Style控件
Web服务器控件:Table控件
Web服务器控件:TableCell控件
Web服务器控件:TableRow控件
Web服务器控件:TextBox控件
Web服务器控件:XML控件
Validation服务器控件:CompareValidator控件
Validation服务器控件:CustomValidator控件
Validation服务器控件:RangeValidator控件
Validation服务器控件:RegularExpressionValidator控件
Validation服务器控件:RequiredFieldValidator控件

ASP.NET 中的 Asp.net中Forms验证的角色验证授权(二)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 294 ::
收藏到网摘: n/a

以admin角色为例,只允许角色为admin的用户访问

1.设定Web.Config文件

2.在Global.asax文件的Application_Start事件处理方法中添加角色

以下为引用的内容:

  if(!Roles.RoleExists("admin"))             Roles.CreateRole("admin");

3.登录时对Admin 角色的用户添加如下代码:

以下为引用的内容:

  FormsAuthentication.SetAuthCookie (tb_username.Text, false);

  if(!Roles.IsUserInRole (tb_username.Text,"admin"))

  Roles.AddUserToRole (tb_username.Text, "admin");

  Response.Redirect (FormsAuthentication.GetRedirectUrl (tb_username.Text, false)); 

4.前提:1)有角色admin存在;2)当前用户属于admin角色