asp.net2.0角色管理,详细步骤

step 1:
C# code


   
     
< authentication mode = " forms " > < forms name = " .ASPXAUTH " loginUrl = " /login.aspx " timeout = " 30 " path = " / " > </ forms > </ authentication >


step 2:
需要角色控制的目录下新建web.config,如以下配置
C# code


   
     
< authorization > < allow users = " comma-separated list of users " roles = " comma-separated list of roles " verbs = " comma-separated list of verbs " /> < deny users = " comma-separated list of users " roles = " comma-separated list of roles " verbs = " comma-separated list of verbs " /> </ authorization >


step 3:
登录代码,获得船票

C# code


   
     
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket ( 1 ,user,DateTime.Now,


   
     
DateTime.Now.AddMinutes( 30 ), false ,userRoles, " / " ) ; // 建立身份验证票对象 string HashTicket = FormsAuthentication.Encrypt (Ticket) ; // 加密序列化验证票为字符串 HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ; // 生成Cookie Context.Response.Cookies.Add (UserCookie) ; // Cookie



step 4:(手工创建角色)
在global.asax 中
C# code


   
     
protected void Application_AuthorizeRequest( object sender, System.EventArgs e) { HttpApplication App = (HttpApplication) sender; HttpContext Ctx = App.Context ; // 获取本次Http请求相关的HttpContext对象 if (Ctx.Request.IsAuthenticated == true ) // 验证过的用户才进行role的处理 { FormsIdentity Id = (FormsIdentity)Ctx.User.Identity ; FormsAuthenticationTicket Ticket = Id.Ticket ; // 取得身份验证票 string [] Roles = Ticket.UserData.Split ( ' , ' ) ; // 将身份验证票中的role数据转成字符串数组 Ctx.User = new GenericPrincipal (Id, Roles) ; // 将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息 } }

你可能感兴趣的:(asp.net)