asp.net Google身份验证关联失败,(未知位置)

wnvonmuf  于 2022-11-26  发布在  .NET
关注(0)|答案(3)|浏览(150)

我想使用此配置将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)
                {}
    
    //..
}

点击“登录谷歌”后,登录到谷歌就可以了!
但是在返回google到“signin-google”时,我遇到了问题:

bttbmeg0

bttbmeg01#

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

s2j5cfk02#

通过删除CookiePolicy中间件解决。
此错误的原因是:
找不到“.AspNetCore.相互相关.XXXXX”Cookie。

选项。Cookie。SameSite = SameSiteMode。未指定;
由CookiePolicy中间件覆盖。

ebdffaop

ebdffaop3#

我遇到了这个问题,我发现我在用户表中添加了一些列,这些列不能为空,并且会阻止用户注册用户

相关问题