Asp.net mvc 在MVC5中使用OWIN Oauth的Google身份验证未命中ExternalLoginCallback函数

Asp.net mvc 在MVC5中使用OWIN Oauth的Google身份验证未命中ExternalLoginCallback函数,asp.net-mvc,oauth,asp.net-identity,owin,google-authentication,Asp.net Mvc,Oauth,Asp.net Identity,Owin,Google Authentication,我目前正在升级我的登录过程,以便谷歌在对其OpenID登录方法进行解密之前使用OAuth 到目前为止,我已经确定的步骤是,我已经将包Microsoft.Owin.Security.Google升级到版本2.1.0,因为该版本包括在UseGoogleAuthentication方法中包含选项的功能 我尝试在链接中使用Alex Wheat的解决方案: Startup.Auth.cs(还包括Facebook身份验证)中的代码如下: var facebookAuthenticationOpti

我目前正在升级我的登录过程,以便谷歌在对其OpenID登录方法进行解密之前使用OAuth

到目前为止,我已经确定的步骤是,我已经将包Microsoft.Owin.Security.Google升级到版本2.1.0,因为该版本包括在UseGoogleAuthentication方法中包含选项的功能

我尝试在链接中使用Alex Wheat的解决方案:

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);
在我向谷歌认证添加选项后,我的应用程序不允许对谷歌或facebook调用ExternalLoginCallback操作(facebook代码没有更改,但问题仍然会影响它)

在前端,单击外部登录按钮后,页面将我重定向到下面的链接,并返回一个空白的白色屏幕

https……/en/Account/ExternalLoginCallback#uuuuu=uuuu(实际上,=符号前只有一个下划线,因此如果地址栏上有下划线,语法会将其删除)

对于facebook和

https……/en/Account/ExternalLoginCallback

对于谷歌来说。它不会像正常情况下那样命中下面的控制器方法(我已经尝试在这个函数中放置调试断点,当有google身份验证选项时,它永远不会停止

    // GET: /Account/ExternalLoginCallback
    [AllowAnonymous]
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
//获取:/Account/ExternalLoginCallback
[异名]
公共异步任务ExternalLoginCallback(字符串返回URL)
{
如果我从Google认证中删除认证选项,它只会恢复到旧的OpenID登录,并再次正常工作

我是否遗漏了一些简单的内容?或者是Owin.Security.Google库中发生了一些导致问题的不好的事情?

请仅尝试此项

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
        {
            ClientId = "MYCLIENTID",
            ClientSecret = "MYSECRET",
        };
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);

这对我来说很有效

为了简单起见,我使用了默认的ASP.NET MVC 5模板和身份验证,但希望可以针对不同的使用情况进行修改

StartupAuth.cs

不要自定义重定向路径。它会被/signin google替换,而我的尝试会导致“静默”(不在调试器中)内部服务器500错误

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
    ClientId = "whatevs.apps.googleusercontent.com",
    ClientSecret = "whatevs_secrut",
    Provider = new GoogleOAuth2AuthenticationProvider()
});
确保在
api&auth
凭证
重定向URI
部分中添加到

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上发布了一个模板项目供参考:

正如@Ronen在评论中所说,这个链接应该可以解决MVC5中Google OAuth的问题:

还可以从NuGet更新OWIN包。这是我的代码的外观和工作原理:


请确保您也在开发人员控制台中启用了Google+API。这是在您拥有客户端和密码后的一个附加步骤。这解决了我的facebook身份验证问题。当我使用Google时,它会尝试以回调
https…/登录Google
的形式转到我网站上的以下链接,您知道吗如何设置回调路径?当我使用原始帖子中的回调路径时,会出现与原始问题相同的问题。clientID在google中设置为指向我的ExternalLoginCallback函数,但它实际上不会在返回时调用它。接下来,使用此方法似乎会自动在中使用signin google请求地址。我所做的修复是将我在谷歌控制台中的谷歌回调位置更改为指向此地址。我还在RouteConfig文件`routes.MapRoute(名称:“signin google”,url:“signin google”,默认值:new{controller=“Account”,action=“ExternalLoginCallback”})谢谢你的帮助-joshi@Brad:抱歉,无法尽快回复,但很高兴您找到了解决方案查看我的发现:关于这些更改的详细信息您能否解释一下为什么我们需要
ExternalLoginCallbackRedirect
?为什么不直接在路由配置中使用
ExternalLoginCallback
alHosala我猜这是可能的。但是,这个问题是今年2月提出的,现在我不再参与回忆上下文所需的工作。如果有更简单的方法来完成任务,我会说去做!谢谢你的回答,但是,如果我使用
外部逻辑调用
,那么,谢谢很显然,它停止工作了,我在登录后根本没有被重定向到
外部LoginCallback
,所以你的代码似乎是正确的,只是不知道为什么我们需要这个额外的步骤:)作为将来的参考-基于我看不出有任何理由修改RouteConfig.cs和AccountController.cs。google auth.继续工作(对我来说)即使删除了这两个文件中的更改。@MichalHosala我认为该链接可能值得自己回答:)我对这个问题的回答是专门针对我在Visual Studio模板附带的示例站点中遇到的问题。这为我解决了问题,ta!遗憾的是,在任何Microsoft演练中都没有提到这一点…这为我解决了问题!数小时的疑难解答,谢谢!
       var googleOptions = new GoogleOAuth2AuthenticationOptions ()
       {
           ClientId = "xxxxxxxxxx",
           ClientSecret = "xxxxxxxxxx",
           CallbackPath = new PathString("/signin-google")
       };
       googleOptions.Scope.Add("email");

       app.UseGoogleAuthentication(googleOptions);