我正在尝试让ASP.Net MVC 5 Google 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,}; googlePlusOptions.Scope.Add(YouTubeService.Scope.Youtube); app.UseGoogleAuthentication(googlePlusOptions);
ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
在Google开发控制台中,我启用了以下API.
> Analytics API
> BigQuery API
> Google Cloud sql
> Google云端存储
> Google云端存储JSON API
> Google API
> Google Domains API
> Identity Toolkit API
> YouTube Analytics API
> YouTube Data API v3
关于向选项添加范围的一些事情是打破GetExternalLoginInfoAsync.
解决方法
如果有人仍然与最新的微软有这个问题
OWIN中间件(3.0.0)……
OWIN中间件(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");