MVC 4 Forms Authentication not working with [Authorize]

青春壹個敷衍的年華 提交于 2019-12-05 06:16:43

If a controller action is decorated with the [Authorize] attribute (as is your Admin/Index action) you cannot invoke this action if you do not have a valid forms authentication cookie in the request.

Also in your Login action, upon successful authentication you should not return a view but you should redirect away, so that the cookie is properly set and available on subsequent requests.

Here's what should happen when a non-authenticated user attempts to access the protected Admin/Index action. The [Authorize] attribute will throw a 401 exception, which as you know from the classic WebForms will be intercepted by the Forms Authentication module and you will be redirected to the loginUrl configured in your web.config passing a ReturnUrl query string parameter the initially requested protected resource.

So you must have a Login action on the account controller that is not decorated with the [HttpPost] attribute and which should serve the view containing the sign-in view. The request will look like this:

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