Tuesday, September 9, 2008

Login Authentication Access Database ado.net C# Sample

Sample Login Authentication using Access Database ado.net C# asp.net 2.0.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Data.OleDb;

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

}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
bool Authenticated = false;
Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
e.Authenticated = Authenticated;
if (Authenticated == true)
{
// Response.Redirect("Default.aspx");
}

}
private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
{
OleDbConnection conDatabase = null;
OleDbDataReader Dr = null;

bool boolReturnValue = false;
string strUserID = UserName;
conDatabase = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\sites\\user.mdb");
conDatabase.Open();

string strSQL = "select * from USERS where USERID=\'" + (strUserID + "\'");

OleDbCommand cmdDatabase = new OleDbCommand(strSQL, conDatabase);
Dr = cmdDatabase.ExecuteReader();

while (Dr.Read())
{
string UserNameDB = Dr["USERID"].ToString();
string PasswordDB = Dr["PASSWORD"].ToString();

if ((UserName == UserNameDB.TrimEnd().TrimStart()) & (Password == PasswordDB.TrimEnd().TrimStart()))
{
Session["loginm"] = strUserID;
boolReturnValue = true;
break;

}
}

Dr.Close();
return boolReturnValue;
}


}

No comments: