spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / asp / quasi / 1 To page 1To page 2current page
[previous]

Vice President of Risk Technology - READY TO HIRE! (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


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"
     
style="border:1px solid #008ACA;"
/>

</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 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;"
     
/><br>
<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;"
     
/><br>
<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")
     
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">
<asp:Label
  id="lblmessage"
BorderWidth="0px"
Text="Please Log In"
     
runat="server"
/>
<br>
<asp:button id="butlogout"
  text=" Log Out "
Type="Submit"
OnClick="logoutBtn_Click"
runat="server"
style="border:1px solid #008ACA;" />
     
</form>
</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.


home / programming / asp / quasi / 1 To page 1To page 2current page
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: February 29, 2004

URL: http://webreference.com/programming/asp/quasi/1