Logins Made with ASP.NET | 3
Logins Made with ASP.NET
Now, we break out of our sub and our scripts. It’s time for more HTML:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample Login Form</title>
</head>
<body style="font:11px arial;">
<form runat="server">
<asp:Label
| id="lblmessage" runat="server" |
||||
<br>
<asp:button id="butlogout"
| text=" Log Out " Type="Submit" OnClick="logoutBtn_Click" runat="server" |
/>
</form>
</body>
</html>
The only elements in our HTML are the label, the button and our form. All of which are running at the server.
Here is the full source code from this article:
Login.aspx
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat="server">
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
| If txtmembername.text = "" and txtpassword.text ="" then | ||||
| lblname.visible = true lblpass.visible = true |
||||
| else if txtmembername.text = "" then | ||||
| lblname.visible = true | ||||
| else if txtpassword.text = "" then | ||||
| lblpass.visible = true | ||||
| else | ||||
| Dim DBConn as OleDbConnection Dim dtrResults as OleDBDataReader Dim DSLogin as New DataSet DBConn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" _ & "DATA SOURCE=" _ & Server.MapPath("/logins.mdb;")) DBConn.Open() Dim CmdStr As String Dim DBSelect As New OleDbCommand CmdStr =("Select id from members Where name = @MemberName and” _ |
||||
| & “password = @Password ") | ||||
| DBSelect = new OleDbCommand(CmdStr, DBconn) | ||||
| DBSelect.Parameters.Add("@MemberName", OleDbType.VarChar, 255) | ||||
| DBSelect.Parameters.Add("@Password", OleDbType.VarChar,
255) DBSelect.Parameters("@MemberName").Value = txtMemberName.text DBSelect.Parameters("@Password").Value = txtPassword.text dtrResults = DBSelect.ExecuteReader() if dtrResults.Read() |
||||
| session("memname") = txtmembername.text session("memberID") = dtrResults("id") Response.Redirect("/success.aspx") |
| else | ||||
| lblmessage.text = "Invalid user name or password" | ||||
| end if |
End Sub
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample Login Form</title>
</head>
<body style="font:11px arial;">
<form runat="server" id="loginfrm">
<asp:Label
| id="lblmessage" Text="Please Log In" runat="server" |
<BR>
Name:<BR>
<asp:TextBox
| id="txtMemberName" Columns="30" MaxLength="30" runat="server" style="border:1px solid #008ACA;color: #788282; width:100px;" |
<asp:Label
| id="lblname" Text="Name required<br>" visible="false" runat="server" |
Password:<br>
<asp:TextBox
| id="txtPassword" Columns="31" MaxLength="30" runat="server" TextMode="Password" style="border:1px solid #008ACA;color: #788282; width:100px;" |
<asp:Label
| id="lblpass" Text="Pass Required<br>" visible="false" style="font runat="server" |
<asp:button
| id="butOK" text=" OK " OnClick="SubmitBtn_Click" runat="server" style="border:1px solid #008ACA;" |
<br>Username: Admin
<br>Pass: 123abc
</form>
</body>
</html>
Success.aspx
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
| if len(session("memberid")) = 0 then | ||||
| response.Redirect("/login.aspx") | ||||
| end if lblmessage.text = "Welcome to the site " & session("memname") _ & ". You have successfully logged in! Your member id is " _ & session("memberid") |
||||
| End Sub Sub logoutBtn_Click(Sender As Object, E As EventArgs) |
||||
| session("memname")="" session("memberid")="" response.Redirect("/success.aspx") |
||||
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample Login Form</title>
</head>
<body style="font:11px arial;">
<form runat="server">
<asp:Label
| id="lblmessage" BorderWidth="0px" Text="Please Log In" |
/>
<br>
<asp:button id="butlogout"
| text=" Log Out " Type="Submit" OnClick="logoutBtn_Click" runat="server" style="border:1px solid #008ACA;" /> |
</body>
</html>
Conclusion
In this article, you’ve been introduced to the basics of creating a login system. In the process, you’ve learned how the Asp.net session state works and how to set and retrieve variables with it. Finally, you’ve seen how to log the user out once they have completed their browsing.If you have questions or comments about this code, feel free to send them to me through email, or feel free to post them in the asp section of http://forums.webdeveloper.com.
About the Author
Nick Waters is a freelance web
developer who specializes in Asp.net. His web site is currently under renovation.
Created: March 27, 2003
Revised: February 29, 2004
URL: http://webreference.com/programming/asp/quasi/1

Find a programming school near you