HttpRequestException:检索Google用户信息时发生错误(禁止)。在Asp.net Core

时间:2019-01-26 06:46:14

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

我正在尝试在asp.net核心应用程序中启用社交登录。我已经成功集成了Facebook和Twitter,并且工作正常。但是,在集成Google时,返回回调URL时,它会退出此错误页面。

我还在过程中使用自定义控制器

[Route("signin/{provider}")]
public IActionResult SignIn(string provider, string returnUrl = null) => 
        Challenge(new AuthenticationProperties { RedirectUri = returnUrl ?? 
          "/" }, provider);

上面的代码是我的自定义控制器调用auth.cs。

error page

services.AddAuthentication(options => {
     options.DefaultAuthenticateScheme = 
CookieAuthenticationDefaults.AuthenticationScheme;
     options.DefaultChallengeScheme = 
CookieAuthenticationDefaults.AuthenticationScheme;
     options.DefaultSignInScheme = 
CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddFacebook(options => 
          {
            options.AppId = "";
            options.AppSecret = "";
        })
.AddTwitter(options =>
        {
            options.ConsumerKey = "";
            options.ConsumerSecret = 
"";
})
.AddGoogle(options => {
            options.ClientId = 
"<my client Id>";
            options.ClientSecret = "<my client secret>";
})
.AddCookie(options => {
            options.LoginPath = "/auth/signin";
});

我希望一旦我使用googles Oauth 2 *登录,它便应重定向回我的应用程序,而不是显示错误页面

5 个答案:

答案 0 :(得分:1)

我遇到此错误是因为我没有从google developer console启用服务

所以当我启用它时,一切正常。

这是我在启用它之前遇到的错误。 My error page

答案 1 :(得分:1)

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins?view=aspnetcore-2.2

  

Blockquote   从2019年1月开始,谷歌开始关闭Google+登录,开发人员必须在3月之前迁移到新的Google登录系统。用于Google身份验证的ASP.NET Core 2.1和2.2程序包将在2月更新,以适应这些更改。有关ASP.NET Core的更多信息和临时缓解措施,请参见此GitHub问题。本教程已使用新的设置过程进行了更新。

答案 2 :(得分:0)

根据官方Quick Start,您是否尝试过IdentityServerConstants.ExternalCookieAuthenticationScheme;

    .AddGoogle("Google", options =>
    {
        options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;

        options.ClientId = "<insert here>";
        options.ClientSecret = "<insert here>";
    })

答案 3 :(得分:0)

尝试一下:

.AddGoogle("Google", o=>
{
    o.ClientId = "<insert here>";
    o.ClientSecret = "<insert here>";
    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");
})

答案 4 :(得分:0)

同一问题,我被困了大约两天。幸运的是,我深入研究发现了asp.net core 2 *及更高版本中的一些新内容,您需要提及UserInformationEndpoint。可从console.google.developer下载的json文件中找到该文件。
请在下面找到详细信息。

services.AddAuthentication()
        .AddGoogle(options =>
        {
            options.ClientId = "196275387351-has575ff9m45ka8ld17s0qtps42jkmtt.apps.googleusercontent.com";
            options.ClientSecret = "MoYpnVChG8VeDiGCw4XYrLH0";
            options.UserInformationEndpoint= "https://www.googleapis.com/oauth2/v1/certs";
          });

控制器:帐户

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);
}

已下载Json

"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"