ASP.NET中自定义数据库的用户角色

时间:2021-09-28 12:47:05

I'm creating an app with ASP.NET WebForms. I have custom database with users table. It contains name and role. How can I add roles from DB to website? I want to use something like this:

我正在使用ASP.NET WebForms创建一个应用程序。我有自定义数据库与用户表。它包含名称和角色。如何从DB添加角色到网站?我想用这样的东西:

<location path="path"> 
   <system.web>
      <authorization> 
         <allow roles="role"/>
         <deny users="*"/>
      </authorization>
   </system.web>
</location>

where role should be imported.

应该导入角色的位置。

Thank you!

谢谢!

1 个解决方案

#1


2  

You can implement a custom RoleProvider.

您可以实现自定义RoleProvider。

As a minimum you need to implement Intialize (of course) and the methods GetRolesForUser and IsUserInRole. The other methods are only needed if you want to be able to administer roles through the RoleProvider.

至少你需要实现Intialize(当然)和方法GetRolesForUser和IsUserInRole。只有在希望能够通过RoleProvider管理角色时才需要其他方法。

IsUserInRole can often be implemented as something close to:

IsUserInRole通常可以实现为接近:

public bool IsUserInRole(string username, string roleName)
{
    return GetRolesForUser(username).Contains(roleName);
}

so apart from initialization, which in your case will probably only be storing a database connection string, you only have one simple method to implement.

因此除了初始化之外,在您的情况下可能只存储数据库连接字符串,您只需要一个简单的方法来实现。

#1


2  

You can implement a custom RoleProvider.

您可以实现自定义RoleProvider。

As a minimum you need to implement Intialize (of course) and the methods GetRolesForUser and IsUserInRole. The other methods are only needed if you want to be able to administer roles through the RoleProvider.

至少你需要实现Intialize(当然)和方法GetRolesForUser和IsUserInRole。只有在希望能够通过RoleProvider管理角色时才需要其他方法。

IsUserInRole can often be implemented as something close to:

IsUserInRole通常可以实现为接近:

public bool IsUserInRole(string username, string roleName)
{
    return GetRolesForUser(username).Contains(roleName);
}

so apart from initialization, which in your case will probably only be storing a database connection string, you only have one simple method to implement.

因此除了初始化之外,在您的情况下可能只存储数据库连接字符串,您只需要一个简单的方法来实现。