我試圖更改IdentityServer4 AspNetIdentity示例以便能夠從本地創建的用戶和Google登錄。當通過Google OAuth登錄時使用IProfileService
我能夠加入谷歌認證要做到這一點:
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"
});
正如預期的那樣在主頁視圖顯示正確的用戶聲明:
sub
c51da331-0348-45dd-352f-08d4526f6266
name
[email protected]
AspNet.Identity.SecurityStamp
568a167f-a431-4f70-ba66-918f99e95eef
idp
Google
amr
external
auth_time
1486815555
在使用谷歌賬戶在首次用戶登錄我向數據庫添加了一些信息,我想我可以通過使用自定義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>();
但現在,當我導航到主頁時,用戶聲明保持不變,甚至GetProfileDataAsync方法也不會發生。
我很感激有人能告訴我這是什麼感覺。