簡體   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