asp.net-core - 为什么 ASP.NET Core 将声明两次添加到 User.Claims 属性中?

标签 asp.net-core asp.net-core-mvc identityserver3 coreclr

我有 asp.net 核心应用程序,该应用程序使用 IdentityServer3 使用 OpenIdConnect 身份验证。当用户成功通过身份验证时,应用程序会从身份服务器接收到正确的声明。我可以在 OnTokenValidatd 中调试行 TokenValidatedContext.Ticket.Principal.Claims 并确保应用程序收到所有声明。

代码片段

    var connectOptions = new OpenIdConnectOptions()
        {
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            Authority = authority,
            ClientId = clientId,
            ResponseType = IdentityConstant.IdTokenClaim,
            AuthenticationScheme = IdentityConstant.OpenIdAuthenticationScheme,
            SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme,
            PostLogoutRedirectUri = postlogoutRedirectUri,
            CallbackPath = IdentityConstant.CallbackPath,
            Events = new OpenIdConnectEvents()
            {
                OnTokenValidated = async context =>
                {
                    var claims = context.Ticket.Principal.Claims;
                    await Task.FromResult(0);
                }
            }
        };

下面是 OnTokenValidated 处理程序中 TokenValidatedContext.Ticket.Principal.Claims 的快速观察

enter image description here

但是,当我在 Home Controller 中调试 User.Cliams 时成功验证后,我看到所有声明都添加了两次。
下面是 Home Controller 中 User.Claims 的快速查看

enter image description here

为什么在 User.Claims 中添加了两次声明?

最佳答案

因为您将 openidconnect 的 AutomaticAuthenticate 设置为 true。如果您查看用户身份,您会看到有两个身份(一个用于 cookie,另一个用于 openidconnect 身份验证)。由于 User.Claims 是这些身份声明的总和,您会看到两次声明。因此,从 openidconnect 选项中删除 AutomaticAuthenticate = true, 可以解决问题。

关于asp.net-core - 为什么 ASP.NET Core 将声明两次添加到 User.Claims 属性中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39881401/

相关文章:

服务结构上托管的ASP.Net核心项目无法启动

asp.net - 使类库与 DNX Core 5 兼容

asp.net-core - 如何在 ASP.NET Core 客户端中使用 IdentityServer3?

identity - 使用刷新 token 时出现 Invalid_Grant 错误

asp.net-mvc - 在不使用 EF 的情况下在 appsettings.json 中获取多个连接字符串

c# - asp.net 核心 5,错误 : The SSL connection could not be established

asp.net-core - 使用 Azure AD V2.0 (MSAL) 和 Asp .Net Core 2.0 获取刷新 token

c# - decimal.ToString ("C") 在 Linux 上生成 ¤ 货币符号

asp.net-core - 将命名空间添加到 ASP.NET MVC 6 中的所有 View

asp.net-identity - 如何向IdentityServer DI框架注册ApplicationUserManager?