【问题标题】:How to do simple header authorization in .net core 2.0?如何在 .net core 2.0 中进行简单的标头授权?
【发布时间】:2020-05-18 20:46:14
【问题描述】:

在 .NET Core 2.0 更改后,我一直无法找到有关此特定问题的信息。

我有这样的 cookie 授权:

services.AddAuthentication("ExampleCookieAuthenticationScheme")
    .AddCookie("ExampleCookieAuthenticationScheme", options => {
         options.AccessDeniedPath = "/Account/Forbidden/";
             options.LoginPath = "/Account/Login/";
});

对于另一部分(我的控制器我想简单地基于一个简单的标头进行授权。 在我找到的示例中,要么我无法获取标题,要么它们仅用于 facebook、google、cookie 等。

如何在 .Net core 2.0 中添加执行简单标头检查的授权?

【问题讨论】:

    标签: c# header authorization asp.net-core-webapi .net-core-2.0


    【解决方案1】:

    可以使用自定义中间件执行简单的授权检查。但是如果需要为选定的控制器或操作方法应用自定义中间件,您可以使用中间件过滤器。

    中间件及其应用构建器扩展:

    public class SimpleHeaderAuthorizationMiddleware
        {
            private readonly RequestDelegate _next;
    
            public SimpleHeaderAuthorizationMiddleware(RequestDelegate next)
            {
                _next = next;
            }
    
            public async Task Invoke(HttpContext context){ 
    
                string authHeader = context.Request.Headers["Authorization"];
                if(!string.IsNullOrEmpty(authHeader))
                {
                    //TODO
                    //extract credentials from authHeader and do some sort or validation
                    bool isHeaderValid =  ValidateCredentials();
                    if(isHeaderValid){
                        await _next.Invoke(context);
                        return;
                    }
    
                }
    
                //Reject request if there is no authorization header or if it is not valid
                context.Response.StatusCode = 401; 
                await context.Response.WriteAsync("Unauthorized");
    
            }
    
        }
    
    public static class SimpleHeaderAuthorizationMiddlewareExtension
        {
            public static IApplicationBuilder UseSimpleHeaderAuthorization(this IApplicationBuilder app)
            {
                if (app == null)
                {
                    throw new ArgumentNullException(nameof(app));
                }
    
                return app.UseMiddleware<SimpleHeaderAuthorizationMiddleware>();
            }
        }
    

    为了将中间件用作过滤器,您需要使用Configure 方法创建一个类型,以指定您要使用的中间件管道。

    public class SimpleHeaderAuthorizationPipeline
        {
            public void Configure(IApplicationBuilder applicationBuilder){
                applicationBuilder.UseSimpleHeaderAuthorization();
            }
        }
    

    现在您可以在特定的控制器或操作方法中使用上述类型,如下所示:

    [MiddlewareFilter(typeof(SimpleHeaderAuthorizationPipeline))]
    public class ValuesController : Controller
    {
    }
    

    【讨论】:

    • 如果您只想验证一个简单的 http 标头值,这(仍然)对 2.0 有效,并且比基于策略的东西更直接
    • 我可以在哪里检查标头中的证书?
    • 不错!这个 SimpleHeaderAuthorizationMiddleware 可以与 [Authorize] 属性一起使用吗?因为,如果这个中间件被意外删除了,我希望我的带有 [Authorize] 注释的控制器无法执行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 2014-01-06
    • 2012-06-19
    • 2022-10-13
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    相关资源
    最近更新 更多