【问题标题】:Blazor WASM Cookie Authentication .NET5Blazor WASM Cookie 身份验证 .NET5
【发布时间】:2021-02-01 16:53:38
【问题描述】:

我在 .NET5 中构建了我的第一个 Blazor WASM(客户端和服务器)应用程序,但在身份验证方面遇到了问题。 我的目标是使用 Identity,因为我不想使用 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 和教程,但没有运气,所以如果有人对此有解决方案,我将不胜感激!

【问题讨论】:

    标签: c# blazor blazor-webassembly .net-5 asp.net-blazor


    【解决方案1】:

    我有项目 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"
      }
    }
    

    下载完整的项目,如果您有任何问题,请告诉我

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2021-11-05
      • 2021-04-30
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      相关资源
      最近更新 更多