问题
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