Dotnet core 2.0 身份驗證多模式身份 cookie 和 jwt
在 dotnet core 1.1 asp 中,我能夠通過執行以下操作來配置和使用身份中間件和 jwt 中間件:
app.UseIdentity(); app.UseJwtBearerAuthentication(new JwtBearerOptions() {});現在已經改變了,我們使用以下方式實現中間件:
app.UseAuthentication();設置的配置是通過 Startup.cs 的 ConfigureServices 部分完成的。
在遷移文件中有一些關於使用授權模式的參考:
<https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x#authentication-middleware-and-services>
在 2.0 項目中,身份驗證是通過服務配置的。每個身份驗證方案都在 Startup.cs 的 ConfigureServices 方法中註冊。UseIdentity 方法被替換為 UseAuthentication。
另外還有一個參考:
設置預設身份驗證方案
在 1.x 中,AutomaticAuthenticate 和 AutomaticChallenge 屬性旨在設置在單個身份驗證方案上。沒有很好的方法來執行此操作。
在 2.0 中,這兩個屬性已作為單個 AuthenticationOptions 實例上的標誌被刪除,並已移至基本 AuthenticationOptions 類中。可以在 Startup.cs 的 ConfigureServices 方法中的 AddAuthentication 方法呼叫中配置屬性:
或者,使用 AddAuthentication 方法的重載版本來設置多個屬性。在以下重載方法範例中,預設方案設置為 CookieAuthenticationDefaults.AuthenticationScheme。身份驗證方案也可以在您的個人 [Authorize] 屬性或授權策略中指定。
在 dotnet core 2.0 中是否仍然可以使用多個身份驗證模式?我無法獲得尊重 JWT 配置(“Bearer”模式)的策略,並且目前只有 Identity 與這兩個配置一起工作。我找不到多個身份驗證模式的任何範例。
編輯:
我重新閱讀了文件,現在明白了:
app.UseAuthentication()針對預設模式添加自動身份驗證。Identity 為您配置預設模式。
通過在 Startup.cs 配置中執行以下操作,我已經解決了似乎針對新 api 的 hack 的問題:
app.UseAuthentication(); app.Use(async (context, next) => { if (!context.User.Identity.IsAuthenticated) { var result = await context.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme); if (result?.Principal != null) { context.User = result.Principal; } } await next.Invoke(); });這是執行此操作的正確方法,還是應該使用框架、DI 和介面來自定義 IAuthenticationSchemeProvider 的實現?
編輯 - 實現的更多細節以及在哪裡可以找到它。
JWT 配置可以在這裡找到,我正在使用策略來定義授權,其中包括接受的身份驗證模式:
<https://github.com/Arragro/ArragroCMS/blob/master/src/ArragroCMS.Management/Startup.cs>
自定義中間件仍在實施。Auth 控制器在這裡:
<https://github.com/Arragro/ArragroCMS/blob/master/src/ArragroCMS.Web.Management/ApiControllers/AuthController.cs>
它使用應用程序生成的 API 密鑰來獲得對數據的只讀訪問權限。您可以在此處找到使用該策略的控制器的實現:
<https://github.com/Arragro/ArragroCMS/blob/master/src/ArragroCMS.Web.Management/ApiControllers/SitemapController.cs>
將 DB Connection 字元串更改為指向您的 SQL Server,然後執行應用程序。它會自動遷移數據庫並配置管理員使用者 (support@arragro.com - ArragroPassword1!)。然後轉到菜單欄中的“設置”選項卡並點擊“配置 JWT ReadOnly API 密鑰設置”以獲取密鑰。在 postman 中,通過配置新選項卡並將其設置為 POST 並使用以下地址獲取 jwt 令牌:
<http://localhost:5000/api/auth/readonly-token>
提供標題: Content-Type: application/json
供給身體:
{ "apiKey": "the api token from the previous step" }複製響應中的令牌,然後在郵遞員中使用以下內容:
<http://localhost:5000/api/sitemap/flat>
Authorization: "bearer - The token you received in the previous request"由於自定義中間件,它將最初工作。註釋掉上面提到的程式碼,然後再試一次,你會收到 401。
編輯 -@DonnyTian 下面的回答涵蓋了我在他的評論中的解決方案。我遇到的問題是在 UseMvc 上設置預設策略,但不提供架構:
services.AddMvc(config => { var defaultPolicy = new AuthorizationPolicyBuilder(new[] { JwtBearerDefaults.AuthenticationScheme, IdentityConstants.ApplicationScheme }) .RequireAuthenticatedUser() .Build(); config.Filters.Add(new AuthorizeFilter(defaultPolicy)); config.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); config.Filters.Add(new ValidateModelAttribute()); });按照建議,這無需自定義中間件即可工作。
Asp.Net Core 2.0 絕對支持多種身份驗證方案。您可以嘗試在
Authorize屬性中指定模式,而不是使用身份驗證中間件進行黑客攻擊:[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]我試了一下,效果很好。假設您已添加 Identity 和 JWT,如下所示:
services.AddIdentity<ApplicationUser, ApplicationRole>() services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)由於
AddIdentity()已經將 cookie 身份驗證設置為預設模式,我們必須在Authorize控制器的屬性中指定模式。目前,我不知道如何覆蓋由 設置的預設模式AddIdentity(),或者我們最好不要這樣做。一種解決方法是編寫一個新類(您可以將其稱為 JwtAuthorize),該類派生自Bearer
Authorize並將其作為預設架構,因此您不必每次都指定它。更新
找到了覆蓋身份預設身份驗證方案的方法!
而不是下面的行:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)使用以下重載設置預設架構:
services.AddAuthentication(option => { option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options =>....更新 2 如評論中所述,您可以通過將身份和 JWT 身份驗證連接在一起來啟用它們。
[Authorize(AuthenticationSchemes = "Identity.Application" + "," + JwtBearerDefaults.AuthenticationScheme)]