C# 外部身份验证未重定向到外部站点
这里发生了一件奇怪的事 我已经建立了一个ASP.NET MVC5网站,通过ASP.NET身份,本地帐户运行良好 我现在正试图启用外部身份验证,但出现了一些奇怪的情况 我确信我已经采取了正确的步骤。我在Startup.Auth.cs中有以下内容:C# 外部身份验证未重定向到外部站点,c#,asp.net,asp.net-mvc,asp.net-mvc-5,asp.net-identity,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
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);
}
但是,在浏览器中,什么也不会发生。我只是得到一个空白页面,我希望重定向到谷歌登录页面
没有任何错误报告
另一件有趣的事情是,我试图创建另一个MVC5应用程序,但在VS2013中我得到一个“对象引用未设置为对象实例”弹出窗口,结果项目缺少默认情况下通常存在的帐户控制器
我修复了VS2013的安装,重新安装了Update 1,还更新了解决方案中的所有Nuget软件包
我已经不知道下一步该去哪里了
更新1
考虑到这可能与我的电脑有关,我已将该网站部署到Azure,但问题仍然存在。这是否意味着它可能与丢失的程序集有关,并且没有正确报告?
我已经运行了fusionlog,但没有发现绑定失败
通过干净地安装Windows8和VS2013来启动一个新的虚拟机,看看是否可以在那里运行一个新项目
更新2
好的,刚刚运行了另一轮“网络”捕获,当用户选择外部提供者时,它会发布到ExternalLogin操作,但响应是未经授权的。这可能是什么原因造成的?好的
我弄明白了问题的(重要部分)是什么
首先,我仍然不知道为什么当我创建一个MVC项目时,我没有得到一个脚手架化的AccountController类
然而,我的问题似乎是,我的登录按钮传递的是“谷歌”而不是“谷歌”作为提供者
说真的!?我有点惊讶,套管与供应商的名称有关系,但这就是问题所在。这不是您问题的答案,但它回答了一个非常类似的问题 如果您使用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>
@p.AuthenticationType
对此
<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>
@p.AuthenticationType
唯一改变的是按钮的名称,但这可能会像我一样打断你的头脑,并花费我两天的工作来找到问题
也许这在新的Owin版本中得到了解释,如果不是的话,必须用大写字母指定。我刚刚在我的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方法
我发现这篇文章也很有用:
我只是想消除这个问题,但您是否已经创建了一个项目?我想到的就是这些。如果在创建项目时选择了个人身份验证,则不确定项目没有帐户控制器的原因。是。我在dev控制台中有一个项目,我也用一个Microsoft帐户尝试过。两者的结果都是一样的——一旦ExternalLogin操作完成,结果就是一个完全空白的页面(完全没有任何内容——甚至查看页面源代码),通过fiddler,我发现没有额外的流量或任何请求。请尝试以下步骤2。1-安装或更新Nuget软件包Microsoft.Owin.Security.Google 2-更改ConfigureAuth方法中的最后一行,使其看起来像此应用程序。UseGoogleAuthentication(clientId:“000-000.apps.googleusercontent.com”,clientSecret:“00000000000”);使用您的clientId和clientSecret。你可以从谷歌控制台上获得这些信息。它位于API和Auth>Credentials部分。我通过以下两个步骤从一个新的MVC5项目中获得了它。祝你好运谢谢我尝试了这两个建议,但都没有改变。@Brendon如果我把我的项目提供给你,你觉得会有帮助吗?如果你愿意,我可以把它放在网络上的zip文件中。让我知道,我们可以离线讨论。我有同样的问题,我的提供商名称是正确的。我知道了,我有一个与Owin 2.0在该版本中的项目,_ExternalLoginsListPartial有这样一句话:对我来说,它正在工作(facebook auth),即使我没有接触任何身份验证代码,它现在也停止工作了!我得到的只是
账户/externallogin
上的一个空白屏幕。。。!我也被困在这里了——通过的是“linkedin”而不是“linkedin”。好。那太糟糕了。谢谢你的现场!在我的代码中,它正在传递“Google”,但仍然没有重定向。