Mvc Authorize attribute is not working

asp.net-mvc web-config authorize-attribute

2790 просмотра

1 ответ

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>
Автор: anitha Источник Размещён: 08.11.2019 11:18

Ответы (1)


0 плюса

Решение

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.

Автор: David Размещён: 10.09.2016 04:36
Вопросы из категории :
32x32