【问题标题】:using GoogleOAuth2AuthenticationOptions got a redirect_uri_mismatch error使用 GoogleOAuth2AuthenticationOptions 得到 redirect_uri_mismatch 错误
【发布时间】:2014-11-16 21:24:52
【问题描述】:

我正在尝试在 MVC 5 网络应用程序中实现 Google 身份验证。身份验证工作正常,但我会检索个人资料和图片信息。

为此,我添加了一个 GoogleOAuth2AuthenticationOptions 对象来指定其他声明:

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
    ClientId = "xxxxxxxxxxxxxxxx",
    ClientSecret = "xxxxxxxxxxxxxxxxx",
    CallbackPath = new PathString("/Account/LoginCallback"),
    Provider = new GoogleOAuth2AuthenticationProvider()
    {
        OnAuthenticated = async context =>
        {
            context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString()));
            context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString()));
        }
    }
};

app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);

但是会导致生成错误的URL:

http://admin.localhost.com/Account/LoginCallback&state=Gs-otJmI79bgWA3_qJzDcGziWnkRCOf7JRoCUDCIz0jv4IIvDdoZlZzVSq2kZxfaPFDmv9hbZGp5q1Aq8mpLPguKnCF31twHj8NQCMv_NrgZzvKwaelmZr_HwY_bdj8h1ICFrkGTKLJ1saEYDbFJ2CJxvDkyBL2iygQmTXQTs-aUiL4yWe5_7dZQOjP_mDUSW-GXns3wr7Okwkoj8VEITJTUz9nAbrBd_N_7puTMlHU&client_id=xxxxxxxxxxxxxxxx

没有'?'在参数之前,这会导致 redirect_uri_mismatch。

但是,当我简单地使用时:

app.UseGoogleAuthentication(
    clientId : "xxxxxxxxxxxxxxxxxxx",
    clientSecret : "xxxxxxxxxxxxxxxxx");

它正在工作。

有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc google-authentication


    【解决方案1】:

    只用这么多。

    var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
                {
                    ClientId = "MYCLIENTID",
                    ClientSecret = "MYSECRET",
                };
        app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
    

    此方法似乎会自动使用地址中的 signin-google 请求。要修复此更改 google 控制台中的 google 回调位置以指向此地址。

    添加RouteConfig文件

     routes.MapRoute( name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "LoginCallback" } ); 
    

    【讨论】:

    • 非常感谢,感谢您提供的回调 url,我得到了我想要的东西,对于个人资料图片,我只是使用这里提到的解决方案:stackoverflow.com/questions/22820349/…
    • 在MVC5中不需要这条路由只需要在google端添加条目如@Saines所示...
    【解决方案2】:

    使用下面的代码片段,它工作正常,只需替换 ClientID 和 ClientSecret 将为您工作。

         var googleOptions = new GoogleOAuth2AuthenticationOptions()
            {
                ClientId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
                ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
                Provider = new GoogleOAuth2AuthenticationProvider()
                {
                    OnAuthenticated = (context) =>
                    {
                        context.Identity.AddClaim(new Claim("urn:google:name", context.Identity.FindFirstValue(ClaimTypes.Name)));
                        context.Identity.AddClaim(new Claim("urn:google:email", context.Identity.FindFirstValue(ClaimTypes.Email)));
                        //This following line is need to retrieve the profile image
                        context.Identity.AddClaim(new System.Security.Claims.Claim("urn:google:accesstoken", context.AccessToken, ClaimValueTypes.String, "Google"));
    
                        return Task.FromResult(0);
                    }
                }
            };
    
            app.UseGoogleAuthentication(googleOptions);
    

    如果仍然存在错误

    假设你的应用程序 URI 如下所示

    http://localhost:2625/

    然后在console.developers.google.com 上您注册的URI 需要更改如下所示。

    只需在 URI 末尾添加 [signin-google]

    http://localhost:2625/signin-google

    最后保存它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 2018-03-21
      • 2021-06-22
      • 1970-01-01
      相关资源
      最近更新 更多