具有动态角色的 FormsAuthentication

发布于 2024-11-27 07:31:44 字数 115 浏览 2 评论 0原文

我有一个带有角色表和权限(每个表单的用户权限)表的应用程序,不同的角色具有不同的访问级别,每个用户对每个表单都有特定的访问权限。 我可以使用 FormsAuthentication 来实现它吗?

谢谢

i have a application with a roles table and a permission (user permissions per form) table different roles has different access levels and each user has specific access permissions to each form .
can i implement it using FormsAuthentication ?

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心作怪 2024-12-04 07:31:44

您必须将列表或角色传递给 FormsAuthenticationTicket

这是完整的代码,我还添加了注释。

protected void lbtnSignIn_Click(object sender, EventArgs e)
{
 .......Login credential checking code......
 .......If the use verified, then add the roles to FormsAuthenticationTicket 
 .......I am assuming in the below code, you are getting list of roles from DB in DataTable
 String roles = String.Empty;
 if (dtblUsersRoles.Rows.Count > 0)
    {
     for (int count = 0; count < dtblUsersRoles.Rows.Count; count++)
     {
      //build list of roles in comma seperate
      roles = roles + "," + dtblUsersRoles.Rows[count]["RoleName"].ToString();
     }
    }

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtUserID.Text, 
DateTime.Now, DateTime.Now.AddMinutes(30), false, roles.Substring(1, roles.Length - 1), FormsAuthentication.FormsCookiePath);
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
Response.Cookies.Add(cookie);
}

然后你可以检查用户,如果他处于某个角色

 if (HttpContext.Current.User.IsInRole("Super Admin"))
 {
  ...................
 }  

You have to pass the list or roles to FormsAuthenticationTicket

Here is the complete code, I have added comments as well.

protected void lbtnSignIn_Click(object sender, EventArgs e)
{
 .......Login credential checking code......
 .......If the use verified, then add the roles to FormsAuthenticationTicket 
 .......I am assuming in the below code, you are getting list of roles from DB in DataTable
 String roles = String.Empty;
 if (dtblUsersRoles.Rows.Count > 0)
    {
     for (int count = 0; count < dtblUsersRoles.Rows.Count; count++)
     {
      //build list of roles in comma seperate
      roles = roles + "," + dtblUsersRoles.Rows[count]["RoleName"].ToString();
     }
    }

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtUserID.Text, 
DateTime.Now, DateTime.Now.AddMinutes(30), false, roles.Substring(1, roles.Length - 1), FormsAuthentication.FormsCookiePath);
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
Response.Cookies.Add(cookie);
}

then you can check the user, if he lies in certain role

 if (HttpContext.Current.User.IsInRole("Super Admin"))
 {
  ...................
 }  
十秒萌定你 2024-12-04 07:31:44

在这种情况下,听起来您可以构建一个自定义表单身份验证提供程序。

这是一个例子
http://www.codeproject.com/KB/web-security/AspNetCustomAuth.aspx

It sounds like you could build a custom forms authentication provider in this case.

Here is an example
http://www.codeproject.com/KB/web-security/AspNetCustomAuth.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文