【发布时间】: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