Login Control using Microsoft Access and C# [ASP.NET]
I'm having trouble with some of the login controls for my website. I've successfully set it up so when a user registers it enters the details into an Access database.
I'm now trying to get the login to work but can't see how to implement this.
I've had a go at some code but I'm not sure what I need to add to it to get it working
Here's My code
[CODE]using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection a = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=I:/skylimos/App_Data/limohire.mdb");
string dbcommand = "SELECT * FROM customer_TBL WHERE [email] = '" + TextBox1.Text + "' AND [password] = '" + TextBox2.Text + "'";
OleDbDataAdapter b = new OleDbDataAdapter(dbcommand, a); DataSet dset = new DataSet();
b.Fill(dset);
Response.Redirect("Register.aspx");
}
}
[/CODE]
Any help is appreciated, cheers
Purushottam RathorePosted Dec 31, 2009, 1:26 AM
Try this code:
Login.aspx:
Login.aspx.cs:
public partial class Home_Login : System.Web.UI.Page
{
public DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
ctlLogin.Focus();
}
protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
bool Authenticated = false;
CheckBox chBox = (CheckBox)ctlLogin.FindControl("RememberMe");
Authenticated = UserAuthenticate(ctlLogin.UserName, ctlLogin.Password);
e.Authenticated = Authenticated;
if (Authenticated == true)
{
Session["UserId"] = ctlLogin.UserName.ToString();
FormsAuthentication.Initialize();
if (chBox.Checked == true)
{
Response.Cookies["UID"].Value = ctlLogin.UserName;
Response.Cookies["PWD"].Value = ctlLogin.Password;
Response.Cookies["UID"].Expires = DateTime.Now.AddMonths(3);
Response.Cookies["PWD"].Expires = DateTime.Now.AddMonths(3);
}
Response.Redirect("../Default.aspx");
}
}
private bool UserAuthenticate(string UserName, string Password)
{
bool boolReturnValue = false;
ds = new DataSet();
ds = GetUserLogin(UserName, Password);
if (ds.Tables[0].Rows.Count > 0)
{
boolReturnValue = true;
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket
(1, // Ticket version
ctlLogin.UserName, // Username associated with ticket
DateTime.Now, // Date/time issued
DateTime.Now.AddMinutes(30), // Date/time to expire
false, // "true" for a persistent user cookie
ds.Tables[0].Rows[0][0].ToString(), // User-data
FormsAuthentication.FormsCookiePath);
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName, // Name of auth cookie
hash); // Hashed ticket
// Set the cookie's expiration time to the tickets expiration time
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
// Add the cookie to the list for outgoing response
Response.Cookies.Add(cookie);
}
return boolReturnValue;
}
public DataSet GetUserLogin(string UserId, string Password)
{
CommonHelper cmhlp = new CommonHelper();
Database db = new Database();
IDbConnection conn = cmhlp.Connection();
//int Id;
string MyUserId;
string sql = "Select * from Registration where UserId ='" + UserId + "' and [Password] = '" + Password + "'";
IDbDataAdapter da = db.CreateDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
}