This code sample covers many areas of programming in ASP.Net along the way of showing how to create a login page/signup page for your web site.
There are multiple placeholders in the code sample. Each one is visible or invisible, depending on which function is desired at the time. The Sign up section is in one placeholder, where the actual login section is in another. Linkbuttons are used to hide or show each one as needed and if the login is successful – a session is started. On each subsequent page, the code is then shown on how to use the session to make sure the person has logged in successfully.
There are two pages you’ll need to create on your system. It doesn’t really matter what you name the first one, but if you copy the code as is, you’ll need to name the second page ‘logintutorial2.aspx’. The table that was created to hold the data is called ‘logintutorial’ and has the following fields:
id (identity), fname, lname, email, uid, pwd
Changes for using this with MS Access (OleDb Managed Provider) are as follows:
Change the Namespace Imports to:<br?
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>
Then, the two Subs (doSignup and doLogin) would have these changes:
Sub doSignup(Source as Object, E as EventArgs)
Dim strFname as string=frmfname.text
Dim strLname as string =frmlname.text
Dim strEmail as string =frmemail.text
Dim struid as string =frmuid.text
Dim strpwd as string =frmpwd.text
Dim MySQL as string = "Insert into logintutorial (fname, lname, email, uid, pwd) values ('" & _
strfname & "', '" & strlname & "', '" & strEmail & "', '" & struid & "', '" & strpwd & "')"
Dim myConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("\loginTutorial.mdb") & ";")
Dim Cmd as New OleDbCommand(MySQL, MyConn)
MyConn.Open()
cmd.ExecuteNonQuery
label1.text = "
It's Done!"
End Sub
Sub dologin(Source as Object, E as EventArgs)
Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("\loginTutorial.mdb") & ";"
Dim MySQL as string = "Select uid, pwd, fname from loginTutorial " & _
"Where uid = '" & lguid.text & "' and pwd = '" & lgPWD.text & "'"
Dim MyConn as New OledbConnection(strConn)
Dim objDR as OleDbDataReader
Dim Cmd as New OleDbCommand(MySQL, MyConn)
MyConn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
if Not objDR.Read() then
label1.text = "Sorry - your user ID & password were not found"
objDR.Close
Myconn.Close
else
objDR.close
Myconn.Open
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
While objDR.Read()
strFname=objDR("fname")
End While
Session("ok") ="yes"
session("Fname")=strFname
ph3.visible="true"
ph2.visible="false"
End If
End Sub
Download the MS Access Database to test with:
Logintutorial.zip
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.1">
<title>Login Tutorial</title>
<script language="VB" runat="server">
Dim strFname as string
Sub doSignup(Source as Object, E as EventArgs)
Dim strFname as string=frmfname.text
Dim strLname as string =frmlname.text
Dim strEmail as string =frmemail.text
Dim struid as string =frmuid.text
Dim strpwd as string =frmpwd.text
Dim MySQL as string = "Insert into logintutorial (fname, lname, email, uid, pwd) values (‘" & _
strfname & "’, ‘" & strlname & "’, ‘" & strEmail & "’, ‘" & struid & "’, ‘" & strpwd & "’)"
Dim myConn As SqlConnection = New SqlConnection(YourConnectionString)
Dim Cmd as New SQLCommand(MySQL, MyConn)
MyConn.Open()
cmd.ExecuteNonQuery
label1.text = "<p>It’s Done!"
End Sub
Sub newsignup(Source as Object, E as EventArgs)
ph1.visible="true"
ph2.visible="false"
ph3.visible="false"
label1.text=""
End Sub
Sub showlogin(Source as Object, E as EventArgs)
ph2.visible="true"
ph1.visible="false"
label1.text=""
End Sub
Sub dologin(Source as Object, E as EventArgs)
Dim strConn as string = "server=YourServer;uid=uid;pwd=yourpwd;database=yourdb"
Dim MySQL as string = "Select uid, pwd, fname from loginTutorial " & _
"Where uid = ‘" & lguid.text & "’ and pwd = ‘" & lgPWD.text & "’"
Dim MyConn as New SQLConnection(strConn)
Dim objDR as SQLDataReader
Dim Cmd as New SQLCommand(MySQL, MyConn)
MyConn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
if Not objDR.Read() then
label1.text = "<i>Sorry – your user ID & password were not found</i>"
objDR.Close
Myconn.Close
else
objDR.close
Myconn.Open
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
While objDR.Read()
strFname=objDR("fname")
End While
Session("ok") ="yes"
session("Fname")=strFname
ph3.visible="true"
ph2.visible="false"
End If
End Sub
</script>
</head>
<body>
<form Name="frmSignup" runat="server">
<asp:LinkButton id="login" Text="Login Here" onclick="showlogin"
causesvalidation="false" runat="server" /><br>
<asp:LinkButton id="signup" Text="If you don’t have an account – sign up here"
causesvalidation="false" onclick="newsignup" runat="server" />
<p>
<asp:placeholder ID="ph1" visible="false" runat="server">
<table>
<tr>
<td align="right"><b>First Name:</b> </td>
<td> <asp:textbox id="frmfname" runat="server" />
<asp:RequiredFieldValidator runat="server" id="vldfname"
ControlToValidate="frmfname" ErrorMessage="fname is required" display="Dynamic">
The fname field is Required!
</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td align="right"><b>Last Name: </b></td>
<td> <asp:textbox id="frmlname" runat="server" />
<asp:RequiredFieldValidator runat="server" id="vldlname"
ControlToValidate="frmlname" ErrorMessage="lname is required" display="Dynamic">
The lname field is Required!
</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td align="right"><b>Email</b></td>
<td> <asp:textbox id="frmemail" runat="server" />
<asp:RequiredFieldValidator runat="server" id="vldemail"
ControlToValidate="frmemail" ErrorMessage="email is required" display="Dynamic">
The email field is Required!
</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td align="right"><b>UserID</b> (10 char max):</td>
<td> <asp:textbox id="frmuid" runat="server" />
<asp:RequiredFieldValidator runat="server" id="vlduid"
ControlToValidate="frmuid" ErrorMessage="uid is required" display="Dynamic">
The uid field is Required!
</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td align="right"><b>Password </b>(10 char max):</td>
<td> <asp:textbox id="frmpwd" runat="server" />
<asp:RequiredFieldValidator runat="server" id="vldpwd"
ControlToValidate="frmpwd"
ErrorMessage="pwd is required" display="Dynamic">
The pwd field is Required!
</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td align="right">
<asp:button id="button1"
Text="Submit" onclick="doSignup" runat="server" /></td>
<td></td>
</tr>
</table>
</asp:placeholder>
<asp:placeholder ID="ph2" visible="false" runat="server">
<b>Login:</b><br>
<table border="0"> <tr>
<td align="right" valign="Top"><b>User ID: </b></td>
<td align="Left" valign="Top"><asp:TextBox id="lguid" runat="server" /></td>
</tr>
<tr>
<td align="Right" valign="Top"><b>Password: </b></td>
<td align="Left" valign="Top"><asp:TextBox id="lgpwd" runat="server" /></td>
</tr>
<tr>
<td align="Right" valign="Top" Colspan="2">
<asp:Button id="buttonlg" Text="Login" onclick="dologin" runat="server" /></td>
</tr>
</table>
</asp:placeholder>
<asp:placeholder ID="ph3" visible="false" runat="server">
<A HREF="logintutorial2.aspx">Go to Next Page</A>
</asp:placeholder>
</form><asp:Label ID="label1" runat="server" />
</body>
</html>
Second Page:
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.1">
<title>Second page – Login Tutorial</title>
<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
if session("ok") <> "yes" then
label1.text="Sorry – you’re not authorized to be here<br><a href=""logintutorial.aspx"">Go to the login page</a>"
else
label1.text="Welcome to our site, " & session ("fname") & "<p>"
label1.text+="<b><i>You’re authorized to be here</i></b>"
End If
End Sub
</script>
</head>
<body>
<asp:Label ID="label1" runat="server" />
</body>
</html>