簡體   English   中英

ASP.NET Core調用身份驗證

[英]ASP.NET Core Invoke an Authentication

我的問題:如果在使用JWT身份驗證之前在數據庫中為特定的Controller / Action設置了BasicAuth,則需要找到一種使用BasicAuth來授權用戶的方法。

我正在尋找一種從自定義AuthenticationHandler調用兩個不同AuthenticationHandler的方法,這是否有可能,或者我將問題解決了嗎?

protected override Task<AuthenticateResult> HandleAuthenticateAsync()
    {
        //Get the Controller/Action from this.OriginalPath            
        //Ask the DB if the Controller/Action should be authorize using
        //BasicAuth first or just use JWT
        //Call the Basic or JWT Handler
    }

這是到目前為止我發現的最好的解決方案:

        if (AuthType.StartsWith("Basic"))
        {
            var handlers = Context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
            var handler = handlers.GetHandlerAsync(Context, BasicAuthenticationDefaults.AuthenticationScheme).Result;
            var res = handler.AuthenticateAsync().Result;
            Debug.WriteLine(res.Succeeded);
            if (res.Succeeded)                
                return Task.FromResult(res);

        }
        else if(AuthType.StartsWith("Bearer"))
        {
            var handlers = Context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
            var handler = handlers.GetHandlerAsync(Context, JwtBearerDefaults.AuthenticationScheme).Result;
            var res = handler.AuthenticateAsync().Result;
            Debug.WriteLine(res.Succeeded);
            if (res.Succeeded)
                return Task.FromResult(res);
        }

它不適用於WindowsAuthentication。 我只得到一個錯誤說:

沒有配置身份驗證處理程序以對該方案進行身份驗證:Windows

但是我添加了IISDefaults

services.AddAuthentication(IISDefaults.AuthenticationScheme)

請隨時發表評論或編輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM