ASP.NET User Authentication/Login Form เป็นตัวอย่างการ Login Form โดยใช้ฐานข้อมูล Access บน ASP.NET Web Site และมีการนำ Session เข้ามาตรวจสอบสถานะการ Login ของ User ด้วย
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="VB" %>
<script runat="server">
Dim objConn As New OleDbConnection
Dim objCmd As New OleDbCommand
Dim strConnString,strSQL As String
Sub btnLogin_Click(sender As Object, e As EventArgs)
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
objConn.ConnectionString = strConnString
objConn.Open()
Dim intNumRows As Integer
strSQL = "SELECT COUNT(*) FROM member WHERE Username = '"& Me.txtUsername.Text &"' AND [Password] = '"& Me.txtPassword.Text &"' "
objCmd = New OleDbCommand(strSQL, objConn)
intNumRows = objCmd.ExecuteScalar()
IF intNumRows > 0 Then
Session("strUser") = Me.txtUsername.Text
Response.Redirect("Page1.aspx")
Else
Me.pnlLogin.Visible = False
Me.lblStatus.Visible = True
Me.lblStatus.Text = "Username/Password is wrong."
End IF
objConn.Close()
objConn = Nothing
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com Tutorial</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel id="pnlLogin" runat="server">
<asp:Label id="lblLogin" Text="Login." runat="server" />
<br />
<br />
<table border="1" style="width: 300px">
<tbody>
<tr>
<td>
<asp:Label id="lblUsername" runat="server" text="Username"></asp:Label></td>
<td>
<asp:TextBox id="txtUsername" runat="server" Width="120px"></asp:TextBox>
<asp:RequiredFieldValidator ID="ChkUsername" runat="server"
ControlToValidate="txtUsername" ErrorMessage="* Username"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label id="lblPassword" runat="server" text="Password"></asp:Label></td>
<td>
<asp:TextBox id="txtPassword" runat="server" Width="120px"
TextMode="Password"></asp:TextBox>
</td>
</tr>
</tbody>
</table>
<br />
<asp:Button id="btnLogin" onclick="btnLogin_Click" runat="server" Text="Login"></asp:Button>
<br />
</asp:Panel>
<asp:Label id="lblStatus" runat="server" visible="False"></asp:Label>
</form>
</body>
</html>
Logout.aspx
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Session.Abandon()
Response.Redirect("Login.aspx")
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com Tutorial</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
Page1.aspx
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
IF Session("strUser") = "" Then
Response.Redirect("Login.aspx")
Response.End
Else
Me.lblStatus.Text = "Welcome [" & Session("strUser") & "]"
End IF
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com Tutorial</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblStatus" runat="server"></asp:Label>
<br />
<br />
<asp:HyperLink id="hplLogout" NavigateURL="Logout.aspx" Text="Logout" runat="server"></asp:HyperLink>
</form>
</body>
</html>