【问题标题】:dotnet core 3.0 WebApi, applicationPart and authorizationdotnet core 3.0 WebApi、applicationPart和授权
【发布时间】:2020-02-12 13:41:49
【问题描述】:

我们有一个模块化应用程序,这意味着我们的 api 控制器会在启动期间加载。我们将控制器加载到 applicationPart 中,如下所示:

services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
            .ConfigureApplicationPartManager(applicationPartManager =>
            {
                foreach (var module in _modules)
                {
                    var apiControllerAssemblies = module.GetApiControllerAssemblies();

                    foreach (var apiControllerAssembly in apiControllerAssemblies)
                        applicationPartManager.ApplicationParts.Add(new AssemblyPart(apiControllerAssembly));
                }
            });

我们希望通过基本身份验证来保护我们的 API。我创建了一个这样的中间件:

 public class BasicAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
    {
        public BasicAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, 
                                          ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
        {
        }

        protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey("Authorization"))
                return AuthenticateResult.Fail("Missing Authorization 
                                                Header");
            //More to come
        }
    }

中间件在startup.cs中注册如下:

services.AddAuthentication("Basic")
             .AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("Basic", null);

访问本地主机:总是会触发HandleAuthenticateAsync 方法。但是,当我尝试访问 localhost:/user/users 端点时,该方法永远不会遇到断点,并且总是会导致 HTTP 401 Unauthorized。控制器本身标有Authorize 属性。

有什么想法会出错吗?关于我应该从哪里开始寻找解决方案的任何提示?

谢谢!

【问题讨论】:

  • 嘿 Tobias - 你解决了你的问题吗,你找到了更好的方法来在 .Net Core 中实现身份验证。我正在开始一个新项目,想知道您是否找到了更好的方法。
  • HI @Rakesh 最终创建了一个带有实现IAuthorizationFilter的过滤器的属性
  • 谢谢@Tobias,我也会试试的。

标签: c# .net-core


【解决方案1】:

不确定这是否有帮助,但是当我必须实施身份验证时,我就是这样做的。

一个。声明一个扩展AuthenticationSchemeOptions的类

    public class CustomAuthOptions: AuthenticationSchemeOptions
    {
    }

b.声明一个实现AuthenticationHandler&lt;TOptions&gt;的类

    internal class CustomAuthHandler : AuthenticationHandler<CustomAuthOptions>
    {
        IHttpContextAccessor _httpContextAccessor;
        IUser _user;

        public CustomAuthHandler(IOptionsMonitor<CustomAuthOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, 
            IHttpContextAccessor httpContextAccessor, IUser user) : base(options, logger, encoder, clock)
        {
            _httpContextAccessor = httpContextAccessor;
            _user = user;
        }

        protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
        {
            //logic to authenticate
        }

        protected override Task HandleChallengeAsync(AuthenticationProperties properties)
        {
            //more code
        }
   }

c。为AuthenticationBuilder类添加扩展方法

        public static AuthenticationBuilder AddCustomAuth(this AuthenticationBuilder builder,
            Action<CustomAuthOptions> config)
        {
            return builder.AddScheme<CustomAuthOptions, CustomAuthHandler>("CheckInDB", "CheckInDB", config);
        }

d。终于在Startup.cs

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = "CheckInDB";
                options.DefaultChallengeScheme = "CheckInDB";
            }).AddCustomAuth(c => { });

这可能超出了需要,但是当我在同一条船上时,几个月前,我花了好几天时间将所有这些拼凑在一起。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2018-08-14
    • 2019-04-27
    • 1970-01-01
    • 2018-10-30
    相关资源
    最近更新 更多