I’m using MVC. I copied the login form from the login page and inserted into a new page (home controller, index view). I copied the code from the accounts controller into the index view. for some reason, i’m still not able to login. I’m not sure what’s wrong as it looks like I copied the necessary code exactly. When I use the form on the index page, I don’t get any validation errors, but it returns me back to the view and I’m not logged in.
You’ll notice that I changed this line to redirect to an Error page but I never get this far:
// If we got this far, something failed, redisplay form
return RedirectToAction("Error", "Home");
I posted the code here: http://pastebin.com/RUj6ASvE
It’s because you are posting the from to the Index action whereas you should be posting it to the LogOn action, where your authentication logic is in.
Try changing
Html.BeginForm()toHtml.BeginForm("LogOn", "HomeController")in your view.In the code you posted you were presenting the form from the
HomeController‘sIndexaction. And posting it back to the same controller’s same action. But that action was not handling the authentication logic. That’s why nothing was happening and you weren’t logging in.In the default ASP.NET MVC 2 site, however, they are presenting the form from the
AccountController‘sLogOnaction :So when they use
Html.BeginForm()in the view, it creates a form that will POST to the same controller’s same action. So they create another action with the nameLogOn:But this time they decorate that action with the
HttpPostattribute. That means if a request hits theAccountController‘sLogOnaction with the POST verb, that method above will be executed. But if the same action is requested with the GET verb (ie without a POST body) the other method will be executed.So basically, you could have done this in your
HomeController:But your action (the one that was supposed to handle the authentication logic) was named differently. And that’s why we needed to explicitly set the controller and the action name in the
Html.BeginForm("LogOn", "HomeController").