繁体   English   中英

AuthorizeAttribute保持重定向到/ Account / Login

[英]AuthorizeAttribute keeps redirecting to /Account/Login

我试图在ASP.NET MVC中围绕Forms身份验证。 MVC 5在我的具体情况下,如果重要的话。

我的应用程序不使用密码,只使​​用电子邮件地址作为用户名。

调试Login方法时,我可以清楚地看到模型有效,我的(自定义) MembershipProvider按预期验证用户。
然后它重定向到提供的returnUrl (为了测试,我在/ Home / About上有一个AuthorizeAttribute )。

可悲的是,我立即被回到了Login视图,所以很明显我错过了整个过程的基本元素(并且,通过扩展,对整个auth / auth过程的基本洞察,我必须承认,因为我很少玩弄它)。

Login方法:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if(ModelState.IsValid && Membership.ValidateUser(model.Email, ""))
    {
        FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);

        if (Url.IsLocalUrl(returnUrl))
        {
            return RedirectToLocal(returnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }
    else
    {
        ModelState.AddModelError("", "Email address unknown");
    }

    return View(model);
}

LoginViewModel:

public class LoginViewModel
{
    [Required]
    [Display(Name = "Email")]
    [EmailAddress]
    public string Email { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

Web.config的相关部分:

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

我没看到什么? 我应该在哪里看?

您正在使用FormsAuthentication设置Cookie。 如果您使用的是MVC5,则会使用[Authorize]属性删除该类型的身份验证。

在web.config中查找。 如果要使用FormsAuthentication,请删除该行。

  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
  </system.webServer>

您可能想要了解Microsoft为何在MVC5中删除FormsAuthentication以及如何使用OWIN的原因: http//blogs.msdn.com/b/webdev/archive/2013/07/03/understanding-owin-forms-authentication-在-MVC-5.aspx

IIS Express的设置不正确可能导致此问题。

检查IISExpress设置。 通过按项目上的F4或编辑项目文件*.csproj

  1. 将属性Anonymous Authentication设置为已Enabled ;
  2. 将属性Windows Authentication设置为Disabled

或编辑配置为:

<PropertyGroup>
  <IISExpressAnonymousAuthentication />
  <IISExpressWindowsAuthentication />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM