c# - dotnetcore 3 web api外部登录与谷歌GetExternalLoginInfoAsync大多数时候返回null
问题描述
我已经尝试了几乎所有可能的在线解决方案,但仍然无法获得有效的解决方案。我使用 GoogleAuthentication 使用 dotnetcore 3 Web API 进行 POC。
我已将 console.google 中的 returnUrl 配置为 localhost:5432/Account/ExternalLoginCallback
每当我尝试在http://localhost:5432/account/externallogin?provider=Google访问 API 时,它会访问 ExternalLogin 方法,然后将我重定向到 google 登录页面,当我登录时,我再次被重定向到ExternalLoginCallback 方法,但 signInManager.GetExternalLoginInfoAsync() 大多返回 null。
最令人惊讶的是,在 100 次尝试中,有 2 次、3 次尝试实际上是从 signInManager.GetExternalLoginInfoAsync() 获取数据。
我完全糊涂了,你能帮帮我吗
startup.cs 看起来像这样
public void ConfigureServices(IServiceCollection services)
{
...
services.AddAuthentication().AddCookie().AddGoogle(options =>
{
options.ClientId = "<id>";
options.ClientSecret = "<secret>";
options.CallbackPath = new PathString("/Account/ExternalLoginCallback");
});
...
AccountController 有两个方法,看起来像这样
[HttpGet("ExternalLogin")]
public IActionResult ExternalLogin(string provider, string returnUrl="/")
{
var redirectUrl = Url.Action("ExternalLoginCallback", "Account",
new { ReturnUrl = returnUrl });
var properties = signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return new ChallengeResult(provider, properties);
}
[HttpGet("ExternalLoginCallback")]
public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
{
ExternalLoginInfo info = await signInManager.GetExternalLoginInfoAsync();
if (info == null)
{
return Content("Could not load user info");
}
.....
}
解决方案
基于 Google+ 的身份验证弃用和替换
ASP.NET 核心 2.x
对于 Microsoft.AspNetCore.Authentication.Google 2.x,缓解方法是将您在 Startup 中对 AddGoogle 的现有调用替换为:
.AddGoogle(o =>
{
o.ClientId = Configuration["Authentication:Google:ClientId"];
o.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
o.UserInformationEndpoint = "https://www.googleapis.com/oauth2/v2/userinfo";
o.ClaimActions.Clear();
o.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
o.ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
o.ClaimActions.MapJsonKey(ClaimTypes.GivenName, "given_name");
o.ClaimActions.MapJsonKey(ClaimTypes.Surname, "family_name");
o.ClaimActions.MapJsonKey("urn:google:profile", "link");
o.ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
});
应用程序应立即使用缓解措施进行测试,以检查数据格式的变化。预计 2 月 2.1 和 2.2 补丁中将包含对此的修复,将上述重新配置合并为新的默认值。由于 2.0 已达到生命周期,因此没有计划为 2.0 提供补丁。
ASP.NET Core 3.0 预览版
为 2.x 提供的缓解措施也可用于当前的 3.0 预览版。在未来的 3.0 预览版中,我们正在考虑删除 Microsoft.AspNetCore.Authentication.Google 包并将用户引导至 Microsoft.AspNetCore.Authentication.OpenIdConnect。我们将跟进最终计划。下面介绍如何在 Startup 中将 AddGoogle 替换为 AddOpenIdConnect。此替换可用于 ASP.NET Core 2.0 及更高版本,并可根据需要适用于 1.x。
.AddOpenIdConnect("Google", o =>
{
o.ClientId = Configuration["Authentication:Google:ClientId"];
o.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
o.Authority = "https://accounts.google.com";
o.ResponseType = OpenIdConnectResponseType.Code;
o.CallbackPath = "/signin-google"; // Or register the default "/sigin-oidc"
o.Scope.Add("email");
});
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();