C# dotnetcore 3 web api外部登录google GetExternalLoginFoAsync大多数情况下返回null

C# dotnetcore 3 web api外部登录google GetExternalLoginFoAsync大多数情况下返回null,c#,.net-core,google-oauth,asp.net-core-webapi,google-authentication,C#,.net Core,Google Oauth,Asp.net Core Webapi,Google Authentication,我在网上尝试了几乎所有可能的解决方案,但仍然无法找到有效的解决方案。我使用GoogleAuthentication使用dotnetcore 3 web API进行POC 我已经将console.google中的returnUrl配置为localhost:5432/Account/ExternalLoginCallback 每当我尝试点击API时,它都会点击ExternalLogin方法,然后将我重定向到google登录页面,当我登录时,我再次被重定向到ExternalLoginCallback方

我在网上尝试了几乎所有可能的解决方案,但仍然无法找到有效的解决方案。我使用GoogleAuthentication使用dotnetcore 3 web API进行POC

我已经将console.google中的returnUrl配置为localhost:5432/Account/ExternalLoginCallback

每当我尝试点击API时,它都会点击ExternalLogin方法,然后将我重定向到google登录页面,当我登录时,我再次被重定向到ExternalLoginCallback方法,但signInManager.GetExternalLoginInfoAsync()大多返回空值

最令人惊讶的是,在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");

            });
            ...
[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");
        }
       .....

    }
public void配置服务(IServiceCollection服务)
{
...
services.AddAuthentication().AddCookie().AddGoogle(选项=>
{
options.ClientId=“”;
options.ClientSecret=“”;
options.CallbackPath=新路径字符串(“/Account/ExternalLoginCallback”);
});
...
AccountController有两种方法,如下所示

    public void ConfigureServices(IServiceCollection services)
        {
            ...

            services.AddAuthentication().AddCookie().AddGoogle(options =>
            {
                options.ClientId = "<id>";
                options.ClientSecret = "<secret>";
                options.CallbackPath = new PathString("/Account/ExternalLoginCallback");

            });
            ...
[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");
        }
       .....

    }
[HttpGet(“外部登录”)]
public IActionResult ExternalLogin(字符串提供程序,字符串返回URL=“/”)
{
var redirectUrl=Url.Action(“ExternalLoginCallback”、“Account”,
新的{ReturnUrl=ReturnUrl});
var properties=signInManager.ConfigureExternalAuthenticationProperties(提供程序,重定向URL);
返回新的ChallengeResult(提供程序、属性);
}
[HttpGet(“ExternalLoginCallback”)]
公共异步任务ExternalLoginCallback(string returnUrl=null,string remoteError=null)
{
ExternalLoginInfo info=await signInManager.GetExternalLoginInfoAsync();
if(info==null)
{
返回内容(“无法加载用户信息”);
}
.....
}
ASP.NET Core 2.x 对于Microsoft.AspNetCore.Authentication.Google 2.x,缓解措施是将您现有的在启动时添加Google的呼叫替换为:

.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已达到使用期限,因此未计划对其进行修补

ASP.NET Core 3.0预览版 为2.x提供的缓解措施也可用于当前的3.0预览。在未来的3.0预览中,我们将考虑删除Microsoft.AspNetCore.Authentication.Google包,并将用户定向到Microsoft.AspNetCore.Authentication.OpenIdConnect。我们将跟进最终计划。以下是如何用AddOpenIdC替换AddGoogle启动时连接。此替换可与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();

你找到解决这个问题的办法了吗?我们有同样的问题。我也有同样的问题。我很困惑。