C# AspNet用户声明丢失

C# AspNet用户声明丢失,c#,asp.net-mvc,asp.net-identity,identityserver4,claims,C#,Asp.net Mvc,Asp.net Identity,Identityserver4,Claims,研究身份提供者的概念证明,需要帮助理解aspnet身份的一些细微差别,特别是与用户声明相关的细微差别 我正在努力实现的目标: 1公开一个MVC应用程序,该应用程序提供对2个微服务之一的安全访问,这些微服务将公开体育和运动员数据 2允许本地用户帐户和外部帐户,如google或facebook auth 当前设置: 1个Asp.Net MVC应用程序,利用Identity Server 4进行身份验证 2两个Web Api 2 Web服务1个用于体育,1个用于运动员。体育API不安全…即…开放API

研究身份提供者的概念证明,需要帮助理解aspnet身份的一些细微差别,特别是与用户声明相关的细微差别

我正在努力实现的目标:

1公开一个MVC应用程序,该应用程序提供对2个微服务之一的安全访问,这些微服务将公开体育和运动员数据

2允许本地用户帐户和外部帐户,如google或facebook auth

当前设置:

1个Asp.Net MVC应用程序,利用Identity Server 4进行身份验证

2两个Web Api 2 Web服务1个用于体育,1个用于运动员。体育API不安全…即…开放API。不过,运动员API应该通过基于策略的授权进行保护

3 Identity Server 4 web应用程序,通过Identity Server 4 nuget软件包利用实体框架和ASP.NET标识

4 SQL Server数据库完全配置为api资源、标识资源、客户端、两个具有不同角色的测试用户以及声称要使用的测试用户

目前有效的方法:

1所有用户都可以登录本地和谷歌

2个索赔似乎正在加载,因为我预计仍然难以理解Identity Server数据模型中索赔表的所有关系

3 MVC应用程序显示id_令牌、access_令牌、refresh_令牌,然后循环查看用户的声明并显示这些声明

什么不起作用:

1并非所有我认为应该在mvc应用程序中显示的声明都实际显示出来

举例说明我的意思:

1在下面的第一个屏幕截图中,您可以看到给定的姓名和姓氏作为用户bob的声明。这是使用MVC razor中的以下代码片段显示的:


终于发现我错过了什么。客户端应用程序负责在连接openid中间件时设置OpenIdConnect选项。期权对象上的一个属性称为索赔。这些声明操作允许客户端连接自定义映射属性。以下是ClaimsActions.MapJsonKey属性/方法组合的文档:

下面是我添加到我的客户机启动类中的代码,用于自动序列化的一组声明

services.AddAuthentication(options =>
{
   options.DefaultScheme = "Cookies";
   options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{                    
    options.SignInScheme = "Cookies";

    options.Authority = "http://localhost:5000";
    options.RequireHttpsMetadata = false;

    options.ClientId = "mvc";
    options.ClientSecret = "secret";
    options.ResponseType = "code id_token";

    options.SaveTokens = true;
    options.GetClaimsFromUserInfoEndpoint = true;

    options.Scope.Add("athlete.full");
    options.Scope.Add("rights");
    options.Scope.Add("email");
    options.Scope.Add("address");
    options.Scope.Add("phone");

    options.Scope.Add("offline_access");

    // These are what allowed the claims to be serialized for front-end consumption.
    options.ClaimActions.MapJsonKey(JwtClaimTypes.WebSite, "website");
    options.ClaimActions.MapJsonKey(JwtClaimTypes.Gender, "gender");
    options.ClaimActions.MapJsonKey(JwtClaimTypes.BirthDate, "birthdate");
});

我面临着与您类似的问题,但我有AddIdentityServerAuthentication而不是AddOpenIdConnect,您能告诉我如何添加Claimerations.MapJsonKey吗。AddIdentityServerAuthentication选项似乎没有索赔方法
context.AddRequestedClaims(claims);
services.AddAuthentication(options =>
{
   options.DefaultScheme = "Cookies";
   options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{                    
    options.SignInScheme = "Cookies";

    options.Authority = "http://localhost:5000";
    options.RequireHttpsMetadata = false;

    options.ClientId = "mvc";
    options.ClientSecret = "secret";
    options.ResponseType = "code id_token";

    options.SaveTokens = true;
    options.GetClaimsFromUserInfoEndpoint = true;

    options.Scope.Add("athlete.full");
    options.Scope.Add("rights");
    options.Scope.Add("email");
    options.Scope.Add("address");
    options.Scope.Add("phone");

    options.Scope.Add("offline_access");

    // These are what allowed the claims to be serialized for front-end consumption.
    options.ClaimActions.MapJsonKey(JwtClaimTypes.WebSite, "website");
    options.ClaimActions.MapJsonKey(JwtClaimTypes.Gender, "gender");
    options.ClaimActions.MapJsonKey(JwtClaimTypes.BirthDate, "birthdate");
});