Asp.net core 通过Google OAuth登录时使用IProfileService

Asp.net core 通过Google OAuth登录时使用IProfileService,asp.net-core,asp.net-core-mvc,identityserver4,asp.net-core-identity,Asp.net Core,Asp.net Core Mvc,Identityserver4,Asp.net Core Identity,我正在尝试更改IdentityServer4作为网络身份示例,以便能够从本地创建的用户和Google登录 我可以通过添加Google身份验证来做到这一点: app.UseIdentity(); app.UseIdentityServer(); var cookieScheme = app.ApplicationServices.GetRequiredService<IOptions<IdentityOptions>>().

我正在尝试更改IdentityServer4作为网络身份示例,以便能够从本地创建的用户和Google登录

我可以通过添加Google身份验证来做到这一点:

        app.UseIdentity();
        app.UseIdentityServer();

        var cookieScheme = app.ApplicationServices.GetRequiredService<IOptions<IdentityOptions>>().Value.Cookies.ExternalCookieAuthenticationScheme;

        // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
        app.UseGoogleAuthentication(new GoogleOptions
        {
            AuthenticationScheme = "Google",
            SignInScheme = cookieScheme,
            ClientId = "client_id",
            ClientSecret = "client_secret"
        });
当用户第一次使用Google帐户登录时,我向数据库中添加了一些信息,我认为可以通过使用自定义IProfileService实现并配置IdentityServer来使用自定义IProfileService将这些信息添加到用户声明中:

         var builder = services.AddIdentityServer();
         builder.AddTemporarySigningCredential();

        builder.AddConfigurationStore(b => b.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationAssembly)));
        builder.AddOperationalStore(b => b.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationAssembly)));
        builder.AddAspNetIdentity<MyUser>();
        builder.AddProfileService<MyCustomProfileService>();
var builder=services.AddIdentityServer();
builder.AddTemporarySigningCredential();
builder.AddConfigurationStore(b=>b.UseSqlServer(connectionString,options=>options.migrationAssembly(migrationAssembly));
builder.AddOperationalStore(b=>b.UseSqlServer(connectionString,options=>options.migrationAssembly(migrationAssembly));
builder.addAsNetIdentity();
AddProfileService();
但现在,当我导航到主页时,用户声明保持不变,甚至GetProfileDataAsync方法也不会命中


如果有人能告诉我这是什么样子,我将不胜感激

根据github中的讨论,只有在声明需要放入令牌时才会调用
GetProfileDataAsync
方法。它也有一个链接到帖子,解释说 默认情况下,IdentityServer具有与OpenID Connect规范相应的行为,该规范建议如下(在第5.4节中):

如第5.3.2节所述,当使用导致发出访问令牌的响应类型值时,将从UserInfo端点返回配置文件、电子邮件、地址和电话范围值请求的声明。但是,当没有发出访问令牌时(响应类型值id\u令牌就是这种情况),结果声明将在id令牌中返回

换句话说,如果只请求一个标识令牌,则将所有声明放入该令牌中。但是,如果还请求了访问令牌=>则从标识令牌中删除声明,并让客户端使用userinfo端点来检索它们


但是,可以通过在客户端配置(更多)上设置
AlwaysIncludeUserClaimsInIdToken
标志来覆盖此默认行为。

因此,根据github上的讨论,只有在需要将声明放入令牌时才调用
GetProfileDataAsync
方法。它也有一个链接到帖子,解释说 默认情况下,IdentityServer具有与OpenID Connect规范相应的行为,该规范建议如下(在第5.4节中):

如第5.3.2节所述,当使用导致发出访问令牌的响应类型值时,将从UserInfo端点返回配置文件、电子邮件、地址和电话范围值请求的声明。但是,当没有发出访问令牌时(响应类型值id\u令牌就是这种情况),结果声明将在id令牌中返回

换句话说,如果只请求一个标识令牌,则将所有声明放入该令牌中。但是,如果还请求了访问令牌=>则从标识令牌中删除声明,并让客户端使用userinfo端点来检索它们


但是,可以通过在客户端配置(更多)上设置
AlwaysIncludeUserClaimsInIdToken
标志来覆盖此默认行为。

事实证明,如果IdentityServer配置为使用ASP.NET标识库,则不会使用IProfileService添加自定义声明,除非(如所述)直接调用UserInfo端点。设计方法是使用ASP.NET标识机制,通过注册


在这种情况下,我们可以创建IUserClaimsPrincipalFactory的自定义实现,或者因为ASP.NET Identity将加载存储在数据库中的任何其他声明,使用类的方法添加任何额外声明。

事实证明,如果将IdentityServer配置为使用ASP.NET标识库,则它不会使用IProfileService添加自定义声明,除非(如所述)直接调用UserInfo端点。设计方法是使用ASP.NET标识机制,通过注册

在这种情况下,我们可以创建IUserClaimsPrincipalFactory的自定义实现,或者因为ASP.NET Identity将加载存储在数据库中的任何附加声明,所以可以使用类的方法添加任何附加声明

         var builder = services.AddIdentityServer();
         builder.AddTemporarySigningCredential();

        builder.AddConfigurationStore(b => b.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationAssembly)));
        builder.AddOperationalStore(b => b.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationAssembly)));
        builder.AddAspNetIdentity<MyUser>();
        builder.AddProfileService<MyCustomProfileService>();