Subscribe

RSS Feed (xml)

Creating a Simple Forms Authentication Login Page

Place two TextBox controls for the username and password on the page. Set the


TextMode property of the password TextBox control to


Password. Add a Button control


to perform the login, and a Label control to provide feedback to the users.


The ASPX page is as follows:


<p align="center">


<table cellpadding="2" cellspacing="0" bgcolor="#fafade">


<tr>


<th colspan="2" bgcolor="#baba99">


Login</th>


</tr>


<tr>


<td>User Name:</td>


<td><asp:TextBox ID="UserName" Runat="server" /></td>


</tr>


<tr>


<td>Password:</td>


<td><asp:TextBox ID="Password" Runat="server"


TextMode="Password" /></td>


</tr>


<tr>


<td colspan="2" align="center"><asp:Button ID="Login"


Text="Login" Runat="server" OnClick="Login_Click"/></td>


</tr>


</table>


<br>


<asp:Label ID="Message" Runat="server" ForeColor="#FF0000"></asp:Label>


</p>



In <script runat="server" /> block or codebehind:




Private Sub Login_Click(sender as Object, e as System.EventArgs)


If UserName.Text="johndoe" AND Password.Text="password" Then


System.Web.Security.FormsAuthentication.RedirectFromLoginPage(


UserName.Text, False)


Else


Message.Text="Invalid Username/Password combination"


End If


End Sub


Comments


On the Login button Click event, you simply add code to verify the user ID and


password are correct.You will most likely want to query a database or check another


source to verify the password instead of hard coding the logins in your code. If the


password is correct, you call System.Web.Security.FormsAuthentication.


RedirectFromLoginPage to send the users to the page they had originally requested.


Pass this function the name of the user who has logged in for use later in your application,


and pass it a boolean of whether to persist the cookie across browser sessions (many


login forms use a check box to determine which value to use for this). If the password is


not correct, you display an error to the users.

No comments:

Archives

Variety in the Web World