C# IdentityServer4-谷歌丢失的索赔

C# IdentityServer4-谷歌丢失的索赔,c#,asp.net-core,google-oauth,identityserver4,C#,Asp.net Core,Google Oauth,Identityserver4,TLDR在使用IdentityServer4的上下文中 你如何从谷歌获得电子邮件地址和高清声明 如何填充User.Identity.Name 我使用过IdentityServer,有一个正在工作的MVC客户端与IdentityServer实例对话(如果使用了错误的术语,我深表歉意)。我使用的是外部身份验证(Google),没有任何稍微复杂的东西,如本地登录/数据库等。我没有使用ASP.NET身份验证。这一切都很好 我可以在我的MVC应用程序中成功进行身份验证,以下代码在下面的屏幕截图中生成声明

TLDR在使用IdentityServer4的上下文中

  • 你如何从谷歌获得电子邮件地址和高清声明
  • 如何填充User.Identity.Name
    我使用过IdentityServer,有一个正在工作的MVC客户端与IdentityServer实例对话(如果使用了错误的术语,我深表歉意)。我使用的是外部身份验证(Google),没有任何稍微复杂的东西,如本地登录/数据库等。我没有使用ASP.NET身份验证。这一切都很好

    我可以在我的MVC应用程序中成功进行身份验证,以下代码在下面的屏幕截图中生成声明:

    @foreach (var claim in User.Claims)
    {
        <dt>@claim.Type</dt>
        <dd>@claim.Value</dd>
    }
    <dt>Identity.Name</dt>
    <dd>&nbsp;@User.Identity.Name</dd>
    
    <dt>IsAuthenticated</dt>
    <dd>@User.Identity.IsAuthenticated</dd>
    
    身份服务器 客户定义

    //OpenID连接隐式流客户端(MVC)
    新客户
    {
    ClientId=“ctda web”,
    ClientName=“公司做网络应用”,
    AllowedGrantTypes=GrantTypes.Implicit,
    EnableLocalLogin=false,
    //登录后重定向到哪里
    重定向URI={”http://localhost:53996/signin-oidc“},
    //注销后重定向到何处
    PostLogoutRedirectUris={”http://localhost:53996/signout-回调oidc“},
    AllowedScopes=新列表
    {
    IdentityServerConstants.StandardScopes.OpenId,
    IdentityServerConstants.StandardScopes.Profile,
    IdentityServerConstants.StandardScopes.Email,
    “域”
    }
    }
    
    标识资源定义

    返回新列表
    {
    新标识资源.OpenId(),
    新标识资源.Profile(),
    新标识资源.Email(),
    新身份资源
    {
    Name=“域”,
    DisplayName=“谷歌组织”,
    Description=“用户的托管G套件域,如果是其中的一部分”,
    UserClaims=新列表{“hd”}
    } 
    };
    
    我可以告诉您如何让电子邮件返回

    有两种方法可以做到这一点,但它们都要求您将电子邮件范围添加到初始请求中。仅仅发送openId是行不通的

    Openid电子邮件

    用户信息请求

    现在,当您获得访问令牌后,您可以执行以下操作

    https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token={access token}
    
    回应

    {
      "family_name": "Lawton", 
      "name": "Linda Lawton", 
      "picture": "https://lh5.googleusercontent.com/-a1CWlFnA5xE/AAAAAAAAAAI/AAAAAAAAl1I/UcwPajZOuN4/photo.jpg", 
      "gender": "female", 
      "email": "xxxx@gmail.com", 
      "link": "https://plus.google.com/+LindaLawton", 
      "given_name": "Linda", 
      "id": "117200475532672775346", 
      "verified_email": true
    }
    
    令牌信息请求:

    使用id令牌

     https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={token id}
    
    回应

    {
     "azp": "07408718192.apps.googleusercontent.com",
     "aud": "07408718192.apps.googleusercontent.com",
     "sub": "00475532672775346",
     "email": "XX@gmail.com",
     "email_verified": "true",
     "at_hash": "8ON2HwraMXbPpP0Nwle8Kw",
     "iss": "https://accounts.google.com",
     "iat": "1509967160",
     "exp": "1509970760",
     "alg": "RS256",
     "kid": "d4ed62ee21d157e8a237b7db3cbd8f7aafab2e"
    }
    

    至于如何填充您的控制器,我无能为力。

    答案非常明显:只要您具有以下配置(在Identity Server Startup.cs中),Identity Server 4提供的示例代码就可以工作:

    services.AddIdentityServer()
    .AddDeveloperSigningCredential()
    .AddInMemoryApiResources(Config.GetApiResources())
    .AddInMemoryIdentityResources(Config.GetIdentityResources())
    .AddInMemoryClients(Config.GetClients())
    
    .AddTestUsers(Config.GetUsers());//感谢您回答DalmTo.Dumb问题:行“options.Scope.Add(“email”);”没有按照您的建议添加电子邮件范围?或者,一个更好的问题是:您能帮助我理解我的代码示例中的内容与您建议的内容之间的差异吗?您正在请求电子邮件,但尚未调用userinfo endpoint。据我所知,Google不会将电子邮件作为ac中嵌入的声明的一部分返回cess令牌。您需要向端点发出请求以获取此信息
    {
      "family_name": "Lawton", 
      "name": "Linda Lawton", 
      "picture": "https://lh5.googleusercontent.com/-a1CWlFnA5xE/AAAAAAAAAAI/AAAAAAAAl1I/UcwPajZOuN4/photo.jpg", 
      "gender": "female", 
      "email": "xxxx@gmail.com", 
      "link": "https://plus.google.com/+LindaLawton", 
      "given_name": "Linda", 
      "id": "117200475532672775346", 
      "verified_email": true
    }
    
     https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={token id}
    
    {
     "azp": "07408718192.apps.googleusercontent.com",
     "aud": "07408718192.apps.googleusercontent.com",
     "sub": "00475532672775346",
     "email": "XX@gmail.com",
     "email_verified": "true",
     "at_hash": "8ON2HwraMXbPpP0Nwle8Kw",
     "iss": "https://accounts.google.com",
     "iat": "1509967160",
     "exp": "1509970760",
     "alg": "RS256",
     "kid": "d4ed62ee21d157e8a237b7db3cbd8f7aafab2e"
    }
    
    services.AddIdentityServer()
        .AddDeveloperSigningCredential()
        .AddInMemoryApiResources(Config.GetApiResources())
        .AddInMemoryIdentityResources(Config.GetIdentityResources())
        .AddInMemoryClients(Config.GetClients()
        .AddTestUsers(Config.GetUsers()); //<--- this line here
    
    services.AddSingleton(new InMemoryUserStore()); //<-- new
    
    services.AddIdentityServer()
        .AddDeveloperSigningCredential()
        .AddInMemoryApiResources(Config.GetApiResources())
        .AddInMemoryIdentityResources(Config.GetIdentityResources())
        .AddInMemoryClients(Config.GetClients())
        .AddProfileService<UserProfileService>(); //<-- new