如何实现用户的权限管理

[复制链接]
查看11 | 回复2 | 2008-5-9 11:39:15 | 显示全部楼层 |阅读模式
我们的做法是写一个UserManage静态类,在里面放上User对象,对象里包含name和key(权限),然后把它对象放在Context(上下文里)。这样,每个页面都可以通过switch(UserManage.User.key) 来判断用户类别。在页的load方法里写switch,如果普通用户不能进,就 case 1 : Response.Redirect("~/Default.aspx",false);
回复

使用道具 举报

千问 | 2008-5-9 11:39:15 | 显示全部楼层
如果用户凭证是Cookie的话,可以在web.config里设置
然后在Login代码如下:private void btnLogin_Click(object sender, System.EventArgs e){ UserManage um = new UserManage(); DataSet ds = um.GetLoginUser(txtUserName.Value,FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Value,"MD5")); if(ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) {
string roles = ds.Tables[0].Rows[0]["Role"].ToString();//把用户权限加入到凭证里,这样页面就可以验证权限了.
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
txtUserName.Value,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
roles);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);
Response.Cookies.Add(authCookie);
Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUserName.Value,false)); }}Global.ascx.cs里代码如下:protected void Application_AuthenticateRequest(Object sender, EventArgs e){ string cookieName = FormsAuthentication.FormsCookieName; HttpCookie authCookie = Context.Request.Cookies[cookieName]; if(authCookie == null)
return; FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); string[] roles = authTicket.UserData.Split(new char[]{','}); FormsIdentity identity = new FormsIdentity(authTicket); GenericPrincipal principal = new GenericPrincipal(identity,roles); Context.User = principal;}
回复

使用道具 举报

千问 | 2008-5-9 11:39:15 | 显示全部楼层
1是管理员2是普通会员3是未注册用户int类型
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行