Mvc Authorize attribute is not working

妖精的绣舞 提交于 2020-01-05 03:51:16

问题


I have created HomeController decorated with AuthorizeAttribute, and also created AccountController, but it is not redirecting to the Login() action of AccountController.

Home controller:

[Authorize]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

Account controller:

public class AccountController : Controller
{
    [HttpGet]
    public ActionResult Login()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login(LogOnCustom log)
    {
        if(ModelState.IsValid)
        {
            if(Membership.ValidateUser(log.UserName,log.Password))
            {
                FormsAuthentication.RedirectFromLoginPage(log.UserName, log.Isremeber);
            }
            else
            {
                ModelState.AddModelError("", "logOn error");
            }
        }
        return View(log);
    }
}

web.config:

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

回答1:


I just had this same problem. Check your web.config and see if it contains this line:

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

Remove the "<remove name="FormsAuthentication" />" line and it should start working. When the FormsAuthentication module is removed, there is no code listening for the 401 event so it doesn't have a chance to redirect the user to the login page.



来源:https://stackoverflow.com/questions/39054129/mvc-authorize-attribute-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!