如何在ASP.NET Boilerplate中设置社交登录?

我正在尝试通过Googlevalidation用户身份。 我正在使用带有Vue的ASP.NET Core的ABP启动模板。

这是我到目前为止所做的:

我在GoogleAuthProviderApi中创建了一个GoogleAuthProviderApi:

 using System; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.Google; using Newtonsoft.Json.Linq; namespace Mindbus.MindbooksSEO.Authentication.External.Google { public class GoogleAuthProviderApi : ExternalAuthProviderApiBase { public const string Name = "Google"; public override async Task GetUserInfo(string accessCode) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.UserAgent.ParseAdd("Microsoft ASP.NET Core OAuth middleware"); client.DefaultRequestHeaders.Accept.ParseAdd("application/json"); client.Timeout = TimeSpan.FromSeconds(30); client.MaxResponseContentBufferSize = 1024 * 1024 * 10; // 10 MB var request = new HttpRequestMessage(HttpMethod.Get, GoogleDefaults.UserInformationEndpoint); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode); var response = await client.SendAsync(request); response.EnsureSuccessStatusCode(); var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); return new ExternalAuthUserInfo { //Name = GoogleHelper.GetName(payload), EmailAddress = GoogleHelper.GetEmail(payload), //Surname = GoogleHelper.GetFamilyName(payload), //ProviderKey = GoogleHelper.GetId(payload), Provider = Name }; } } } } 

我在Web.Host的AuthConfigurer.cs中注册了Google外部身份validation:

 if (bool.Parse(configuration["Authentication:Google:IsEnabled"])) { services.AddAuthentication().AddGoogle(googleOptions => { googleOptions.ClientId = configuration["Authentication:Google:ClientId"]; googleOptions.ClientSecret = configuration["Authentication:Google:ClientSecret"]; }); } 

我已将设置添加到Web.Host中的appsettings.json,并在Secret Manager工具中创建了相应的机密( ClientIdClientSecret )。

我已经使用RequireHttpsAttribute强制API进行SSL。

我在[ProjectName] WebCoreModule.cs中注册了GoogleAuthProviderApi

 public override void PreInitialize() { Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString( MindbooksSEOConsts.ConnectionStringName ); // Use database for language management Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization(); Configuration.Modules.AbpAspNetCore() .CreateControllersForAppServices( typeof(MindbooksSEOApplicationModule).GetAssembly() ); ConfigureTokenAuth(); Configuration.Modules.Zero().UserManagement.ExternalAuthenticationSources.Add(); } 

我不知道我在这里缺少什么,也不知道到底发生了什么。

原本以为调用api / TokenAuth / GetExternalAuthenticationProviders端点至少会给我一个包含Google的列表,但是这个请求在结果中返回一个空数组。

此外,对于我来说,这种外部身份validation的范围有点不清楚,例如Google和Facebook等OAuth提供商。 在我看来,您要么使用OAuth进行服务器端使用,在这种情况下,我不明白为什么要通过API公开部分内容。 或者您有用于JavaScript Web应用程序的OAuth,在这种情况下,您不需要在自己的服务器上使用API​​端点,只需通过Web应用程序处理整个客户端。

那么,External Authenticate API端点的确切目的是什么? 是否您自己的服务器充当身份validation的代理? 那么您可以同时使用外部(Google)API的客户端和服务器端使用?

更新1

评论要求我补充一些说明。

#1 :如果我在Postman中添加Abp.TenantId标头,响应将保持不变:

 GET /api/TokenAuth/GetExternalAuthenticationProviders HTTP/1.1 Host: localhost:44300 Accept: application/json Abp.TenantId: 2 Cache-Control: no-cache Postman-Token: 0cb72e57-4b9a-474d-b60d-492fa727a7a2 

#2 :Swagger中的控制台“欺骗”导致错误:

 abp.swagger.login() undefined VM40:49 POST https://localhost:44300/api/TokenAuth/Authenticate 500 () abp.swagger.login @ VM40:49 (anonymous) @ VM84:1 abp.swagger.addAuthToken() false 

更新2

我认为GoogleAuthProviderApi 。 在我对所有CLRexception进行调试器中断后,我发现了以下错误:

 'Mindbus.MindbooksSEO.Authentication.External.Google.GoogleAuthProviderApi' to type 'Abp.Authorization.Users.IExternalAuthenticationSource`2 [Mindbus.MindbooksSEO.MultiTenancy.Tenant, Mindbus.MindbooksSEO.Authorization.Users.User]'.' 

ASP.NET Core 1.x或MVC 5

  1. 请注意, 社交登录 提供程序 (例如Google)的配置与外部身份validation (例如LDAP)完全不同。 所以,删除这一行:

    Configuration.Modules.Zero().UserManagement.ExternalAuthenticationSources.Add();

  2. 观察GetExternalAuthenticationProvidersIExternalAuthConfiguration查找。
    因此,在*WebHostModule PostInitialize方法中配置IExternalAuthConfiguration

     if (bool.Parse(configuration["Authentication:Google:IsEnabled"])) { var externalAuthConfiguration = IocManager.Resolve(); externalAuthConfiguration.Providers.Add( new ExternalLoginProviderInfo( GoogleAuthProviderApi.Name, configuration["Authentication:Google:ClientId"], configuration["Authentication:Google:ClientSecret"], typeof(GoogleAuthProviderApi) ) ); } 

ASP.NET Core 2.x

虽然上述处理社交登录提供程序的方式可能有效,但不再推荐使用 。

内置的.AddGoogle方式:

 if (bool.Parse(configuration["Authentication:Google:IsEnabled"])) { services.AddAuthentication().AddGoogle(googleOptions => { googleOptions.ClientId = configuration["Authentication:Google:ClientId"]; googleOptions.ClientSecret = configuration["Authentication:Google:ClientSecret"]; }); } 

……用于:

 var result = await _signInManager.ExternalLoginSignInAsync( info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor : true ); 

获得外部身份validation方案的方法是:

 var schemes = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); 

您可以修改GetExternalAuthenticationProviders以返回此数据。