无法通过google oauth2.0授权用户

时间:2020-03-22 11:52:46

标签: asp.net-core asp.net-identity google-oauth

我正在尝试通过以下代码设置google外部身份验证:

services.AddAuthentication()
            .AddGoogle(opts =>
            {
                opts.Events = new Microsoft.AspNetCore.Authentication.OAuth.OAuthEvents()
                {
                    OnRemoteFailure = (ex) =>
                    {
                        //error handlin
                        return Task.CompletedTask;
                    }
                };
                opts.ClientId = config["Authentification:Google:ClientId"];
                opts.ClientSecret = config["Authentification:Google:ClientSecret"];
                opts.CallbackPath = new PathString("/Authentification/GooglePostRedirect");
            });

我的控制器代码如下:

public class AuthentificationController : Controller
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly SignInManager<ApplicationUser> _signInManager;

    public AuthentificationController(
        UserManager<ApplicationUser> userManager,
        SignInManager<ApplicationUser> signInManager)
    {
        _userManager = userManager;
        _signInManager = signInManager;
    }

    [HttpGet]
    public IActionResult GoogleLogin()
    {
        var redirectUrl = Url.Action(nameof(GooglePostRedirect), "Authentification");
        var properties = _signInManager.ConfigureExternalAuthenticationProperties("Google", redirectUrl);

        return new ChallengeResult("Google", properties);
    }

    [AllowAnonymous]
    public async Task<IActionResult> GooglePostRedirect()
    {
        var info = await _signInManager.GetExternalLoginInfoAsync();

        return View();
    }
}

在Google网站上登录后,我被重定向回到了设置回调的位置,但它不是读取操作方法,而是给我以下错误: Error 我试图在.AddGoogle()中的回调和控制器中的操作中使用不同的url,但是没有任何帮助。到目前为止,有没有人经历过? 预先感谢

0 个答案:

没有答案