簡體   English   中英

Blazor WASM Cookie 身份驗證 .NET5

[英]Blazor WASM Cookie Authentication .NET5

我在 .NET5 中構建了我的第一個 Blazor WASM(客戶端和服務器)應用程序,並且在身份驗證方面遇到了問題。 我的目標是使用身份,因為我不想使用 localstorage 並且只有 JWT (或類似的)cookie 身份驗證,如下圖所示: Chrome DevTools

我曾嘗試使用

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie( ... )

在服務器端,但我得到一個錯誤說

There is no registered service of type 'Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider'. .

我沒有錯誤

builder.Services.AddOidcAuthentication

在客戶端的 Program.cs 中,但在我登錄后應用程序沒有授權我(我的 cookie 中有一個令牌)。

我覺得我已經嘗試了所有 Microsoft Docs 和教程,但沒有運氣,所以如果有人對此有解決方案,我將不勝感激!

我有項目 Blazor webassembly 已通過身份服務器進行身份驗證,您可以在此處找到完整代碼https://github.com/kdehia/BlazorSecureWithIdentity_API_GRPC

在 main.cs 中,您應該從 appsettings.json 獲取配置值

builder.Services.AddOidcAuthentication(options =>
        {

            builder.Configuration.Bind("oidc", options.ProviderOptions);

        });

你的 appsettings.json 應該是這樣的

{
  "oidc": {
    "Authority": "https://localhost:5005/",
    "ClientId": "blazorWASM",
    "DefaultScopes": [
      "openid",
      "profile",
      "companyApi",
      "ApplicationRole",
      "GRPC"
    ],
    "PostLogoutRedirectUri": "authentication/logout-callback",
    "RedirectUri": "authentication/login-callback",
    "ResponseType": "code"
  }
}

下載完整的項目,如果您有任何問題,請告訴我

暫無
暫無

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

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