I have started an MVC project and it has created the AccountController Class for me. So to login to my current site I would have to go to, localhost:2500\Account.mvc\LogOn
I wanted to make the login on my HomeController in the Index view. So basically if you go to my root of the site if not authenticated you would have to login to go anywhere else.
So what I did was add the form to my Index View and tried to have it post to my Account view calling the LogOn action.
I have called the BeginForm method passing it my action and the respective view (LogOn, Account). When I click the button, it doesn't post to the Account view therefore doesn't execute the LogOn action. Instead, it just posts to the Home view calling the Index action again. So it really does nothing for me... haha.
Not sure what I am doing wrong. Below is my raw form code from Home/Index.aspx and then I also will show you the source after its rendered before hitting the logon button.
Here's my code from Home/Index.aspx:
<% using (Html.BeginForm("LogOn", "Account")) { %>
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">Username:</label>
<%= Html.TextBox("username") %>
<%= Html.ValidationMessage("username") %>
</p>
<p>
<label for="password">Password:</label>
<%= Html.Password("password") %>
<%= Html.ValidationMessage("password") %>
</p>
<p>
<%= Html.CheckBox("rememberMe") %> <label class="inline" for="rememberMe">Remember me?</label>
</p>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
<% } %>
View Source of Home.mvc/Index:
<form action="/Account.mvc/LogOn" method="post">
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">Username:</label>
<input id="username" name="username" type="text" value="" />
</p>
<p>
<label for="password">Password:</label>
<input id="password" name="password" type="password" />
</p>
<p>
<input id="rememberMe" name="rememberMe" type="checkbox" value="true" /><input name="rememberMe" type="hidden" value="false" /> <label class="inline" for="rememberMe">Remember me?</label>
</p>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
</form>
I set a break point at my LogOn action and it never gets hit, so its not a login error either.
It looks like the action in the html source is set correctly, I just don't understand why it doesn't post correctly. I'm not doing something right. Please let me know if you have any ideas. Thanks!
LogOn Action in the Account View:
[AcceptVerbs(HttpVerbs.Post)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
Justification = "Needs to take same parameter type as Controller.Redirect()")]
public virtual ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl)
{
if (!ValidateLogOn(userName, password))
{
return View();
}
FormsAuth.SignIn(userName, rememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}