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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23072786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:38:52  来源:igfitidea点击:

Google Authentication using OWIN Oauth in MVC5 not hitting ExternalLoginCallback function

asp.net-mvcoauthasp.net-identityowingoogle-authentication

提问by Brad Baskin

I am currently upgrading my login process for Google to use OAuth before they depricate their OpenID login method.

我目前正在升级我的登录过程,以便 Google 在他们贬低他们的 OpenID 登录方法之前使用 OAuth。

The steps I have Identified so far is that I have upgraded the package Microsoft.Owin.Security.Google to version 2.1.0 as this version includes the ability to include options in the UseGoogleAuthentication method.

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

I have tried to use Alex Wheat's Solution in the link: Get ExtraData from MVC5 framework OAuth/OWin identity provider with external auth provider

我尝试在链接中使用 Alex Wheat 的解决方案: Get ExtraData from MVC5 framework OAuth/OWin identity provider with external auth provider

The code in Startup.Auth.cs (which also includes Facebook authentication) goes from this:

Startup.Auth.cs(也包括 Facebook 身份验证)中的代码来自:

    var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
        {
            AppId = "MYAPPID",
            AppSecret = "MYSECRET"
        };
        facebookAuthenticationOptions.Scope.Add("email");
        app.UseFacebookAuthentication(facebookAuthenticationOptions);

        app.UseGoogleAuthentication();

To this:

对此:

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);

After I add options to the Google Authentication, my app does not allow the ExternalLoginCallback action to be called for either google or facebook (no changes to facebook code but the issue still affects it).

在向 Google 身份验证添加选项后,我的应用不允许为 google 或 facebook 调用 ExternalLoginCallback 操作(facebook 代码没有更改,但问题仍然影响它)。

On the front end, after clicking the external login buttons, the page redirects me to the link below and returns an empty white screen

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

https....../en/Account/ExternalLoginCallback#__=_ (There is actually only a single underscore before the = sign, SO syntax removes it if I have it as it appears on my address bar).

https....../en/Account/ExternalLoginCallback#__=_(= 符号前实际上只有一个下划线,如果我有它,那么语法会将它删除,因为它出现在我的地址栏中)。

for facebook and

脸书和

https....../en/Account/ExternalLoginCallback

https....../en/Account/ExternalLoginCallback

for google. It does not hit the controller method below as it normally does (I have tried to place debug breakpoints within this function and it never gets stopped when there are google authentication options.

对于谷歌。它不会像往常一样命中下面的控制器方法(我尝试在此函数中放置调试断点,并且在有 google 身份验证选项时它永远不会停止。

    // GET: /Account/ExternalLoginCallback
    [AllowAnonymous]
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {

If I remove the authentication options from Google Authentication, it just reverts back to the old OpenID login and works fine again.

如果我从 Google 身份验证中删除身份验证选项,它只会恢复到旧的 OpenID 登录名并再次正常工作。

Am I missing something simple here? or is there something bad happening inside the Owin.Security.Google Library that is causing the problem?

我在这里错过了一些简单的东西吗?或者 Owin.Security.Google 库内部发生了什么不好的事情导致了问题?

采纳答案by Suhas Joshi

Try only this

只试这个

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

This worked for me

这对我有用

回答by Brad Baskin

I'm using the default ASP.NET MVC 5 template with Identity Authentication for simplicity, but hopefully this can be modified for different use cases.

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

StartupAuth.cs

启动Auth.cs

Do not customize the redirect path. It gets replaced by /signin-google anyways and my attempts at getting around that caused "silent" (not in the debugger) Internal Server 500 errors.

不要自定义重定向路径。它无论如何都会被 /signin-google 取代,我试图绕过导致“无声”(不在调试器中)内部服务器 500 错误的尝试。

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
    ClientId = "whatevs.apps.googleusercontent.com",
    ClientSecret = "whatevs_secrut",
    Provider = new GoogleOAuth2AuthenticationProvider()
});

Make sure to add http://whatever.com/signin-googleto https://console.developers.google.com/in your APIs & auth> Credentials> Redirect URIssection.

确保添加http://whatever.com/signin-googlehttps://console.developers.google.com/在您APIs & auth> Credentials>Redirect URIs部分。

RouteConfig.cs

路由配置文件

Add a route to a permanent redirect controller action to your routes. Permanent redirects are the only thing that will suffice here. It is not enough to simply direct directly to the Callback URL.

将路由添加到您的路由的永久重定向控制器操作。永久重定向是唯一可以满足要求的方法。仅仅直接指向回调 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

账户控制器.cs

Permanent redirect to the built-in callback method and you should be fine.

永久重定向到内置回调方法,你应该没问题。

[AllowAnonymous]
public ActionResult ExternalLoginCallbackRedirect(string returnUrl)
{
    return RedirectPermanent("/Account/ExternalLoginCallback");
}

A template project has been posted on GitHub for reference: https://github.com/Pritchard/Test-AspNetGoogleOAuth2Authentication

GitHub 上已经发布了一个模板项目供参考:https: //github.com/Pritchard/Test-AspNetGoogleOAuth2Authentication

回答by Aaron Sherman

Make sure you have also enabled the Google+ API in your developer console. It is an additional step after you have your client and secret

确保您还在开发者控制台中启用了 Google+ API。这是您拥有客户和机密后的额外步骤

回答by Rick

As @Ronen said in the comments, this link should solve the issues with Google OAuth in MVC5:

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

http://blogs.msdn.com/b/webdev/archive/2014/07/02/changes-to-google-oauth-2-0-and-updates-in-google-middleware-for-3-0-0-rc-release.aspx

http://blogs.msdn.com/b/webdev/archive/2014/07/02/changes-to-google-oauth-2-0-and-updates-in-google-middleware-for-3-0- 0-rc-release.aspx

Also update the OWIN packages from NuGet. This is how my code looks and is working great:

还要从 NuGet 更新 OWIN 包。这是我的代码的外观并且运行良好:

       var googleOptions = new GoogleOAuth2AuthenticationOptions ()
       {
           ClientId = "xxxxxxxxxx",
           ClientSecret = "xxxxxxxxxx",
           CallbackPath = new PathString("/signin-google")
       };
       googleOptions.Scope.Add("email");

       app.UseGoogleAuthentication(googleOptions);
       var googleOptions = new GoogleOAuth2AuthenticationOptions ()
       {
           ClientId = "xxxxxxxxxx",
           ClientSecret = "xxxxxxxxxx",
           CallbackPath = new PathString("/signin-google")
       };
       googleOptions.Scope.Add("email");

       app.UseGoogleAuthentication(googleOptions);