我通常在我的项目中使用这些等代码:如何设计数据库的授权和认证
If user.IsInRole("Admin") Then
deleteButton.Visible = True
else
deleteButton.Visible = False
但我想控制的角色,它可以在数据库中看到此按钮。
为此,数据库设计应该如何?
谢谢。
我通常在我的项目中使用这些等代码:如何设计数据库的授权和认证
If user.IsInRole("Admin") Then
deleteButton.Visible = True
else
deleteButton.Visible = False
但我想控制的角色,它可以在数据库中看到此按钮。
为此,数据库设计应该如何?
谢谢。
假设您使用.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" });
无论你想成为什么样的设计,但在ASP.NET端实现你自己的MembershipProvider。这会将您的数据库设计转换为.NET可以使用的用户/角色。之后,你可以作为平时使用它 - 与user.isInRole("Admin")
:)
嘛,一个设计是具有表,如:
User(UserID, ...) PK = UserID
Role(RoleID, RoleName, ...) PK = RoleID
UserHasRole(UserHasRoleID, UserID, RoleID) PK=UserHasRoleID ; Unique= (UserID, RoleID)
这是一种方法。这是一个基于角色的系统,而不是一个任意的基于对象的授权系统(在一个任意的系统中,你可以为每个对象设置权限,比如说这个用户x具有对客户的DELETE权限,或类似的东西)。
可能是我应该更清楚,但我不知道如何:)。我会再试一次。
例如我用我的deletebutton验证码:
if user.isInRole("Admin") then
deleteButton.visible = true
else
deleteButton.visible = false
AFTE一个整体,作出这样的,用户有角色“主持人”也应该看到删除按钮的决定。所以,我要改变我的代码是这样的:
if user.isInRole("Admin","Moderator") then
deleteButton.visible = true
else
deleteButton.visible = false
如果我有一个数据库设计采取控制这一点,我并不需要更改我的代码吧。
那么,它应该怎么样?
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>
为什么不deleteButton.Visible = user.isInRole( “管理”)???? – 2008-12-08 08:10:29
是的,你的代码更好,但我只是把它作为一个例子;) – mavera 2008-12-08 09:12:49
一个不好的例子,BTW。请改变你的问题。好痛。 – 2008-12-24 02:49:02