Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I created a Login view in mvc3 application.
I have the following code to call the authentication function:
$('#btnLogin').click(function () {
        var user = $('#txtUserName').val();
        var pass = $('#txtPass').val();

        $.ajax({
            url: '/Login/Login_btnClick',
            type: 'POST',
            data: { userName: user, password: pass },
            success: function (result) {
                if (result == false) {
                    alert('Invalid Username or Password');
                }
            },
            error: function (result) {
                alert(result.responseText);
            }
        })
    });


As I set the web.config to use Form Authentication:
HTML
<authentication mode="Forms">
      <forms loginUrl="~/Login/Login" defaultUrl="~/Home/index" />
    </authentication>
    <authorization>
      <deny users="?" />
    </authorization>


I also added some locations to be available for all users:
HTML
<location path="Content">
  <system.web>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>

<location path="Scripts">
  <system.web>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>


Then I tried to login using me Login View, but my application couldn't load my functions. Using chrome I saw the following error:

Failed to load resource: the server responded with a status of 400 (Bad Request) http://localhost/ReplaceDNN/Login/%3C%=%20Url.Content

I thought it might be because it can't reach my controller's code since the user is not authenticated yet, so I added to the web.config:

HTML
<location path="Controllers">
  <system.web>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>


It still didn't work...


I also tried on my form to do the following:
HTML
<form method="post" action="/Login/Login_btnClick">
<div align="center">
    <table border="border: solid 1px #e8eef4;">
        <tr>
            <td style="background-color: #e6e6fa">
                <h2 align="center">
                    Login</h2>
                <table>
                    <tr>
                        <td>
                            <label style="color: Black; font-weight: bold">
                                User Name:</label>
                        </td>
                        <td>
                            @Html.TextBox("txtUserName")
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label style="color: Black; font-weight: bold">
                                Password:</label>
                        </td>
                        <td>
                            @Html.TextBox("txtPass")
                        </td>
                    </tr>
                </table>
                <br />
                @*<button id="btnLogin" style="width: 100px">
                    Login</button>*@
                <input type="submit" name="Login" value="Login" />
            </td>
        </tr>
    </table>
</div>
</form>


As I figured it, it supposed on submit to get to the function in the "action" of the form. It still doesn't work. same error....

Any idea what can I do in order to solve it?
Thanks
Posted
Updated 21-Nov-12 22:05pm
v2

1 solution

Ok, I found the solution myself....

So here it is...
First, I used the form which in my original question was:
HTML
<form method="post" action="/Login/Login_btnClick">
<div align="center">
    <table border="border: solid 1px #e8eef4;">
        <tr>
            <td style="background-color: #e6e6fa">
                <h2 align="center">
                    Login</h2>
                <table>
                    <tr>
                        <td>
                            <label style="color: Black; font-weight: bold">
                                User Name:</label>
                        </td>
                        <td>
                            @Html.TextBox("txtUserName")
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label style="color: Black; font-weight: bold">
                                Password:</label>
                        </td>
                        <td>
                            @Html.TextBox("txtPass")
                        </td>
                    </tr>
                </table>
                <br />
                @*<button id="btnLogin" style="width: 100px">
                    Login</button>*@
                <input type="submit" name="Login" value="Login" />
            </td>
        </tr>
    </table>
</div>
</form></form>



Im my web.config I cahanged the:
HTML
<deny users="?" />

to:
HTML
<allow users="?" />


In my form I changed the:
HTML
<form method="post" action="/Login/Login_btnClick">

to:
HTML
<form method="post" action=@Url.Action("DoLogin", "Login")>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900