繁体   English   中英

如何为ActiveAuthenticationSchemes设置默认值?

[英]How do i set up a default for ActiveAuthenticationSchemes?

在我的ASP.Net Core 2项目中,我有一个我想要插入的自定义AuthenticationHandler中间件。

public class BasicAuthenticationMiddleware : AuthenticationHandler<AuthenticationSchemeOptions>
{
    public BasicAuthenticationMiddleware(IOptionsMonitor<AuthenticationSchemeOptions> options,
        ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
        : base(options, logger, encoder, clock)
    {
    }
    protected override Task<AuthenticateResult> HandleAuthenticateAsync()
    {
        var principal = new GenericPrincipal(new GenericIdentity("User"), null);
        var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), "BasicAuth");
        return Task.FromResult(AuthenticateResult.Success(ticket));
    }
}

在我的创业公司中,我有以下内容:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = "BasicAuth";
        options.DefaultChallengeScheme = "BasicAuth";
        options.AddScheme("BasicAuth", x => {
            x.DisplayName = "BasicAuthenticationMiddleware";
            x.HandlerType = typeof(BasicAuthenticationMiddleware);
        });
    });
}

最后我的视图控制器:

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values/Works
    [HttpGet]
    [Route("Works")]
    [Authorize(ActiveAuthenticationSchemes = "BasicAuth")]
    public string Works()
    {
        return "works";
    }

    // GET api/values/DoesNotWork
    [HttpGet]
    [Route("DoesNotWork")]
    [Authorize]
    public string DoesNotWork()
    {
        return "does not work";
    }

}

当我将ActiveAuthenticationSchemes指定给我的方案名称时,将调用我的身份验证器HandleAuthenticateAsync ,否则它将不会。 我有一个演示应用程序显示此处的行为: https//github.com/JohnPAguirre/AuthenticationSchemaProblem

我希望我的BasicAuthenticationMiddleware使用我的演示逻辑记录每个人。 如何为所有请求将ActiveAuthenticationSchemes默认为“BasicAuth”?

任何人对我可能缺少的东西都有任何想法?

我设法设置了默认的身份验证方案通过将我想要的方案设置为DefaultPolicy的唯一身份验证方案进行授权。 在配置中使用以下内容。 我在AddMvcAddAuthentication之间使用它,它工作正常。

services.AddAuthorization(config => {
    var def = config.DefaultPolicy;
    config.DefaultPolicy = new AuthorizationPolicy(def.Requirements, 
        new List<string>(){ "BasicAuth" });
});

我不认为你可以设置默认值,但你有其他选择。

  1. 创建自己的自定义授权属性:

     public class BasicAuthAuthorizeAttribute : AuthorizeAttribute { public BasicAuthAuthorizeAttribute() { ActiveAuthenticationSchemes = "BasicAuth"; } } 

    并像以前一样在你的行动中使用它:

     [BasicAuthAuthorize] public string SomeAction() { //snip } 
  2. Authorize属性添加到您的所有操作,并仅在需要时覆盖它。 要做到这一点,在你的``方法:

     public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.Filters.Add(new AuthorizeAttribute { ActiveAuthenticationSchemes = "BasicAuth" }); }); //snip } 

    并覆盖它:

     [AllowAnonymous] public string UnsecureAction() { //snip } 

我使用了类似的代码,它完美无缺。 我看到的主要区别是我使用AddScheme函数链接AddAuthentication而不是它的配置。

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = "BasicAuth";
    options.DefaultChallengeScheme = "BasicAuth";
})
AddScheme<AuthenticationSchemeOptions, BasicAuthenticationMiddleware>
    ("BasicAuth", "BasicAuthenticationMiddleware", x => { });

其余的代码似乎很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM