【问题标题】:Blazor Server: authenticating against Identity Server 4 and using cookies for local authenticationBlazor Server:针对 Identity Server 4 进行身份验证并使用 cookie 进行本地身份验证
【发布时间】:2019-11-18 12:00:35
【问题描述】:

我正在尝试构建一个服务器端 Blazor 应用程序,该应用程序允许用户登录 Identity Server 4 并使用 Cookie 来处理本地授权。这是我当前设置的代码:

services.AddAuthentication(options => {
                               options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                               options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                               options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                           })
        .AddCookie("Cookies")
        .AddOpenIdConnect("oidc",
                          options => {
                              Configuration.GetSection("OidcSettings").Bind(options);

                              // remove unecessary claims
                              options.ClaimActions.MapAllExcept("amr", "sub");
                              options.TokenValidationParameters = new TokenValidationParameters {
                                                                                                    NameClaimType = ClaimTypes.NameIdentifier
                                                                                                };
                          });

我想使用 nameidentifier 声明作为用户名,我的印象是之前的映射应该解决这个问题。不幸的是,它不起作用。下面是一些演示代码:

@page "/"
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor ContextAccessor
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
<h1>Hello, world!</h1>

Welcome to your new app.

<AuthorizeView>
  <NotAuthorized>
    <form method="post" action="/Account/Login">
      <button>Iniciar</button>
    </form>
  </NotAuthorized>
  <Authorized>
    <h1>Hi, @ContextAccessor.HttpContext.User.Identity.Name!</h1>
    <h1>Hi, @(GetUsernameAsync().Result)!</h1>
  </Authorized>
</AuthorizeView>

@code{
    private async Task<string> GetUsernameAsync() {
      var user1 = ContextAccessor.HttpContext.User.Identity.Name;
      var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
      var user = authState.User;

      if (user.Identity.IsAuthenticated)
      {
            return user.Identity.Name;
      }

      return "Nop";
    }
}

在这两种情况下,用户名始终设置为 null。以下是我从调试器中得到的信息:

有几件事我没有得到:

  1. 为什么我会得到这种身份验证类型?不应该是 cookie 或应用程序 cookie(或类似的东西)吗?
  2. 为什么名称声明没有更改为名称标识符?

我肯定错过了一些东西,但我不确定是什么......

谢谢

【问题讨论】:

    标签: authentication identityserver4 openid-connect blazor-server-side


    【解决方案1】:

    好的,经过几次测试,我终于设法让它工作了。我已经开始使用 sub 声明作为我的用户名。我不得不在 OpenId Connect 设置上更改几件事:

    .AddOpenIdConnect("oidc",
                      options => {
                          Configuration.GetSection("OidcSettings").Bind(options);
    
                          options.TokenValidationParameters = new TokenValidationParameters {
                                                                                                NameClaimType = "sub"
                                                                                            };
                      });
    

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 1970-01-01
      • 2017-06-26
      • 2022-01-23
      • 2020-06-08
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 2021-01-29
      相关资源
      最近更新 更多