How to create a database for authorization and authentication

Typically, my code uses this code:

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

But I want to control the roles that this button can see in the database.

To this end, what should the database design look like?

Thanks.

+3
source share
6 answers

Assuming you are using .NET, one way to do this is to implement your own role and membership providers. Then you could add functionality by implementing an interface that contained the elements you wanted (I just knocked this sample off the top of my head, so I apologize if this seems a little rude):

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" });
-1

, , ASP.NET MemberhipProvider. /, .NET. - user.isInRole("Admin"):)

+1

LDAP - . API openLDAP .

+1

, , :

User(UserID, ...) PK = UserID

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

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

. , , ( , , x DELETE - ).

0

, , , :). .

For example, I use this code deletebutton:

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

In general, make a decision that the user has the role of "moderator", as well as the delete button. Therefore, I have to change my code as follows:

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

If I have a database project to take control of, I did not need to change its code.

Well, how should it be?

0
source

the code:

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>
0
source

All Articles