Asp.net 如何设计用于授权和身份验证的数据库
我通常在项目中使用以下代码:Asp.net 如何设计用于授权和身份验证的数据库,asp.net,database-design,authentication,authorization,Asp.net,Database Design,Authentication,Authorization,我通常在项目中使用以下代码: If user.IsInRole("Admin") Then deleteButton.Visible = True else deleteButton.Visible = False 但我想控制角色,可以在数据库中看到这个按钮 为此,应如何设计数据库 谢谢。让设计成为你想成为的样子,但在ASP.NET端实现你自己的会员资格提供商。这将把数据库设计转换为.NET可以使用的用户/角色。之后,您可以像往常一样使用它-与user.isInRole(“Adm
If user.IsInRole("Admin") Then
deleteButton.Visible = True
else
deleteButton.Visible = False
但我想控制角色,可以在数据库中看到这个按钮
为此,应如何设计数据库
谢谢。让设计成为你想成为的样子,但在ASP.NET端实现你自己的会员资格提供商。这将把数据库设计转换为.NET可以使用的用户/角色。之后,您可以像往常一样使用它-与
user.isInRole(“Admin”)
:)好的,一种设计是有如下表:
User(UserID, ...) PK = UserID
Role(RoleID, RoleName, ...) PK = RoleID
UserHasRole(UserHasRoleID, UserID, RoleID) PK=UserHasRoleID ; Unique= (UserID, RoleID)
这是一种方法。这是一个基于角色的系统,而不是基于任意对象的授权系统(在任意系统中,您可以设置每个对象的权限,比如说此用户x具有客户的删除权限,或者类似的权限)。可能我应该更清楚,但我不知道如何:)。我会再试一次 例如,我使用以下代码作为我的Delete按钮:
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
如果我有一个数据库设计来控制它,我就不需要为它更改代码
那么,应该是怎样的呢 假设您使用的是.NET,一种方法是实现您自己的角色和成员资格提供程序。然后,您可以通过实现一个包含您想要的项目的界面来添加功能(我刚刚把这个示例从我的脑袋上敲了下来,如果它看起来有点粗糙,我很抱歉): 然后,在代码中执行以下操作:
bool isValid = ((ICustomRole)Roles.Provider).IsInRole(
User, new[] { "Admin", "Moderator", "Validator" });
LDAP是授权和身份验证的最佳选项。 您可以出于同样的目的使用openLDAP API。代码:
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>
Why not deleteButton.Visible=user.isInRole(“Admin”)?是的,您的代码更好,但我只是作为示例编写;)一个坏例子,顺便说一句。请改变你的问题。很痛,看看我的答案。代码来自真实世界的银行系统。比皮特的答案好多了。相信我:)请帮你自己一个忙,不要再写这种“如果是真的,否则是假的”的东西了。那么,世界会变得更美好LDAP需要一个目录(AD等),这是一种非常非常耗费资源/时间/金钱的方法
<?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>