如何设计数据库进行授权和认证

时间:2008-12-08 08:04:42

标签: asp.net database-design authentication authorization

我通常在我的项目中使用这些代码:

If user.IsInRole("Admin") Then 
  deleteButton.Visible = True 
else 
  deleteButton.Visible = False

但是我想控制角色,可以在数据库中看到这个按钮。

为此目的,数据库设计应该如何?

感谢。

6 个答案:

答案 0 :(得分:1)

使设计成为您想要的,但在ASP.NET端实现您自己的MembershipProvider。这会将您的数据库设计转换为.NET可以使用的用户/角色。之后,您可以像往常一样使用它 - 使用user.isInRole("Admin"):)

答案 1 :(得分:1)

LDAP是授权和身份验证的最佳选择。 您可以将openLDAP API用于相同目的。

答案 2 :(得分:0)

嗯,一个设计就是拥有如下表格:

User(UserID, ...) PK = UserID

Role(RoleID, RoleName, ...) PK = RoleID

UserHasRole(UserHasRoleID, UserID, RoleID) PK=UserHasRoleID ; Unique= (UserID, RoleID)

这是一种方法。这是一个基于角色的系统,而不是一个自由的,基于对象的授权系统(在自由选择的系统中,你可以设置每个对象的权限,比如说这个用户x具有客户的DELETE权限,或类似的东西)。

答案 3 :(得分:0)

可能我应该更清楚,但我不知道如何:)。我会再试一次。

例如我用于我的删除按钮这段代码:

if user.isInRole("Admin") then 
  deleteButton.visible = true 
else 
  deleteButton.visible = false
完成后,做出决定,用户有角色“主持人”也应该看到删除按钮。所以我应该改变我的代码:

if user.isInRole("Admin","Moderator") then 
  deleteButton.visible = true 
else 
  deleteButton.visible = false

如果我有一个数据库设计来控制它,我不需要为它更改我的代码。

嗯,应该怎么样?

答案 4 :(得分:0)

代码:

public class YourSqlRoleProvider : System.Web.Security.RoleProvider
{
    private string ConnectionString { get; set; }

    public override void AddUsersToRoles(string[] userNames, string[] roleNames)
    {
        // logic here
    }

    public override string ApplicationName
    {
        get
        {
            throw new NotSupportedException();
        }
        set
        {
            throw new NotSupportedException();
        }
    }

    public override void CreateRole(string roleName)
    {
        throw new NotSupportedException();
    }

    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        throw new NotSupportedException();
    }

    public override string[] FindUsersInRole(string roleName, string userNameToMatch)
    {
        throw new NotSupportedException();
    }

    public override string[] GetAllRoles()
    {
        // logic here
    }

    public override string[] GetRolesForUser(string userName)
    {
        // logic here
    }

    public override string[] GetUsersInRole(string roleName)
    {
        throw new NotSupportedException();
    }

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

    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
        this.ConnectionString = ConfigurationManager.ConnectionStrings[config["connectionStringName"]].ConnectionString;

        base.Initialize(name, config);
    }

    public override void RemoveUsersFromRoles(string[] userNames, string[] roleNames)
    {
        throw new NotSupportedException();
    }

    public override bool RoleExists(string roleName)
    {
        throw new NotSupportedException();
    }
}

的Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <connectionStrings>
        <clear />
        <add name="YourConnectionString" providerName="System.Data.SqlClient" connectionString="connection string here" />
    </connectionStrings>
    <system.web>
        <roleManager defaultProvider="YourSqlRoleProvider" enabled="true">
            <providers>
                <clear />
                <add name="YourSqlRoleProvider" type="YourSqlRoleProvider" connectionStringName="YourConnectionString" />
            </providers>
        </roleManager>
    </system.web>
</configuration>

答案 5 :(得分:-1)

假设您正在使用.NET,一种方法是实现您自己的角色和成员资格提供程序。然后,你可以通过实现一个包含你想要的项目的界面来添加功能(我刚把这个样本从头顶敲了下来,所以如果看起来有点粗糙我会道歉):

public interface ICustomRole
{
  bool IsInRole(string userName, object[] params roles);
}

public class MyCustomRole : RoleProvider, ICustomRole
{
  public IsInRole(MembershipUser user, object[] params roles)
  {
    if (roles == null || roles.Length == 0)
      throw new ArgumentException("roles");
    // Put your logic here for accessing the roles
  }
}

然后,在您的代码中,您将执行此操作:

bool isValid = ((ICustomRole)Roles.Provider).IsInRole(
  User, new[] { "Admin", "Moderator", "Validator" });