【问题标题】:Google Authentication Correlation failed.(Unknown location)Google 身份验证关联失败。(位置未知)
【发布时间】:2021-03-22 10:39:42
【问题描述】:

我想使用以下配置将 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”后登录谷歌就OK了!

但是在返回 google 到“signin-google”时我有问题:

【问题讨论】:

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


    【解决方案1】:

    不是您问题的实际答案,而是查看您的代码的额外提示。您可以添加查询字符串参数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
            }
        }
    

    【讨论】:

    • 谢谢你有解决问题的想法吗?
    • 偶尔我也面临同样的问题。我不记得我是如何解决它的,但请尝试在不同的浏览器上运行您的程序。
    • 我使用 Firefox 和 chrome 进行测试。两者都有相同的错误
    • 这个问题背后可能有很多原因。我建议您使用我的代码 sn-p,并在您的配置中仅保留 ClientIdClientSecret 选项。您最近是否修改过操作系统中的日期和时间设置?
    【解决方案2】:

    通过删除 CookiePolicy 中间件解决。

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

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

    options.Cookie.SameSite = SameSiteMode.Unspecified;

    被 CookiePolicy 中间件覆盖。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 2018-10-11
      相关资源
      最近更新 更多