问题描述
我正在尝试在MVC 5网络应用中实施Google身份验证.身份验证工作正常,但我会检索个人资料和图片信息.
I'm trying to implement a Google authentication in a MVC 5 web app. Authentication is working fine, but I would retrieve profile and picture informations.
为此,我添加了一个GoogleOAuth2AuthenticationOptions对象以指定其他声明:
To do this, I added a GoogleOAuth2AuthenticationOptions object to specify additional claims :
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:
But it causes that a wrong URL is generated :
没有'?'在参数之前,这会导致redirect_uri_mismatch.
There is no '?' before parameters, that's cause a redirect_uri_mismatch.
但是,当我简单使用时:
However, when I use simply :
app.UseGoogleAuthentication(
clientId : "xxxxxxxxxxxxxxxxxxx",
clientSecret : "xxxxxxxxxxxxxxxxx");
正在工作.
有什么主意吗?
推荐答案
仅使用这一点.
var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = "MYCLIENTID",
ClientSecret = "MYSECRET",
};
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
该方法似乎会自动在地址中使用signin-google请求.要解决此更改,请在Google控制台中更改Google回调位置,使其指向该地址.
This method seems to automatically use the signin-google request in the address.To fix this change google callback location in the google console to point to this address.
添加 RouteConfig文件
routes.MapRoute( name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "LoginCallback" } );
这篇关于使用GoogleOAuth2AuthenticationOptions时遇到了redirect_uri_mismatch错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!