使用MVC5中的OWIN Oauth进行的Google身份验证不会影响ExternalLoginCallback功能

Bra*_*kin 24 asp.net-mvc oauth google-authentication owin asp.net-identity

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

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

我试图在链接中使用Alex Wheat的解决方案: 从外部身份验证提供程序获取MVC5框架OAuth/OWin身份提供者的ExtraData

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

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

        app.UseGoogleAuthentication();
Run Code Online (Sandbox Code Playgroud)

对此:

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);
Run Code Online (Sandbox Code Playgroud)

在我向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)
    {
Run Code Online (Sandbox Code Playgroud)

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

我错过了一些简单的东西吗?或者Owin.Security.Google图书馆内是否有一些不良事件导致了这个问题?

Ale*_*ard 20

为简单起见,我使用带有身份验证的默认ASP.NET MVC 5模板,但希望可以针对不同的用例修改此模板.

StartupAuth.cs

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

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

确保添加http://whatever.com/signin-googlehttps://console.developers.google.com/在您APIs & auth> Credentials> 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 }
    );
}
Run Code Online (Sandbox Code Playgroud)

AccountController.cs

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

[AllowAnonymous]
public ActionResult ExternalLoginCallbackRedirect(string returnUrl)
{
    return RedirectPermanent("/Account/ExternalLoginCallback");
}
Run Code Online (Sandbox Code Playgroud)

模板项目已发布在GitHub上以供参考:https: //github.com/Pritchard/Test-AspNetGoogleOAuth2Authentication

  • 您能否解释一下为什么我们需要“ExternalLoginCallbackRedirect”?为什么不直接在路由配置中使用“ExternalLoginCallback”? (2认同)

Suh*_*shi 15

试试这个

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
        {
            ClientId = "MYCLIENTID",
            ClientSecret = "MYSECRET",
        };
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
Run Code Online (Sandbox Code Playgroud)

这对我有用

  • 只是为了跟进,使用此方法似乎会自动在地址中使用signin-google请求.我为解决这个问题所做的是将谷歌控制台中的谷歌回调位置更改为指向此地址.我还在RouteConfig文件中添加了一个路由`routes.MapRoute(name:"signin-google",url:"signin-google",默认值:new {controller ="Account",action ="ExternalLoginCallback"});`谢谢为了你的帮助@ suhas-joshi (14认同)
  • 读这篇文章的人,必须阅读:http://stackoverflow.com/a/23595862/833846 (4认同)
  • 这解决了我的facebook身份验证时发生的问题.当我使用谷歌它试图转到我的网站上的以下链接作为回调`https .../signin-google`你知道如何设置回调路径吗?当我使用我在原始帖子中使用的callbackPath时,出现了与原始问题相同的问题.clientID在google中设置为指向我的ExternalLoginCallback函数,但实际上并没有在返回时调用它. (2认同)

Aar*_*man 13

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