Tuesday, May 25, 2010

Login Control

Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8"
BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
Font-Size="0.8em" ForeColor="#333333">
<TextBoxStyle Font-Size="0.8em" />
<LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em"
ForeColor="White" />
</asp:Login>

</div>
</form>
</body>
</html>

Default.aspx.vb

Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page


Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
'Read the xml file
'Check to see if anything in the file matches the username
'and password in the login
'if it does authenticate and redirect
'if not make them reenter
e.Authenticated = False

Dim ds As New DataSet
ds.ReadXml(MapPath("login.xml"))

For Each row As DataRow In ds.Tables(0).Rows
If Login1.UserName = row("username").ToString AndAlso Login1.Password = row("password") Then
e.Authenticated = True
Session("username") = Login1.UserName
Response.Redirect("Default2.Aspx")
End If
Next

End Sub
End Class

Default2.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Welcome</h1>

</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>

Default2.aspx.vb


Partial Class Default2
Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("username") = Nothing Then
Response.Redirect("Default.aspx")
End If

Label1.Text = Session("userName")
End Sub
End Class

login.xml


<?xml version="1.0" encoding="utf-8" ?>

<calogins>

<calogin>
<username>jones</username>
<password>pa$$w0rd</password>
</calogin>

<calogin>
<username>Smith</username>
<password>pa$$w0rd</password>
</calogin>

</calogins>

No comments:

Post a Comment