如何在运行时重新加载自定义属性?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(),并且会返回一个包含新 Role 的字符串。但是,当尝试以具有新角色的用户身份访问“链接”方法时,我得到 403 Forbidden 状态。


鸿蒙传说
浏览 132回答 1
1回答

MYYA

我找到了解决办法。而不是这个:public class AuthorizeRolesAttribute : AuthorizeAttribute{&nbsp; &nbsp; public AuthorizeRolesAttribute(params Permission[] permissions)&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Roles = GetRoles(permissions);&nbsp; &nbsp; }}我现在有这个:public class AuthorizeRolesAttribute : Attribute, IAuthorizationFilter&nbsp;{&nbsp; &nbsp; private readonly Permission[] permissions;&nbsp; &nbsp; public AuthorizeRolesAttribute(params Permission[] permissions)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.permissions = permissions;&nbsp; &nbsp; }&nbsp; &nbsp; public void OnAuthorization(AuthorizationFilterContext context)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string[] roles = Authentication.GetRoles(permissions).Split(",");&nbsp; &nbsp; &nbsp; &nbsp; bool allowed = context.HttpContext.User.Claims.Any(c => c.Type.Contains("role") && roles.Contains(c.Value));&nbsp; &nbsp; &nbsp; &nbsp; if (!allowed)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.Result = new ForbidResult();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP