我目前正在升级Google的登录流程,以便在他们使用OAuth登录OpenID登录方法之前使用OAuth。
到目前为止,我已经确定的步骤是我已将Microsoft.Owin.Security.Google软件包升级到2.1.0版,因为此版本包含了在UseGoogleAuthentication方法中包含选项的功能。
我试图在链接中使用Alex Wheat的解决方案: Get ExtraData from MVC5 framework OAuth/OWin identity provider with external auth provider
Startup.Auth.cs中的代码(也包括Facebook身份验证)来自:
var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
{
AppId = "MYAPPID",
AppSecret = "MYSECRET"
};
facebookAuthenticationOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookAuthenticationOptions);
app.UseGoogleAuthentication();
对此:
var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
{
AppId = "MYAPPID",
AppSecret = "MYSECRET"
};
facebookAuthenticationOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookAuthenticationOptions);
var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = "MYCLIENTID",
ClientSecret = "MYSECRET",
CallbackPath = new PathString("/en/Account/ExternalLoginCallback"),
Provider = new GoogleOAuth2AuthenticationProvider()
{
}
};
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
在我向Google身份验证添加选项后,我的应用不允许为Google或Facebook调用ExternalLoginCallback操作(不更改Facebook代码,但问题仍会影响它)。
在前端,点击外部登录按钮后,页面会将我重定向到下面的链接,并返回一个空的白色屏幕
https ...... / en / Account / ExternalLoginCallback #__ = _(在=符号之前实际上只有一个下划线,SO语法会删除它,如果我有它,就像它出现在我的地址栏上一样)。
对于facebook和
谷歌HTTPS ...... / EN /帐户/ ExternalLoginCallback
。它没有像往常那样点击下面的控制器方法(我试图在这个函数中放置调试断点,并且当有google身份验证选项时它永远不会停止。
// GET: /Account/ExternalLoginCallback
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
如果我从Google身份验证中删除身份验证选项,它只会恢复为原来的OpenID登录状态,并再次正常运行。
我在这里错过了一些简单的东西吗?或者Owin.Security.Google图书馆内是否发生了导致问题的不良事件?
答案 0 :(得分:20)
为了简单起见,我使用默认的ASP.NET MVC 5模板与身份验证,但希望可以针对不同的用例修改它。
<强> StartupAuth.cs 强>
不要自定义重定向路径。无论如何它被/ signin-google取代,我试图绕过它会导致&#34;沉默&#34; (不在调试器中)内部服务器500错误。
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "whatevs.apps.googleusercontent.com",
ClientSecret = "whatevs_secrut",
Provider = new GoogleOAuth2AuthenticationProvider()
});
请务必在APIs & auth
&gt;中将http://whatever.com/signin-google添加到https://console.developers.google.com/。 Credentials
&gt; Redirect URIs
部分。
<强> RouteConfig.cs 强>
将路由添加到路由的永久重定向控制器操作。永久重定向是唯一足够的。仅直接指向回调URL是不够的。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Google API Sign-in",
url: "signin-google",
defaults: new { controller = "Account", action = "ExternalLoginCallbackRedirect" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
<强> AccountController.cs 强>
永久重定向到内置回调方法,你应该没问题。
[AllowAnonymous]
public ActionResult ExternalLoginCallbackRedirect(string returnUrl)
{
return RedirectPermanent("/Account/ExternalLoginCallback");
}
模板项目已发布在GitHub上以供参考:https://github.com/Pritchard/Test-AspNetGoogleOAuth2Authentication
答案 1 :(得分:15)
只尝试这个
var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = "MYCLIENTID",
ClientSecret = "MYSECRET",
};
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
这对我有用
答案 2 :(得分:12)
确保您还在开发者控制台中启用了Google+ API。拥有客户端和秘密后,这是一个额外的步骤
答案 3 :(得分:3)
正如@Ronen在评论中所说,此链接应解决MVC5中Google OAuth的问题:
还从NuGet更新OWIN包。这就是我的代码的外观和工作方式:
var googleOptions = new GoogleOAuth2AuthenticationOptions () { ClientId = "xxxxxxxxxx", ClientSecret = "xxxxxxxxxx", CallbackPath = new PathString("/signin-google") }; googleOptions.Scope.Add("email"); app.UseGoogleAuthentication(googleOptions);