OAuth2 通过 Google/Google+ 来自 ASP.NET MVC 5

OAuth2 via Google/Google+ from ASP.NET MVC 5

我正在开发一个 MVC 5 应用程序,我需要使用 Google 中的 oAuth2 进行身份验证。那里有很多教程(包括打字教程和一些视频),它们都显示了获得此设置的相同方法,但我就是无法让它们为我工作。那么让我从头说起。

我开始使用 Rick Anderson 的 great blog post 来了解如何获得此设置。该博客 post 有点过时,因此与 Google 的网站交互时的步骤略有不同,但除了不同的导航之外,所有重要信息都在那里,我能够跟着。这导致我启用 GooglePlus API 并设置以下客户端 ID 以使用:

快进到我的代码,我做了以下事情:

  1. 新建MVC应用(个人账号认证)
  2. 启用 HTTPS(目前使用 IISExpress,但我相信该证书能让浏览器满意)
  3. 这样配置我的 Startup.Auth.cs:

Startup.Auth.cs:

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
    {
        ClientId = GoogleClientId,
        ClientSecret = GoogleClientSecret
    });

此时,我能够让 Google 按钮显示在登录屏幕上:

当我单击它时,它会将我带到 Google 的 authentication/authorization 屏幕,我在该屏幕上授予我的应用程序访问我的 Google 帐户信息的权限。

我在这里单击 "Allow",遗憾的是,这就是问题所在。但有些事情也很顺利。此时,如果我在我的 Google 帐户下查看我的连接应用程序,我现在确实看到我的 MVC 应用程序出现了。所以 Google 的结局在大多数情况下看起来不错。但是当我检查请求时,会弹出一个红旗:

在与一些比我聪明的人交谈时(感谢加洛韦先生!),有人建议我听从 this blog post 的建议。长话短说,我做了以下更改:

  1. 将 Google API 的重定向 URI 配置为 /signin-googleplus
  2. 已安装的 nuget 包:Install-Package Owin.Security.GooglePlus
  3. 将我的 Startup.Auth.cs 修改为:

Startup.Auth.cs

app.UseGooglePlusAuthentication(new GooglePlusAuthenticationOptions
    {
        ClientId = GoogleClientId,
        ClientSecret = GoogleClientSecret
    });

结果是一样的:

在使用 Fiddler 深入研究这个拒绝访问错误时,我可以看出对 /signin-google 的请求的响应是 error=access_denied 最先出现的地方:

深入研究那个 403,我看到了这个回复:

HTTP/1.1 403 Forbidden
Vary: X-Origin
Content-Type: application/json; charset=UTF-8
Date: Tue, 14 Jun 2016 23:36:15 GMT
Expires: Tue, 14 Jun 2016 23:36:15 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic
Alt-Svc: quic=":443"; ma=2592000; v="34,33,32,31,30,29,28,27,26,25"
Accept-Ranges: none
Vary: Origin,Accept-Encoding
Content-Length: 213

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "userRateLimitExceeded",
    "message": "User Rate Limit Exceeded"
   }
  ],
  "code": 403,
  "message": "User Rate Limit Exceeded"
 }
}

为了以防万一,我还尝试了这些额外的东西:

我真的需要一些帮助让这个重定向从 Google 恢复正常!

I started off using Rick Anderson's great blog post on how to get this setup.

它对我很有效(我认为这是一个很好的解决方案),问题可能出在其他地方 - 请阅读下文。


In speaking with a few folks who are smarter than I am (thx Mr. Galloway!), it was suggested that I follow the advice of this blog post. So long story short, I made the following changes

从来没有用过这个,所以无法确认它是否有效。


In digging into this Access Denied error with Fiddler,

User Rate Limit Exceeded

您的 Google 帐户似乎超出了限制 - 请在此处阅读更多相关信息: