Mar 04, 2010, 6:56 AM
ASP.Net Membership System - Login
Hey, After a little work I've added the asp.net membership functionality to a login window. Any help or improvements welcome
VB.Net:
C#
VB.Net:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="DriveBusiness_Tracking._Default" %>
<%@ Register assembly="Coolite.Ext.Web" namespace="Coolite.Ext.Web" tagprefix="ext" %>
<%@ Register assembly="Devart.Data.Mysql" namespace="Devart.Data.Mysql" tagprefix="ddm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As Coolite.Ext.Web.AjaxEventArgs)
'We need to determine if the user is authenticated and set e.Authenticated accordingly
'Get the values entered by the user
Dim loginUsername As String = Username.Text
Dim loginPassword As String = Password.Text
'determine if the user's username/password are valid
If Membership.ValidateUser(loginUsername, loginPassword) Then
FormsAuthentication.SetAuthCookie(loginUsername, True)
Response.Redirect("/desktop.aspx")
Else
e.Success = False
e.ErrorMessage = "Invalid Username or Password"
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="loginhead" runat="server">
<title>Login</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Login ID="Login1" runat="server" Width="312px">
<LayoutTemplate>
<ext:ScriptManager ID="ScriptManager1" runat="server" />
<ext:Window ID="LoginWindow" runat="server" Closable="false" Resizable="false" Height="150" Icon="Lock" Title="Login" Draggable="false" Width="350" Modal="true" BodyStyle="padding:5px;">
<Body>
<ext:FormLayout ID="FormLayout1" runat="server">
<ext:Anchor Horizontal="100%">
<ext:TextField ID="Username" runat="server" ReadOnly="false" FieldLabel="Username" AllowBlank="false" BlankText="Your username is required." />
</ext:Anchor>
<ext:Anchor Horizontal="100%">
<ext:TextField ID="Password" runat="server" ReadOnly="false" InputType="Password" FieldLabel="Password" AllowBlank="false" BlankText="Your password is required." />
</ext:Anchor>
<ext:Anchor Horizontal="100%">
<ext:Checkbox id="RememberMe" runat="server" ReadOnly="false" FieldLabel="Remember Me" />
</ext:Anchor>
</ext:FormLayout>
</body>
<Buttons>
<ext:Button ID="LoginButton" ValidationGroup="Login1" runat="server" CommandName="Login1.Authenticate" Text="Login" Icon="Accept">
<AjaxEvents>
<Click OnEvent="Login1_Authenticate"></Click>
</AjaxEvents>
</ext:Button>
</Buttons>
</ext:Window>
</LayoutTemplate>
</asp:Login>
</form>
</body>
</html>
C#
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="DriveBusiness_Tracking._Default" %>
<%@ Register assembly="Coolite.Ext.Web" namespace="Coolite.Ext.Web" tagprefix="ext" %>
<%@ Register assembly="Devart.Data.Mysql" namespace="Devart.Data.Mysql" tagprefix="ddm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Login1_Authenticate(object sender, Coolite.Ext.Web.AjaxEventArgs e)
{
//We need to determine if the user is authenticated and set e.Authenticated accordingly
//Get the values entered by the user
string loginUsername = Username.Text;
string loginPassword = Password.Text;
//determine if the user's username/password are valid
if (Membership.ValidateUser(loginUsername, loginPassword)) {
FormsAuthentication.SetAuthCookie(loginUsername, true);
Response.Redirect("/desktop.aspx");
}
else {
e.Success = false;
e.ErrorMessage = "Invalid Username or Password";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="loginhead" runat="server">
<title>Login</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Login ID="Login1" runat="server" Width="312px">
<LayoutTemplate>
<ext:ScriptManager ID="ScriptManager1" runat="server" />
<ext:Window ID="LoginWindow" runat="server" Closable="false" Resizable="false" Height="150" Icon="Lock" Title="Login" Draggable="false" Width="350" Modal="true" BodyStyle="padding:5px;">
<Body>
<ext:FormLayout ID="FormLayout1" runat="server">
<ext:Anchor Horizontal="100%">
<ext:TextField ID="Username" runat="server" ReadOnly="false" FieldLabel="Username" AllowBlank="false" BlankText="Your username is required." />
</ext:Anchor>
<ext:Anchor Horizontal="100%">
<ext:TextField ID="Password" runat="server" ReadOnly="false" InputType="Password" FieldLabel="Password" AllowBlank="false" BlankText="Your password is required." />
</ext:Anchor>
<ext:Anchor Horizontal="100%">
<ext:Checkbox id="RememberMe" runat="server" ReadOnly="false" FieldLabel="Remember Me" />
</ext:Anchor>
</ext:FormLayout>
</body>
<Buttons>
<ext:Button ID="LoginButton" ValidationGroup="Login1" runat="server" CommandName="Login1.Authenticate" Text="Login" Icon="Accept">
<AjaxEvents>
<Click OnEvent="Login1_Authenticate"></Click>
</AjaxEvents>
</ext:Button>
</Buttons>
</ext:Window>
</LayoutTemplate>
</asp:Login>
</form>
</body>
</html>
Hope this helps