如何使用asp.net和MSSQL管理基于角色的用户权限?

时间:2018-11-02 11:55:37

标签: sql asp.net

我有多个asp.net网络表单和sql express数据库。 在Web表单中,需要从菜单中控制3种类型的操作

 ADD, DELETE , EDIT.

我要创建自定义用户角色,以指定用户权限来管理对用户的访问控制 asp.net c#应用 (菜单,添加,删除和编辑可见性。)

目前,我正在尝试在每个页面上针对每个按钮和链接使用简单的SQL阅读器查询。

 string yes = "1";
 if (rdr["access"].ToString() == yes.ToString())
    Button1.Visible = true;

1 个答案:

答案 0 :(得分:0)

您将需要以下SQL表:

CREATE TABLE Roles
(
    Rolename Text (255) NOT NULL,
    ApplicationName Text (255) NOT NULL, 
    CONSTRAINT PKRoles PRIMARY KEY (Rolename, ApplicationName)
)

CREATE TABLE UsersInRoles
(
    Username Text (255) NOT NULL,
    Rolename Text (255) NOT NULL,
    ApplicationName Text (255) NOT NULL,
    CONSTRAINT PKUsersInRoles PRIMARY KEY (Username, Rolename, ApplicationName)
)

然后构建角色提供程序类

您可以使用EF或ADO.NET来实现方法

public class SqlRoleProvider: RoleProvider
{
    public override void Initialize(string name, NameValueCollection config)
    {
        // 
        // Initialize values from web.config. 
        // 
    }

    public override void AddUsersToRoles(string[]  usernames, string[] rolenames)
    {
    }

    public override void CreateRole(string rolename)
    {
    }

    public override bool DeleteRole(string rolename, bool throwOnPopulatedRole)
    {

    }

    public override string[] GetAllRoles()
    {

    }

    public override string[] GetRolesForUser(string username)
    {

    }

    public override string[] GetUsersInRole(string rolename)
    {

    }

    public override bool IsUserInRole(string username, string rolename)
    {

    }

    public override void RemoveUsersFromRoles(string[] usernames, string[] rolenames)
    {

    }

    public override bool RoleExists(string rolename)
    {

    }

    public override string[] FindUsersInRole(string rolename, string usernameToMatch)
    {

    }

}

您必须在web.config中进行设置

<configuration>
  <connectionStrings>
    <add name="SqlServices" connectionString="DSN=RolesDSN;" />
  </connectionStrings>

  <system.web>
    <authentication mode="Forms" />
      <forms loginUrl="loginvb.aspx"
        name=".ASPXFORMSAUTH" />
    </authentication>

    <authorization>
      <deny users="?" />
    </authorization>

    <roleManager defaultProvider="SqlRoleProvider" 
      enabled="true"
      cacheRolesInCookie="true"
      cookieName=".ASPROLES"
      cookieTimeout="30"
      cookiePath="/"
      cookieRequireSSL="false"
      cookieSlidingExpiration="true"
      cookieProtection="All" >
      <providers>
        <clear />
        <add
          name="SqlRoleProvider"
          type="Samples.AspNet.Roles.SqlRoleProvider"
          connectionStringName="SqlServices" 
          applicationName="SampleApplication" 
          writeExceptionsToEventLog="false" />
      </providers>
    </roleManager>

  </system.web>
</configuration>

Microsoft提供了示例角色提供程序:

配置和数据库架构:

https://msdn.microsoft.com/en-us/library/tksy7hd7.aspx

用于OLEDB(访问数据库)的示例实现,您可以使用SqlConnection,SqlCommand,SqlDataReader ...将实现更改为使用SQL数据库。

https://msdn.microsoft.com/en-us/library/317sza4k.aspx