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

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

点击次数:25 次 发布日期:2008-11-21 22:06:06 作者:源代码网
源代码网推荐

源代码网整理以下以admin角色为例,只允许角色为admin的用户访问

源代码网整理以下1.设定Web.Config文件

源代码网整理以下

以下为引用的内容:

源代码网整理以下  <roleManager enabled="true"/>  

源代码网整理以下  <authorization>        

源代码网整理以下  <allow roles="admin"/>

源代码网整理以下  <deny users="*"/>  

源代码网整理以下  </authorization>

源代码网整理以下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角色

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华