tags:

views:

114

answers:

1

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");
            }
        }
+1  A: 

Can you confirm that /Account.mvc/LogOn is a valid route?

Things to check:

  • Is the route valid?

  • Does the LogOn action have ActionVerb on it, preventing post requests - such as: [ActionVerbs(HttpVerbs.Get)]

Edit: Looking at your code, ignoring the fact that the breakpoint. get redirected to the home page, since you're not passing the returnUrl, won't it just send you back to the home page when it's successful?

Put this in your master page. What does it return?

<%=Request.IsAuthenticated %>

Edit 2:

Apparently this returns false.

Okay... So the controller is okay, and the view looks okay, and is rendering the correct url... I assume that ASP.NET MVC is installed and is in the GAC, given that you can also browse to other urls such as LogOn directly. And it looks like you're running it on Windows XP, given the '.mvc' in the route.

What do your routes look like? Have you made any changes to them?

Dan Atkinson
Yes, this is a valid route. I can definitely go directly to it in my browser... localhost:2500\Account.mvc\LogOn
gmcalab
So it also accepts GET requests? What about POST? Are there any ActionVerbs on it?
Dan Atkinson
Yes the LogOn action has the ActionVerb. I will post that action above for reference.
gmcalab
But you mentioned that you can browse to it. Looking at what the action, I don't see how you could browse to the page though, if you see what I mean?
Dan Atkinson
Unless you have another LogOn method that accepts GET requests, that is...
Dan Atkinson
Well its the out of the box auth generated by MVC. So in the Account View I have ChangePassword.aspx, ChangePAsswordSuccess.aspx, LogOn.aspx and Register.aspx. I just wanted to use the LogOn() logic so i didnt have to write a whole new one in my home controller...
gmcalab
Yes if successful it would return me to my home page. But I have put in the incorrect credentials to test...and it still throws me back to my Home View
gmcalab
So putting <%=Request.IsAuthenticated %> into the masterpage show 'false'?
Dan Atkinson
Yes, Request.IsAuthenticated returns false currently...
gmcalab
Okay. I've updated the answer a bit further in that case.
Dan Atkinson
Well I cleaned the project recompiled and now it works. I should have gone back to the basic fix first. Thanks much for helping me! +1
gmcalab