我正在努力使ASP.Net MVC 5谷歌OAuth2身份验证工作正常。
当我设置在没有任何作用域的GoogleOauth2AuthenticationOptions中传递时,我就能够成功地登录。
var googlePlusOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = googleClientId,
ClientSecret = googleClientSecret,
SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
Provider = new GoogleOAuth2AuthenticationProvider()
{
OnAuthenticated = async ctx =>
{
ctx.Identity.AddClaim(new Claim("urn:tokens:googleplus:accesstoken", ctx.AccessToken));
}
},
};
app.UseGoogleAuthentication(googlePlusOptions);然后,此调用将返回具有所有属性集的ExternalLoginInfo对象。
ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();但是,当我添加任何作用域时,就不会返回任何登录信息。都是空的。
var googlePlusOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = googleClientId,
ClientSecret = googleClientSecret,
SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
Provider = new GoogleOAuth2AuthenticationProvider()
{
OnAuthenticated = async ctx =>
{
ctx.Identity.AddClaim(new Claim("urn:tokens:googleplus:accesstoken", ctx.AccessToken));
}
},
};
googlePlusOptions.Scope.Add(YouTubeService.Scope.Youtube);
app.UseGoogleAuthentication(googlePlusOptions);然后调用获取外部信息--只需--返回空。
ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();在Google控制台中,我打开了以下API。
有关向选项添加范围的内容破坏了GetExternalLoginInfoAsync。
发布于 2014-12-18 15:09:23
如果有人在使用最新的Microsoft中间件(3.0.0+)时仍有问题.
我从Fiddler那里注意到,默认情况下,将向accounts.google.com发送以下作用域:
scope=openid%20profile%20email如果通过GoogleOAuth2AuthenticationOptions.Scope.Add(...),添加您自己的作用域,则范围将变为:
scope=YOUR_SCOPES_ONLY因此,您也需要添加默认的作用域(或者至少解决了这个问题):
var googlePlusOptions = new GoogleOAuth2AuthenticationOptions {
...
};
// default scopes
googlePlusOptions.Scope.Add("openid");
googlePlusOptions.Scope.Add("profile");
googlePlusOptions.Scope.Add("email");
// additional scope(s)
googlePlusOptions.Scope.Add("https://www.googleapis.com/auth/youtube.readonly");发布于 2014-03-27 18:31:00
因此,在http://www.beabigrockstar.com/blog/google-oauth-sign-asp-net-identity的大量帮助下,我发现了这一点。事实证明,内置于Google认证提供商的MVC仅仅是openId。这就是为什么增加一个范围破坏了它。使用Fiddler,我能够看到对accounts.google.com的GET请求,其中在查询字符串中包含了"scope=openid“。
通过切换到上面链接中的GooglePlusOAuth2提供者,或者在Nuget https://www.nuget.org/packages/Owin.Security.GooglePlus上,并使用提供者名称"GooglePlus",我成功地添加了作用域,并且仍然从GetExternalLoginInfoAsync获得了登录信息。
发布于 2014-09-09 12:41:51
谷歌对他们的auth机制所做的改变是Microsoft中间件的反映在3.0.0版中。正如您正确识别的那样,其中一个更改是将OAuth端点移动到Google+ (https://www.googleapis.com/plus/v1/people/me)。
因此,关键是:
https://stackoverflow.com/questions/22690949
复制相似问题