Asp.net mvc 使用GoogleOAuth2AuthenticationOptions时出现重定向\u uri\u不匹配错误

Asp.net mvc 使用GoogleOAuth2AuthenticationOptions时出现重定向\u uri\u不匹配错误,asp.net-mvc,google-authentication,Asp.net Mvc,Google Authentication,我正在尝试在MVC5 web应用程序中实现Google身份验证。身份验证工作正常,但我会检索个人资料和图片信息 为此,我添加了一个GoogleOAuth2AuthenticationOptions对象来指定其他声明: var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions { ClientId = "xxxxxxxxxxxxxxxx", ClientSecret = "xxxxxx

我正在尝试在MVC5 web应用程序中实现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:

参数前没有“?”,这会导致重定向uri不匹配

然而,当我简单地使用:

app.UseGoogleAuthentication(
    clientId : "xxxxxxxxxxxxxxxxxxx",
    clientSecret : "xxxxxxxxxxxxxxxxx");
它起作用了


有什么想法吗?

只使用这么多

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
            {
                ClientId = "MYCLIENTID",
                ClientSecret = "MYSECRET",
            };
    app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
此方法似乎自动使用地址中的登录google请求。若要修复此问题,请更改google控制台中的google回调位置以指向此地址

添加路由配置文件

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

使用下面这个工作正常的代码片段,只需替换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如下所示

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

只需在末尾的URI中添加[登录google]

最后保存它


非常感谢,我得到了我想要的,感谢你的回调url,对于配置文件图片,我只是在使用这里提到的解决方案:MVC5中不需要此路由,只需要在google end上添加条目,如@Saines所示。。。