| |||||||||||||
Simple Forms AuthenticationASP.NET Web Application Security Designing Secure ASP.NET Applications This example presents the simplest possible implementation of ASP.NET Forms-based Authentication. It is intended to illustrate the fundamentals of how to create an ASP.NET application that uses forms authentication. In this scenario, the client requests a protected resource, Default.aspx. There is only one user who can gain access to the protected resource: jdoe@somewhere.com, with a password of password. The username and password are hard-coded into the Login.aspx file. For an example of forms authentication that uses an XML file to hold usernames and passwords, see Forms Authentication Using An XML Users File. There are three files involved: Default.aspx, Login.aspx, and Web.config. The files reside in the application root directory. The code in these files is analyzed in the following discussion. The |
|||||||||||||
<script language = "C#" runat=server>
void Login_Click ( Object sender, EventArgs E ) {
if ( ( UserEmail.Value == "jdoe@somewhere.com" ) &&
( UserPass.Value == "password" ) )
FormsAuthentication.RedirectFromLoginPage
( UserEmail.Value, PersistForms.Checked );
else
Msg.Text = "Invalid Credentials: Please try again";
}
</script>
| ||
| C# | VB | |
<body>
<form runat=server>
<h3>Login Page</h3>
<table>
<tr>
<td>Email:</td>
<td><input id = "UserEmail" type = "text" runat=server/></td>
<td><ASP:RequiredFieldValidator
ControlToValidate = "UserEmail"
Display = "Static"
ErrorMessage = "*"
runat=server/>
</td>
</tr>
<tr>
<td>Password:</td>
<td><input id = "UserPass" type=password runat=server/></td>
<td><ASP:RequiredFieldValidator
ControlToValidate = "UserPass"
Display = "Static"
ErrorMessage = "*"
runat=server/>
</td>
</tr>
<tr>
<td>Persistent Forms:</td>
<td><ASP:CheckBox id=PersistForms runat = "server"
autopostback = "true" />
</td>
<td></td>
</tr>
</table>
<input type = "submit" onServerClick = "Login_Click" Value = "Login"
runat = "server" /><p>
<asp:Label id = "Msg" ForeColor = "red" Font-Name = "Verdana"
Font-Size = "10" runat=server />
</form>
</body>
The Default.aspx file is the requested, protected resource. It is a simple file that merely displays the string, Hello, plus the recorded e-mail name, and a Signout button.
<%@ Page language = "C#" %>
<html>
<head>
<title>Forms Authentication</title>
<script runat=server>
private void Page_Load ( Object sender, EventArgs e ) {
Welcome.InnerHtml = "Hello, " + Context.User.Identity.Name;
}
private void Signout_Click ( Object sender, EventArgs E ) {
FormsAuthentication.SignOut ( );
Response.Redirect ( "Login.aspx" );
}
</script>
<body>
<h3>Using Forms Authentication</h3>
<span id = "Welcome" runat=server/>
<form runat=server>
<p><input type = "submit" onServerClick = "Signout_Click" Value = "Signout"
runat = "server" />
</form>
</body>
</html>
| ||
| C# | VB | |
Check out related books at Amazon
© 2000-2008 Rey Nuñez All rights reserved.
If you have any question, comment or suggestion
about this site, please send us a note
You can help support aspxtreme