我通常在我的项目中使用这些代码:
If user.IsInRole("Admin") Then
deleteButton.Visible = True
else
deleteButton.Visible = False
但我想控制角色,可以在数据库中看到这个按钮。
为此,数据库设计应该如何?
谢谢。
我通常在我的项目中使用这些代码:
If user.IsInRole("Admin") Then
deleteButton.Visible = True
else
deleteButton.Visible = False
但我想控制角色,可以在数据库中看到这个按钮。
为此,数据库设计应该如何?
谢谢。
使设计随心所欲,但在 ASP.NET 端实现您自己的 MembershipProvider。这会将您的数据库设计转换为 .NET 可以使用的用户/角色。之后,您可以像往常一样使用它 - 使用user.isInRole("Admin")
:)
LDAP 是授权和身份验证的最佳选择。您可以将 openLDAP API 用于相同目的。
好吧,一种设计是有如下表格:
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
如果我有一个数据库设计来控制它,我不需要更改我的代码。
嗯,应该怎么样?
代码:
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();
}
}
网络配置:
<?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>
假设您使用.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" });