Similar to this post: MVC 3 - FormsAuthentication - Can't give access to my Login action
I'm unable to get my form to submit the username/password to the post handler of the Logon action. However, my AccountController inherits from the default controller, i.e. no [Authorize]-attribute. My account controller looks like this:
[HttpGet]
public ActionResult LogOn()
{
return View();
}
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl = null)
{
...
}
And my web.config contains, amongst others:
<location path="Account">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
<authorization>
<deny users="?" />
</authorization>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" defaultUrl="~/Home/Index" />
</authentication>
Upon submitting the login-form, it immediately redirects me to the [HttpGet] version of the LogOn-action. I never get directed to the POST version. I get the same response as mentioned in the post above:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/Account/LogOn?ReturnUrl=%2f">here</a>.</h2>
</body></html>
If I comment out <authorization> everything works fine. However, in that case, when I enter a deep link to a page which required authorization, I don't get redirected to the logon page.
I've also tried adding the location "Account" and allowing all users, but this does not seem to have any effect.
Can anyone point out what I'm doing wrong?
EDIT
In Fiddler I always see this pattern:
# Result Protocol Host URL Body Caching Content-Type Process Comments Custom
1 302 HTTP localhost:36372 / 145 private text/html; charset=utf-8 iexplore:6400
2 200 HTTP localhost:36372 /Account/LogOn?ReturnUrl=%2f 4,752 private text/html; charset=utf-8 iexplore:6400
Why is the first URL always /?
EDIT2
It occured to me to check the generated HTML, and to my surprise it looks like this:
<form action="/" id="LogOnForm" method="post">
Obviously, this is not the correct action. The ASP code looks like this:
@using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { id = "LogOnForm" }))
And my routes look like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
);