Blazor 服务器:针对 Identity Server 4 进行身份验证并使用 cookie 进行本地身份验证

路易斯·阿布雷乌

我正在尝试构建一个服务器端 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";
    }
}

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

来自 aspnet 核心上下文的用户身份

有几件事我没有得到:

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

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

谢谢

路易斯·阿布雷乌

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

.AddOpenIdConnect("oidc",
                  options => {
                      Configuration.GetSection("OidcSettings").Bind(options);

                      options.TokenValidationParameters = new TokenValidationParameters {
                                                                                            NameClaimType = "sub"
                                                                                        };
                  });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

.NET Core身份服务器4身份验证VS身份身份验证

在没有浏览器的情况下针对WSO2 Identity Server进行身份验证,并获取SAML2断言消息

ASP.NET Identity 2.0针对我们自己的承载服务器进行身份验证

Identity Server 4 Swagger身份验证

Identity Server 4联合身份验证网关

身份服务器4 Windows身份验证

.Net Core自定义身份验证,使用带有Identity Server 4的API密钥

使用Identity Server的静默身份验证

使用NGINX反向代理后面的docker + kubernetes在带有Identity Server 4的Blazor Server应用程序中添加身份验证和授权

如何在没有Microsoft身份的情况下进行JWT身份验证blazor服务器?

如何使用Blazor服务器对用户进行身份验证

在身份服务器中使用cookie和令牌进行共享身份验证

使用运行Identity Server 4的ASP.NET CORE 3身份验证服务器对ASP.NET MVC 5应用(目标.net 4.5)进行身份验证

使用Cookie进行网络抓取进行身份验证?

如何在blazor Webassembly项目的服务器端控制器中对用户进行身份验证?

Azure DevOps服务-使用本地ADFS进行身份验证

如何使用通过安全Cookie对用户进行身份验证的测试龙卷风服务器处理程序

无法使用Windows身份验证向SMTP服务器进行身份验证

使用Google身份验证在SSH服务器上进行两因素身份验证

Identity Server 3身份验证/授权

如何使用服务器密钥在Google Spreadsheet Api V4中进行身份验证

身份服务器 4 身份验证

我无法让 Identity Server 4 对客户端进行身份验证。服务器和客户端正在运行 Core 2.0

使用 Asp.net Core 2.1 和 Identity Server 4 的身份验证/授权

如何让 Blazor 应用程序查看本地 SQL Server 以进行身份验证?

oidcSecurityService checkAuth 方法在使用 Identity Server 进行身份验证时返回 false

使用 Blazor 服务器和 Azure AD 身份验证获取身份验证令牌

Aspnet Core 3.1 MVC Razor 页面在使用 Identity Server 4 OpenIdConnect 进行身份验证后获得 302

Identity Server 4 与 ASP.NET Core MVC 进行身份验证然后调用 Angular SPA