【问题标题】:External Authentication not redirecting to external site外部身份验证未重定向到外部站点
【发布时间】:2014-05-26 16:03:43
【问题描述】:

这里发生了一件奇怪的事情。

我已经建立了一个 ASP.NET MVC5 网站,并通过 ASP.NET Identity 让本地帐户正常工作。

我现在正在尝试启用外部身份验证,但发生了一些奇怪的事情。

我确定我已经采取了正确的步骤。我的 Startup.Auth.cs 中有这个:

    public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Uncomment the following lines to enable logging in with third party login providers
        //app.UseMicrosoftAccountAuthentication(
        //    clientId: "",
        //    clientSecret: "");

        //app.UseTwitterAuthentication(
        //   consumerKey: "",
        //   consumerSecret: "");

        //app.UseFacebookAuthentication(
        //   appId: "",
        //   appSecret: "");

        app.UseGoogleAuthentication();
    }

当用户点击链接登录 Google 时,调用 ExternalLogin 方法:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult ExternalLogin(string provider, string returnUrl)
    {
        // Request a redirect to the external login provider
        return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
    }

我已通过调试验证进入 ChallengeResult 类的 ExecuteResult 方法:

        public override void ExecuteResult(ControllerContext context)
        {
            var properties = new AuthenticationProperties() { RedirectUri = RedirectUri };
            if (UserId != null)
            {
                properties.Dictionary[XsrfKey] = UserId;
            }
            context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
        }

但是,在浏览器中,什么也没有发生。我只是得到一个空白页面,我希望重定向到 Google 登录页面。

根本没有报告任何错误。

另一个有趣的事情是,我尝试创建另一个 MVC5 应用程序,但是在 VS2013 中弹出“对象引用未设置为对象的实例”,结果项目缺少通常存在的帐户控制器默认情况下。

我已经修复了VS2013的安装,我已经重新安装了Update 1,我还更新了解决方案中的所有Nuget包。

我已经不知道下一步该去哪里了。

更新 1 想到这可能和我的电脑有关,我已经将网站部署到Azure,问题依旧。这是否意味着它可能与缺少的程序集有关,并且没有正确报告? 我运行了 fusionlog,但没有发现绑定失败。

启动一个全新安装 Windows 8 和 VS2013 的新虚拟机,看看我是否可以让一个新项目在那里工作。

更新 2 好的,刚刚运行了另一轮“网络”捕获,当用户选择外部提供商时,它确实发布到 ExternalLogin 操作,但响应是 401 Unauthorized。这可能是什么原因造成的?

【问题讨论】:

  • 只是想消除这种情况,但是您是否在Google developers Console 创建了一个项目?这就是我想到的所有内容。如果您在创建项目时选择了个人身份验证,不确定为什么您的项目没有帐户控制器。
  • 是的。我在开发控制台中有一个项目,我也尝试过使用 Microsoft 帐户。两者都导致相同的结果 - 一旦 ExternalLogin 操作完成,结果是一个带有完全空白页面的浏览器(绝对没有 - 甚至查看页面源)并且通过提琴手我看到没有额外的流量或任何请求制作。
  • 尝试这些步骤 2. 1- 安装或更新 Nuget 包 Microsoft.Owin.Security.Google 2- 更改 ConfigureAuth 方法的最后一行,使其看起来像这个 app.UseGoogleAuthentication(clientId: " 000-000.apps.googleusercontent.com”,clientSecret:“00000000000”);使用您的 clientId 和 clientSecret。您可以从 Google 控制台获取这些内容。它位于 APIs and Auth > Credentials 部分。我按照这两个步骤从一个新的 MVC 5 项目中得到了它。祝你好运!
  • 谢谢。我已经尝试了这两个建议,但没有任何改变。
  • @Brendon 你认为如果我将我的项目提供给你会有帮助吗?如果您愿意,我可以将它以 zip 文件的形式放到网上。告诉我,我们可以离线讨论。

标签: c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity


【解决方案1】:

好的,

我弄清楚了问题的(重要部分)是什么。

首先,我仍然不知道为什么当我创建一个 MVC 项目时,我没有得到一个脚手架的 AccountController 类。

但是,我的问题似乎是我的登录按钮传递的是“google”而不是“Google”作为提供者。

说真的!?我有点惊讶,大小写与提供者的名称很重要,但是你去吧。

【讨论】:

  • 我遇到了同样的问题,我的提供商名称是正确的
  • 我明白了,我在那个版本中有一个带有 Owin 2.0 的项目,_ExternalLoginsListPartial 有这样一行:
  • 对我来说它正在工作(facebook auth),即使我没有接触任何身份验证代码,它现在停止工作了!我得到的只是account/externallogin上的一个空白屏幕@
  • 一个……的儿子!我也被困在这里-传递的是“linkedin”而不是“LinkedIn”。好吧。太糟糕了。感谢现场!
  • 在我的代码中,它传递了“Google”,但仍然没有重定向。
【解决方案2】:

这不是您问题的答案,但它回答了一个非常相似的问题

如果您拥有 Owin 2.0 并迁移到 2.1

_ExternalLoginsListPartial 需要从这里改变:

<button type="submit" class="btn btn-default" id="@p.AuthenticationType" name="Partner" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button>

到这里

<button type="submit" class="btn btn-default" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button>

唯一改变的是按钮的名称,但这可能会像我一样让你头疼,并花费我 2 天的时间来找到问题。

也许这在新的 Owin 版本中有解释,如果不是必须用大写字母指定。

【讨论】:

    【解决方案3】:

    我的 ASP.Net WebAPI 服务器刚刚遇到了同样的问题。我将正确的 AuthenticationType 传递给质询方法,但仍然得到一个空白页。

    原来问题是我在启动时将 OAuth 提供程序添加到 OWIN 管道中的顺序。 大致是我的工作顺序:

            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
            OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
    
            var authServerOptions = new OAuthAuthorizationServerOptions
            {
            ...
            };
    
            // Token Generation
            app.UseOAuthAuthorizationServer(authServerOptions);
            app.UseOAuthBearerAuthentication(OAuthBearerOptions);
    
            // Configure Google External Login
            GoogleAuthOptions = new GoogleOAuth2AuthenticationOptions()
            {
                ClientId = xxxx,
                ClientSecret = xxxx,
                Provider = new GoogleAuthProvider()
            };
            app.UseGoogleAuthentication(GoogleAuthOptions);
    
            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
            app.UseWebApi(config);
    

    之前我在 UseGoogleAuthentication 方法之前添加了 UseCors + UseWebApi 方法。

    我发现这篇文章也很有用: http://coding.abel.nu/2014/06/understanding-the-owin-external-authentication-pipeline/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      相关资源
      最近更新 更多