Database
  Home arrow Database arrow Page 4 - Database Storage with the ASP.NET Web Matr...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

Database Storage with the ASP.NET Web Matrix: Simple Login Page
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 28
    2005-09-21

    Table of Contents:
  • Database Storage with the ASP.NET Web Matrix: Simple Login Page
  • Start with the Properties pane
  • Switch to the Code view
  • Encrypting the password
  • Do you want to use a template?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Database Storage with the ASP.NET Web Matrix: Simple Login Page - Encrypting the password


    (Page 4 of 5 )

    Because we chose to encrypt the user’s password on the registration page before it was written to the database, we need to also encrypt the password when it is submitted on the login page, otherwise the values won’t match.  Add the following block of code, which is exactly the same as it was on the register page:

    Dim hashMethod as string
    hashMethod = "MD5"
    Dim encryptPassword as string
    encryptPassword =
    FormsAuthentication.HashPasswordForStoringInConfigFile
    (txtPassword.Text, hashMethod)

    We now need to create a method that passes the txtUsername value to the first SELECT statement, and a variable in which to hold the returned dataset:

    Dim userDetailsName As New System.Data.DataSet
    userDetailsName = checkUserName(txtUsername.Text)

    The userDetailsName variable will now hold a dataSet containing the matching values from the database.  We also need to get a dataSet containing both the matching usernames and passwords from the database, so also add the following code to the page:

    Dim userDetailsPass As New System.Data.DataSet

    userDetailsPass = checkUserPass(txtUsername.Text,
    encryptPassword)

    So now we have two datasets, one containing the matching Username from the database and one containing the matching Username and the encrypted Password from the database.   You need to use two datasets so that an appropriate error message can be displayed.  The first dataSet is just used to check that the user is defined in the database, while the second dataSet is used to check that the username matches the password.

    The data returned in the datasets is automatically formatted into a table by .NET, so to address the data correctly you need to specify which table in the dataSet the row holding the information resides in. There will only be one table in our datasets, and because the tables within a dataSet have zero-based indices, we will be working with table(0).  DataSets don’t have to be created by a dataAdapter reading data from a database; they can also read information from XML files, or be constructed manually.  Additionally, because the database we are using can only hold unique values, only one row will be held in the dataSet -- therefore if the Count method returns anything other than a one, it will return a zero. 

    Add the following If statement to the Code page, which checks that both dataSets hold just one record each:

    If userDetailsName.Tables(0).Rows.Count <> 1 Then
          lblUsernameError.Text = "* Username not known, please <a
     href='register.aspx'>Register</a> with us"
        Else If userDetailsPass.Tables(0).Rows.Count <> 1 Then
          lblPasswordError.Text = "* Password incorrect"
    Else
          lblWelcome.Text = "Welcome back " + txtUsername.Text
    End If

    This statement first checks that the username entered on the page exists in the dataSet; if it doesn’t, it displays an error message and links to the registration page.  If the username does exist, the statement then moves on and checks that the password entered matches the password associated with the username in the second dataSet. If it doesn’t, it displays a message advising that the password is incorrect.  If both of these conditions are true, a welcome message is displayed.  Test the page to check that it’s working properly.

    More Database Articles
    More By Dan Wellman


       · Hi everybody, this is the second in a so far four part series exploring the built...
     

    DATABASE ARTICLES

    - Excel Reference
    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client
    - ADO`s Stream Object
    - Opening a Record Object Referencing an Open ...
    - Introducing Jasper (SQL Anywhere 10 Beta)
    - Creating a Database Project in VS 2005





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT