Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
<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:
  <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:
 
  <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:
<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 21-Nov-12 22:28pm
DanaH85575
Edited 21-Nov-12 23:05pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Ok, I found the solution myself....
 
So here it is...
First, I used the form which in my original question was:
<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>
                
 
                @*<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:
<deny users="?" />
to:
<allow users="?" />
 
In my form I changed the:
<form method="post" action="/Login/Login_btnClick">
to:
<form method="post" action=@Url.Action("DoLogin", "Login")>
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100