如何使用 OWIN 安全中间件在应用程序中设置 NameClaimType

How to set the NameClaimType in an application using OWIN security middleware

我创建了一个 OWIN Web 应用程序,它使用 OpenId Connect 通过 Microsoft.Owin.Security.OpenIdConnect 进行身份验证。

虽然身份验证有效,但我发现创建的 ClaimsIdentity.Name 成员为空。 ClaimsIdentity 似乎希望在声明中提供名称:

http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name

但是,Thinktecture IdentityServer v3 提供的名称只是:

name

I find this is configurable via NameClaimType in older ASP.Net applications 但不清楚我是如何用 OWIN 做到这一点的。

使用 OWIN 时如何配置映射到 ClaimsIdentity.Name 的声明?

通过代码挖掘我发现这可以使用 NameClaimTypeNameClaimTypeRetrieverTokenValidationParameters 对象上配置(如果声明类型不固定)。

TokenValidationParameters 在配置中间件时依赖于选项对象。对配置的以下更改适用于所描述的情况,

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
        {
        <existing configuration snipped>,
        TokenValidationParameters =
            {
            NameClaimType = Thinktecture.IdentityServer.Core.Constants.ClaimTypes.Name
            }
        } );

Thinktecture.IdentityServer.Core.Constants.ClaimTypes.Namename。可以提供不同的值以指示应使用不同的声明。