Build User rights control system in Asp.net.(Windows Form Authentication)

1.You must change your web system to windows form authentication in web.config

  1. <authentication mode="Forms">
  2.       <forms loginUrl="Pub/Login.aspx" name=".ASPXAUTH"></forms>
  3.     </authentication>

2. Create a new web.config file under the folder you want to protect.

  1. <?xml version="1.0"?>
  2. <configuration>
  3.     <appSettings/>
  4.     <connectionStrings/>
  5.     <system.web>
  6.       <authorization>
  7.         <!--多个角色用,分隔-->
  8.         <allow roles="SuperAdmin,Admin"/>
  9.         <deny users="*" />
  10.       </authorization>
  11.     </system.web>
  12. </configuration>
  13. <!--也可控制某个页的权限
  14.   <location path="AnnounceList.aspx">
  15.      <system.web>
  16.         <authorization>
  17.            <allow roles="admin"/>
  18.            <deny users="*" />
  19.         </authorization>
  20.      </system.web>
  21.   </location>
  22.   <location path="ConfigInfo.aspx">
  23.      <system.web>
  24.         <authorization>
  25.            <allow roles="users"/>
  26.            <deny users="*" />
  27.         </authorization>
  28.      </system.web>
  29.   </location>
  30.   -->

Please be care of the under path for the location nodes. It should be under <configuration> node.

 

In the Login.aspx page,

the code should be like this,

  1. protected void Button1_Click(object sender, EventArgs e)
  2.         {
  3.             string username = UserLogin(TextBox1.Text);
  4.             if (username != null && username != "")
  5.             {
  6.                 string userRole = GetUserRole(username);
  7.                 SetLoginCookie(username, userRole);
  8.                 Response.Redirect("~//highlevel//high.aspx");
  9.             }
  10.             else
  11.             {
  12.                 ltError.Text = "用户名输入错误!请重新输入!";
  13.             }
  14.         }
  15.         public static void SetLoginCookie(string username, string roles)
  16.         {
  17.             //建立身份验证票对象
  18.             FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(30), false, roles, "/");
  19.             //加密序列化验证票为字符串
  20.             string hashTicket = FormsAuthentication.Encrypt(ticket);
  21.             HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
  22.             HttpContext.Current.Response.Cookies.Add(userCookie);
  23.         }

Create Global.asax at the root folder.

  1. protected void Application_AuthenticateRequest(object sender, EventArgs e)
  2.         {
  3.             HttpApplication app = (HttpApplication)sender;
  4.             HttpContext ctx = app.Context; //获取本次Http请求的HttpContext对象  
  5.             if (ctx.User != null)
  6.             {
  7.                 if (ctx.Request.IsAuthenticated == true//验证过的一般用户才能进行角色验证  
  8.                 {
  9.                     System.Web.Security.FormsIdentity fi = (System.Web.Security.FormsIdentity)ctx.User.Identity;
  10.                     System.Web.Security.FormsAuthenticationTicket ticket = fi.Ticket; //取得身份验证票  
  11.                     string userData = ticket.UserData;//从UserData中恢复role信息
  12.                     string[] roles = userData.Split(','); //将角色数据转成字符串数组,得到相关的角色信息  
  13.                     ctx.User = new System.Security.Principal.GenericPrincipal(fi, roles); //这样当前用户就拥有角色信息了
  14.                 }
  15.             }
  16.         }

then the user can't visit the page without the right role. You must assure every person should have correct role.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值