外部身份验证不会重定向到外部站点

时间:2014-04-12 12:44:52

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

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

我已经构建了一个ASP.NET MVC5网站,并通过ASP.NET身份使本地帐户正常工作。

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

我确定我已经按照正确的步骤。我在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 认为这可能与我的PC有关,我已将该网站部署到Azure,问题仍然存在。这是否意味着它可能与缺少的装配有关,并且没有正确报告? 我已经运行了fusionlog,但我没有看到绑定失败。

通过全新安装Windows 8和VS2013来启动新VM,看看我是否可以在那里开展新项目。

更新2 好的,只是进行了另一轮“网络”捕获,当用户选择外部提供商时,它会发布到ExternalLogin操作,但响应是401 Unauthorized。可能导致这种情况的原因是什么?

3 个答案:

答案 0 :(得分:12)

确定,

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

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

然而,似乎我的问题是我的登录按钮是通过“google”而不是“Google”作为提供商。

严重!?套管与提供商的名称有关,我有点惊讶,但你去了。

答案 1 :(得分:0)

这不是你的问题的答案,但它回应了一个非常类似的问题

如果你有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版本中解释的,如果不是必须在大写字母中指定。

答案 2 :(得分:0)

我的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/