2014-04-29 61 views
8

我在我的ASP.Net MVC 5/WebApi 2项目中使用OWIN的外部身份验证提供程序,我遇到了一个奇怪的问题。第一次外部登录尝试重定向回登录操作,第二次工作

登录工作流程与SO完全相同。用户点击登录页面,挑选供应商,并会记录我的问题是,在运营商的第一次点击重定向到同一个登录页面:

http://localhost:57291/Account/Login?ReturnUrl=%2fAccount%2fExternalLogin 

这将使意义,如果将缺少ExternalLogin行动AllowAnonymous属性。

当用户第二次点击一切正常。

我也尝试过使用不同的浏览器,并且Chrome,IE11和Firefox的问题是一致的。

Login.cshtml:

@using (Html.BeginForm("ExternalLogin", "Account", new { ReturnUrl = ViewBag.ReturnUrl })) 
{ 
    <fieldset> 
     <legend>@Strings.ExternalAuthenticationProvidersDescription</legend> 
     <p> 
      @foreach (var p in Model.ExternalAuthenticationProviders) 
      { 
       <button type="submit" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.Caption</button> 
      } 
     </p> 
    </fieldset> 
} 

AccountController.cs

public class AccountController : Controller 
{ 
    ... 

    [AllowAnonymous] 
    [HttpPost] 
    public ActionResult ExternalLogin(string provider, string returnUrl) 
    { 
     return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new 
     { 
      loginProvider = provider, 
      ReturnUrl = returnUrl 
     })); 
    } 
    ... 
} 

ChallengeResult.cs:

public class ChallengeResult : HttpUnauthorizedResult 
{ 
    public ChallengeResult(string provider, string redirectUrl) 
    { 
     LoginProvider = provider; 
     RedirectUrl = redirectUrl; 
    } 

    public string LoginProvider { get; set; } 
    public string RedirectUrl { get; set; } 

    public override void ExecuteResult(ControllerContext context) 
    { 
     context.HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties 
     { 
      RedirectUri = RedirectUrl 
     }, LoginProvider); 
    } 
} 

个FilterConfig.cs

public class FilterConfig 
{ 
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     filters.Add(new HandleErrorAttribute()); 

     // make all api controllers secure by default 
     filters.Add(new AuthorizeAttribute()); 
    } 
} 

回答

8

原来的问题是,我的项目最初开始了作为有这在web.config中引起问题的MVC 4应用程序:

<authentication mode="Forms"> 
    <forms loginUrl="~/Account/Login" timeout="2880" /> 
</authentication> 

我认为无论OWIN并且表单身份验证同时处于活动状态。

+0

这真的解决了我的问题!谢谢 –

0

我有更新ASPNet.Web.Pages.Web.Data 3.1.1 nuget到任何更高版本时发生的相同问题。与3.1.1它是工作!我找到了解决方案here

0

由于表单身份验证添加我被重定向到登录页面。因此,除去该代码有助于

<authentication mode="Forms"> 
    <forms loginUrl="~/Account/Login" timeout="2880" /> 
</authentication> 

,但我不得不加入这一行以及

<system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules> 
     <remove name="FormsAuthentication" /> <-- added this line to remove it completely --> 
    </modules> 
</system.webServer> 

希望这可以帮助别人。

相关问题