| | |
Authenticate Username and Password
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Solved Threads: 0
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.........
Can somebody help on this please.........
•
•
Join Date: Dec 2007
Posts: 252
Reputation:
Solved Threads: 27
great example for login
http://www.daniweb.com/forums/thread6028.html
http://www.daniweb.com/forums/thread6028.html
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
ASP.NET Syntax (Toggle Plain Text)
<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script language="VB" runat="server"> sub btnLogin_Click(sender as Object, e as EventArgs) if tbPassword.length > 0 and tbUserName > 0 then Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _ "C:\Inetpub\wwwroot\Projects.mdb;" Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) 'Specify our SQL statement Dim strSQL as String = "SELECT UserPassword FROM Users WHERE UserName=?" 'Create the Command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) objCommand.Parameters.AddWithValue( "?UserName", Trim(tbUserName.Text) ) objConnection.Open() 'open the connection Dim strPassword As String = objCommand.ExecuteScalar() if (String.Compare(strPassword, Trim(tbPassword.Text), False) = 0 then 'login was a success. else 'login failed. end if else 'login failed due to no or not enough parameters end if </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
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Solved Threads: 0
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.
Thanks.
•
•
•
•
ASP.NET Syntax (Toggle Plain Text)
<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script language="VB" runat="server"> sub btnLogin_Click(sender as Object, e as EventArgs) if tbPassword.length > 0 and tbUserName > 0 then Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _ "C:\Inetpub\wwwroot\Projects.mdb;" Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) 'Specify our SQL statement Dim strSQL as String = "SELECT UserPassword FROM Users WHERE UserName=?" 'Create the Command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) objCommand.Parameters.AddWithValue( "?UserName", Trim(tbUserName.Text) ) objConnection.Open() 'open the connection Dim strPassword As String = objCommand.ExecuteScalar() if (String.Compare(strPassword, Trim(tbPassword.Text), False) = 0 then 'login was a success. else 'login failed. end if else 'login failed due to no or not enough parameters end if </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
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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:
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:
ASP.NET Syntax (Toggle Plain Text)
<asp:Button id="btnLogin" Text="Login" OnClick="btnLogin_Click" runat="server" />
ASP.NET Syntax (Toggle Plain Text)
Session("userGood") = True or Session("userID") = "18279283749" ' Then if Not Session("userGood") = True then response.redirect("/login.aspx") 'or if Session("userID").length <= 0 or Session("userID") Is Nothing then response.redirect("/login.aspx")
•
•
Join Date: Aug 2006
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
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";
}
}
}
}
}
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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!
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!
![]() |
Similar Threads
- mysql_query(): supplied argument is not a valid MySQL-Link (PHP)
- Password (Visual Basic 4 / 5 / 6)
- html/php form for .htaccess validation (PHP)
- Netgear wireless network - help! (Networking Hardware Configuration)
- Win XP Pro, how to change workgroup user (Windows NT / 2000 / XP)
- Trying to create a login system (PHP)
- help with asp.net/JS and cookies (ASP.NET)
- Help Sharing internet for a dummie (Networking Hardware Configuration)
- php help needed for login (PHP)
- DB-Authentication php/mysql on Mac OSX v3 (PHP)
Other Threads in the ASP.NET Forum
- Previous Thread: Help Needed
- Next Thread: How to get UserId from cookies?
| Thread Tools | Search this Thread |
.net 2.0 3.5 ajax alltypeofvideos appliances asp asp.net beginner box browser businesslogiclayer button c# cac checkbox class commonfunctions compatible content contenttype control countryselector courier dataaccesslayer database datagrid datagridview datalist deployment development dgv dialog dropdownlist dropdownmenu dynamic dynamically edit embeddingactivexcontrol fileuploader fill findcontrol flash flv gridview gudi iis javascript list listbox login menu microsoft mouse mssql nameisnotdeclared news novell numerical opera order panelmasterpagebuttoncontrols problem radio ratings redirect registration relationaldatabases reportemail schoolproject search security serializesmo.table sessionvariables silverlight smoobjects software sql sql-server sqlserver2005 ssl tracking treeview validatedate validation vb.net videos vista visual-studio visualstudio vs2008 web webapplications webarchitecture webdevelopment webprogramming webservice wizard xsl youareanotmemberofthedebuggerusers






