Authenticate Username and Password

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 3
Reputation: Sur62 is an unknown quantity at this point 
Solved Threads: 0
Sur62 Sur62 is offline Offline
Newbie Poster

Authenticate Username and Password

 
0
  #1
Jan 16th, 2008
Iam creating a programme in Visual Web Developer 2005. In that am creating a login pager where the user has to give the username and password to enter. The username and password i have stored in Access Mdb.How to connect to database and check whether the username and password is valid?

Can somebody help on this please.........
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 252
Reputation: ProfessorPC is an unknown quantity at this point 
Solved Threads: 27
ProfessorPC ProfessorPC is offline Offline
Posting Whiz in Training

Re: Authenticate Username and Password

 
0
  #2
Jan 16th, 2008
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Authenticate Username and Password

 
0
  #3
Jan 16th, 2008
  1. <%@ Import Namespace="System.Data" %>
  2. <%@ Import Namespace="System.Data.OleDb" %>
  3. <script language="VB" runat="server">
  4. sub btnLogin_Click(sender as Object, e as EventArgs)
  5. if tbPassword.length > 0 and tbUserName > 0 then
  6. Dim connString as String
  7. connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _
  8. "C:\Inetpub\wwwroot\Projects.mdb;"
  9.  
  10. Dim objConnection as OleDbConnection
  11. objConnection = New OleDbConnection(connString)
  12. 'Specify our SQL statement
  13. Dim strSQL as String = "SELECT UserPassword FROM Users WHERE UserName=?"
  14.  
  15. 'Create the Command object
  16. Dim objCommand as OleDbCommand
  17. objCommand = New OleDbCommand(strSQL, objConnection)
  18. objCommand.Parameters.AddWithValue( "?UserName", Trim(tbUserName.Text) )
  19.  
  20. objConnection.Open() 'open the connection
  21. Dim strPassword As String = objCommand.ExecuteScalar()
  22.  
  23. if (String.Compare(strPassword, Trim(tbPassword.Text), False) = 0 then
  24. 'login was a success.
  25. else
  26. 'login failed.
  27. end if
  28. else
  29. 'login failed due to no or not enough parameters
  30. end if
  31. </script>

This performs a basic search of your mdb and grabs the password corresponding to the username. If no username exists, it will return false as strPassword will be Null or an empty string. String.Compare will return "0" if it is a success, and will return -1 or 1 if it is a failure.

Should be the simplest and quickest login you can do
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3
Reputation: Sur62 is an unknown quantity at this point 
Solved Threads: 0
Sur62 Sur62 is offline Offline
Newbie Poster

Re: Authenticate Username and Password

 
0
  #4
Jan 16th, 2008
This Code has to be written in login.aspx or in web.config file, Can you clarify please. Since am new to this if you could help me by giving the steps it would be of great help.

Thanks.


Originally Posted by SheSaidImaPregy View Post
  1. <%@ Import Namespace="System.Data" %>
  2. <%@ Import Namespace="System.Data.OleDb" %>
  3. <script language="VB" runat="server">
  4. sub btnLogin_Click(sender as Object, e as EventArgs)
  5. if tbPassword.length > 0 and tbUserName > 0 then
  6. Dim connString as String
  7. connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _
  8. "C:\Inetpub\wwwroot\Projects.mdb;"
  9.  
  10. Dim objConnection as OleDbConnection
  11. objConnection = New OleDbConnection(connString)
  12. 'Specify our SQL statement
  13. Dim strSQL as String = "SELECT UserPassword FROM Users WHERE UserName=?"
  14.  
  15. 'Create the Command object
  16. Dim objCommand as OleDbCommand
  17. objCommand = New OleDbCommand(strSQL, objConnection)
  18. objCommand.Parameters.AddWithValue( "?UserName", Trim(tbUserName.Text) )
  19.  
  20. objConnection.Open() 'open the connection
  21. Dim strPassword As String = objCommand.ExecuteScalar()
  22.  
  23. if (String.Compare(strPassword, Trim(tbPassword.Text), False) = 0 then
  24. 'login was a success.
  25. else
  26. 'login failed.
  27. end if
  28. else
  29. 'login failed due to no or not enough parameters
  30. end if
  31. </script>

This performs a basic search of your mdb and grabs the password corresponding to the username. If no username exists, it will return false as strPassword will be Null or an empty string. String.Compare will return "0" if it is a success, and will return -1 or 1 if it is a failure.

Should be the simplest and quickest login you can do
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Authenticate Username and Password

 
0
  #5
Jan 16th, 2008
that is written in an aspx file. the web.config file are just configuration settings and a place to store information you use frequently, like connection strings!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Authenticate Username and Password

 
0
  #6
Jan 16th, 2008
Put that information on an aspx file and change the connection string and your database name. Then build a form or modify the tbUserName and tbPassword elements of the sub function to the already in place textboxes on your current form. On your form, make the submit button an asp.net control and make it like the following:
  1. <asp:Button id="btnLogin" Text="Login" OnClick="btnLogin_Click" runat="server" />
Since this is a button runat the server, when you click it, it will called the sub "btnLogin_Click" which will run the code within that sub. It is up to you to do any changes you wish for when the user successfully logs in and when the user fails to login. I would suggest creating a label called "lblError" and setting the visibility to false. That way if a user fails to login, you can then add the code to the sub function (btnLogin_Click) to set the lblError.Visibility = true and lblError.Text = "Failed to login. Try again" or something. If the user successfully logs in, set their ID or something in a session. This way you can keep people out of protected areas by checking to see if the session either exists or contains certain information. An example of this would be:
  1. Session("userGood") = True
  2. or
  3. Session("userID") = "18279283749"
  4.  
  5. ' Then
  6. if Not Session("userGood") = True then response.redirect("/login.aspx")
  7.  
  8. 'or
  9. if Session("userID").length <= 0 or Session("userID") Is Nothing then response.redirect("/login.aspx")
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 20
Reputation: happy8899 is an unknown quantity at this point 
Solved Threads: 0
happy8899 happy8899 is offline Offline
Newbie Poster

Re: Authenticate Username and Password

 
0
  #7
Jan 17th, 2008
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
{
OleDbConnection myconnection = new OleDbConnection();
myconnection.ConnectionString = ConfigurationManager.ConnectionStrings["LBLDatabaseConnectionString"].ConnectionString;

OleDbCommand mycommand = new OleDbCommand();
mycommand.CommandText = "SELECT * FROM SYSTEMUSER WHERE SystemUser_Name=?";
mycommand.Parameters.Add("SystemUser_Name", OleDbType.VarChar, 255, "SystemUser_Name");
mycommand.Parameters["SystemUser_Name"].Value = this.Login1.UserName;

mycommand.Connection = myconnection;

OleDbDataAdapter myadapter = new OleDbDataAdapter(mycommand);

DataSet mydataset = new DataSet();

OleDbCommandBuilder mycommandbuilder = new OleDbCommandBuilder(myadapter);

//myadapter.Fill(mydataset);
myadapter.Fill(mydataset, "SYSTEMUSER");

if (mydataset.Tables["SYSTEMUSER"].Rows.Count == 1)
{

if (string.Compare(mydataset.Tables["SYSTEMUSER"].Rows[0]["SystemUser_Password"].ToString(), this.Login1.Password) == 0)
{
e.Authenticated = true;

if (mydataset.Tables["SYSTEMUSER"].Rows[0]["SystemUser_Type"].ToString() == "Customer")
{
Session["CustomerID"] = mydataset.Tables["SYSTEMUSER"].Rows[0]["SystemUser_ID"];
this.Login1.DestinationPageUrl = "~/Customer/ViewProfile.aspx";
}
else if (mydataset.Tables["SYSTEMUSER"].Rows[0]["SystemUser_Type"].ToString() == "Administrator")
{
Session["UserID"] = mydataset.Tables["SYSTEMUSER"].Rows[0]["SystemUser_ID"];
this.Login1.DestinationPageUrl = "~/PowerUser/PowerUserMain.aspx";
}
else if (mydataset.Tables["SYSTEMUSER"].Rows[0]["SystemUser_Type"].ToString() == "Employee")
{
Session["UserID"] = mydataset.Tables["SYSTEMUSER"].Rows[0]["SystemUser_ID"];
this.Login1.DestinationPageUrl = "~/User/ViewRewardItem.aspx";
}

}
}
}
}
Hope this can help you...
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Authenticate Username and Password

 
0
  #8
Jan 17th, 2008
Yeah, basically same concept. Mine is in VB.NET and his is in C#. Don't mix them, it won't work.

Anyway, Happy8899, two questions.. why are you using the dataset for this and why are you selecting everything in the row pertaining to that user if you are only using column[0]?

Datasets use up so much resources. a scalar would only grab one value, but that seems like you're only wanting one value anyway!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 20
Reputation: happy8899 is an unknown quantity at this point 
Solved Threads: 0
happy8899 happy8899 is offline Offline
Newbie Poster

Re: Authenticate Username and Password

 
0
  #9
Jan 23rd, 2008
Yes, thx to remind me on that really appreaciate it. I use the datasets cause i want to get all the value from the database and compare it with the login ID and password.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC