Google 身份验证关联失败。(位置未知)

时间:2021-03-22 10:39:42

标签: c# asp.net asp.net-core .net-core

我想使用此配置将 Google 身份验证添加到 Cookie 身份验证: 启动:

//ConfigureServices:
services.AddAuthentication( )
                    .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
                    {
                     //   ... 
                    })
                    .AddGoogle(options =>
                    {
                        IConfigurationSection googleAuthNSection =
                            Configuration.GetSection("ExternalLogin:Google");
                         
                        options.ClientId = googleAuthNSection["ClientId"];
                        options.ClientSecret = googleAuthNSection["ClientSecret"];  
                        options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                        options.CorrelationCookie.SameSite = SameSiteMode.Lax;
                    });

    //Configure():
         app.UseAuthentication();
         app.UseAuthorization();

[AllowAnonymous]
        public IActionResult SigninGoogle(string returnurl)
        {
            var authProperties = new AuthenticationProperties
            {   RedirectUri =   Url.Action("ExternalLoginCallback","Auth",new{returnurl}).ToString(),
                Items =
                {
                    { "LoginProvider", "Google" },
                },
                AllowRefresh = true,
            };
 
            return Challenge(authProperties, GoogleDefaults.AuthenticationScheme );
        }



             [AllowAnonymous][HttpGet("signin-google")]
            public async Task<IActionResult> ExternalLoginCallback(string returnurl, string remoteError = null)
            {
                var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                if (result.Succeeded)
                {}
    
    //..
}

点击“SigninGoogle”并登录谷歌后就可以了!

但在谷歌后面“登录谷歌”我有问题:

enter image description here

2 个答案:

答案 0 :(得分:0)

不是您问题的实际答案,而是查看您的代码的奖励提示。您可以添加查询字符串参数 provider。这样您的外部登录名就可以重复使用,例如,如果您决定稍后在您的程序中添加一个 facebook 登录名:

    [HttpPost]
    public IActionResult ExternalLogin(string returnUrl, string provider)
    {
        var properties = new AuthenticationProperties 
        { 
            RedirectUri = Url.Action(nameof(ExternalResponse), 
                                     new { returnUrl, provider }) 
        };

        return Challenge(properties, provider);
    }

    [HttpGet]
    public async Task<IActionResult> ExternalResponse(string returnUrl, string provider)
    {
        var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        if (result.Succeeded)
        {
            //succeeded
        }
        else
        {
           //failed
        }
    }

答案 1 :(得分:0)

通过删除 CookiePolicy 中间件解决。

此错误是由以下原因引起的:

<块引用>

".AspNetCore.Correlation.XXXXX' cookie 未找到。

<块引用>

options.Cookie.SameSite = SameSiteMode.Unspecified;

被 CookiePolicy 中间件覆盖。