ASP.NET MVC3角色和权限管理 - >使用运行时权限分配

Ant*_*lin 18 asp.net asp.net-mvc asp.net-membership asp.net-roles asp.net-mvc-3

ASP.NET MVC允许用户在设计时分配功能(即操作)的权限,就像这样.

[Authorize(Roles = "Administrator,ContentEditor")]
public ActionResult Foo()
{
    return View();
}
Run Code Online (Sandbox Code Playgroud)

要实际检查权限,可以在(Razor)视图中使用以下语句:

@if (User.IsInRole("ContentEditor"))
{
    <div>This will be visible only to users in the ContentEditor role.</div>
}
Run Code Online (Sandbox Code Playgroud)

此方法的问题是必须在设计时设置所有权限并将其指定为属性.(属性与DLL编译所以我目前知道任何机制的应用属性(以允许额外的权限),如[授权(角色="管理员,ContentEditor")]在运行时.

在我们的用例中,客户端需要能够在部署后更改用户拥有哪些权限.

例如,客户端可能希望允许ContentEditor角色中的用户编辑特定类型的某些内容.可能不允许用户编辑查找表值,但现在客户端希望允许此操作而不授予用户下一个更高角色的所有权限.相反,客户端只想修改用户当前角色可用的权限.

有哪些策略可用于允许在属性之外(如在数据库中)定义MVC控制器/视图/动作的权限,并在运行时进行评估和应用?

如果可能,我们非常希望尽可能地坚持ASP.NET成员资格和角色提供程序功能,以便我们可以继续利用它提供的其他好处.

提前感谢您的任何想法或见解.

Dar*_*rov 21

有哪些策略可用于允许在属性之外(如在数据库中)定义MVC控制器/视图/动作的权限,并在运行时进行评估和应用?

自定义Authorize属性是实现此目的的一种可能性:

public class MyAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        Roles = ... go ahead and fetch those roles dynamically from wherever they are stored
        return base.AuthorizeCore(httpContext);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后:

[MyAuthorize]
public ActionResult Foo()
{
    return View();
}
Run Code Online (Sandbox Code Playgroud)

  • 如果我在这里错了,请纠正我,但每次引用自定义属性时查找角色效率不高?将角色分配给User对象一次以允许使用User.IsInRole是不是更好,这应该可以在Application_AuthenticateRequest中使用:Context.User = new GenericPrincipal(Context.User.Identity,roles); (5认同)

blo*_*art 16

因为我很懒,所以我不能打扰我自己的属性并使用FluentSecurity.除了在运行时应用规则的能力之外,它还允许以自定义方式检查角色成员身份.在我的情况下,我为每个角色设置了一个配置文件,然后我实现了类似下面的内容;

// Map application roles to configuration settings
private static readonly Dictionary<ApplicationRole, string> 
    RoleToConfigurationMapper = new Dictionary<ApplicationRole, string>
        {
            { ApplicationRole.ExceptionLogViewer, "ExceptionLogViewerGroups" }
        };
Run Code Online (Sandbox Code Playgroud)

然后像这样应用应用程序角色

SecurityConfigurator.Configure(
    configuration =>
    {
        configuration.GetAuthenticationStatusFrom(() =>
            HttpContext.Current.User.Identity.IsAuthenticated);
        configuration.GetRolesFrom(() => 
            GetApplicationRolesForPrincipal(HttpContext.Current.User));
        configuration.ForAllControllers().DenyAnonymousAccess();
        configuration.For<Areas.Administration.Controllers.LogViewerController>()
            .RequireRole(ApplicationRole.ExceptionLogViewer);
    });

filters.Add(new HandleSecurityAttribute());
Run Code Online (Sandbox Code Playgroud)

然后通过执行检查

public static object[] GetApplicationRolesForPrincipal(IPrincipal principal)
{
    if (principal == null)
    {
        return new object[0];
    }

    List<object> roles = new List<object>();
    foreach (KeyValuePair<ApplicationRole, string> configurationMap in
             RoleToConfigurationMapper)
    {
        string mappedRoles = (string)Properties.Settings.Default[configurationMap.Value];

        if (string.IsNullOrEmpty(mappedRoles))
        {
            continue;
        }

        string[] individualRoles = mappedRoles.Split(',');
        foreach (string indvidualRole in individualRoles)
        {
            if (!roles.Contains(configurationMap.Key) && principal.IsInRole(indvidualRole))
            {
                roles.Add(configurationMap.Key);
                if (!roles.Contains(ApplicationRole.AnyAdministrationFunction))
                {
                    roles.Add(ApplicationRole.AnyAdministrationFunction);
                }
            }
        }
    }

    return roles.ToArray();
}
Run Code Online (Sandbox Code Playgroud)

您当然可以从数据库中提取角色.关于这一点的好处是我可以在开发过程中应用不同的规则,而且有人已经为我做了艰苦的工作!