c# - 有条件地忽略授权 .NET Core 3.1

标签 c# .net-core authorize

我正在将 Web API 从 .net Framework 迁移到 .net Core。如果应用程序在私有(private)服务器上运行,旧版本能够忽略 Controller 上的授权属性。这是代码。 我知道 .net core 3.1 不提供自定义 AuthorizeAttributes。那不是我的问题。

// A custom AuthroizeAttribute
public class ConditionalAuthorizeAttribute : AuthorizeAttribute
{

    protected override bool IsAuthorized(HttpActionContext httpContext)
    {
        if (environment_private())
            return true;
        else
            return base.IsAuthorized(httpContext);
    }


    private bool environment_private()
    {
        // code that will tell you if you are in your dev environment or not
        return Properties.Settings.Default.PrivateServer;
    }
}

// How it was called from the controller
[ConditionalAuthorize(Roles = "MyRole")]
[Route(...)]
// More code for controller

在我们的私有(private)服务器上运行项目时,我只需要一种简单的方法来授权所有请求(由 appSettings.json 中的变量确定)。我尝试过政策,但我面临以下困难:

1) 我无法将配置中的变量从 Controller 传递给参数化授权属性。

2) 我无法将配置注入(inject)参数化授权属性。

这有效地消除了我以任何方式遵循本指南的能力:https://docs.microsoft.com/en-us/aspnet/core/security/authorization/iauthorizationpolicyprovider?view=aspnetcore-2.2

这引出了我的问题:如何使用 appSettings.json 中的值来覆盖请求是否检查角色?

最佳答案

经过大量研究,我找到了一种使用 TypeFilterAttribute 的方法。 .本质上,它是相同的方法(使用自定义属性过滤所有请求并检查自定义属性中的条件),除了我使用.net Core 支持的方法。

如果您尝试解决同样的问题,以下是我的解决方案的确切步骤。

  • 添加两个文件,“YourAttributeNameAttribute.cs”和“YourFilterNameFilter.cs”。
  • 在“YourAttributeNameAttribute.cs”文件中,代码如下:
  • public class YourAttributeNameAttribute : TypeFilterAttribute
    {
        public YourAttributeNameAttribute(string role) : base(typeof(YourFilterNameFilter))
        {
            Arguments = new object[] { role };
        }
    }
    
  • “YourFilterNameFilter.cs”中的代码:
  • public class YourFilterNameFilter : IAuthorizationFilter
    {
        private readonly string Role;
        public YourFilterNameFilter(string role)
        {
            Role = role;
        }
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            var configuration = context.HttpContext.RequestServices.GetService<IConfiguration>();
    
            // If private server, ignore roles
            if (private_server_logic_here)
                return;
    
            var user = context.HttpContext.User;
    
            // Check role if on public server
            if (!user.IsInRole(Role))
            {
                context.Result = new StatusCodeResult((int)System.Net.HttpStatusCode.Unauthorized);
                return;
            }
        }
    }
    
  • Controller 代码:
  • [YourAttributeName("role_name")]
    [Route("api/my_route")]
    [HttpGet]
    

    关于c# - 有条件地忽略授权 .NET Core 3.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62134901/

    相关文章:

    c# - 如何在NHibernate中查询每个组中的第一个条目

    asp.net - Azure Application Insights 不显示数据

    c# - 将枚举作为参数传递

    c# - 如何根据单选按钮的值启用或禁用组合框 C#

    c# - 容器化.net core应用程序无法连接到容器化mongo db

    c# - ASP .NET Core MVC 部署到 azure Identity Server 4

    ios - Facebook ios sdk authorize 打开两个权限请求对话框

    Swagger/Swashbuckle 授权。默认检查范围

    c# - 裁剪图像 .Net