如何在运行时重新加载自定义属性?ASP.NET 核心 MVC

克里斯

我有一个应用程序,用户可以在其中创建新角色。某些操作只能由某些角色访问。为了检查是否允许用户执行某个操作,我使用了自定义 AuthorizeAttribute,类似于https://stackoverflow.com/a/40300184

[AuthorizeRoles(Permission.Unlink, Permission.Link)] 
[HttpGet("link")]
    public IActionResult Link(int id)
    {
        ...
    }

AuthorizeRolesAttribute 类:

public class AuthorizeRolesAttribute : AuthorizeAttribute
{
    public AuthorizeRolesAttribute(params Permission[] permissions)
    {   
        Roles = GetRoles(permissions);
    }
}

获取角色:

public static string GetRoles(params Permission[] permissions)
{
    DataRowCollection rows = DatabaseHelper.RoleTable.Rows;
    List<string> allowedRoles = new List<string>();
    foreach (DataRow row in rows)
    {
        bool allowed = true;
        foreach (Permission permission in permissions)
        {
            if ((bool)row[permission.ToString()] == false)
                allowed = false;
        }
        //if all required permissions are true in this role it is added to the allowed roles
        if (allowed)
            allowedRoles.Add(row["ROLE"].ToString());
    }
    return string.Join(",", allowedRoles);
}

当应用程序启动时,每个具有 AuthorizeRolesAttribute 的方法都会调用 GetRoles 方法来确定允许哪些角色使用该方法。但是,当添加新角色时,这适用于现有角色。该属性不会重新评估角色。我需要更新该属性并允许新角色使用该方法而无需重新启动应用程序。

添加新角色后,我尝试运行以下代码。(如https://stackoverflow.com/a/12196932所建议

typeof(UsersController).GetMethod(nameof(UsersController.Link)).GetCustomAttributes(false);

这确实会导致 AuthorizeRolesAttribute 再次调用 GetRoles(),并且会返回一个包含新角色的字符串。但是,当尝试以具有新角色的用户身份访问“链接”方法时,我收到 403 禁止状态。

克里斯

我找到了解决方案。取而代之的是:

public class AuthorizeRolesAttribute : AuthorizeAttribute
{
    public AuthorizeRolesAttribute(params Permission[] permissions)
    {   
        Roles = GetRoles(permissions);
    }
}

我现在有这个:

public class AuthorizeRolesAttribute : Attribute, IAuthorizationFilter 
{
    private readonly Permission[] permissions;
    public AuthorizeRolesAttribute(params Permission[] permissions)
    {
        this.permissions = permissions;
    }

    public void OnAuthorization(AuthorizationFilterContext context)
    {
        string[] roles = Authentication.GetRoles(permissions).Split(",");
        bool allowed = context.HttpContext.User.Claims.Any(c => c.Type.Contains("role") && roles.Contains(c.Value));
        if (!allowed)
            context.Result = new ForbidResult();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用MVC(.Net核心MVC 3.1)运行IdentityServer 4(.Net核心3.1)

自定义路由是不是在asp.net核心MVC打?

Asp.net mvc 核心购买模板按钮自定义 onClick 事件在控制器

如何使用Ajax在Asp.net核心MVC中刷新数据而无需重新加载页面?

在运行时更改asp.net核心模型属性名称

asp.net mvc核心如何返回Json查询

如何在 MVC .net 核心中授权会话?

从mvc.net核心中的数据库加载图像

ASP.net核心MVC局部视图无法卸载JS

Asp.Net核心MVC Javascript Ajax参数null HttpPost

在 ASP.NET 核心 MVC 项目上启用 CORS

试图强制 ASP.NET 核心 MVC 使用 HTTPS

使用旧的 asp.net mvc(非核心)设置 DryIoc

asp.net mvc核心-等效于User.IsSignedIn()

asp.net mvc核心依赖注入构造函数参数

在ASP.NET MVC核心中绑定Guid参数

无法在asp.net核心MVC中拖入路由的URL

ASP.NET 核心 MVC。IDataProtectionKeyContext 接口的实现

ASP.NET MVC核心-多对多关系-创建视图

ASP.net 核心 MVC 服务文件最佳实践

Asp.Net 核心 MVC 微软实体框架

用于 jquery CRUD 的 Asp.net 核心 mvc + kendo ui

ASP.NET MVC如何在运行时添加新页面?

将类实现从 asp.net mvc 移植到 asp.net mvc 核心

如何在asp.net核心中更改`ReflectionIT.Mvc.Paging`包样式

如何在asp.net核心项目的MVC视图中读取Request.Form值?

Asp.Net核心MVC6如何在Identity 3中初始添加角色

我可以在ASP.NET MVC 5(非MVC核心)项目中使用ILoggerFactory吗?

将 json 发布到 mvc .net 核心