I created a website in ASP 2.0/c# that connects to an Access 2000 database. Currently I have 4 pages that people can view without logging in, which are reports that post marathon race results. Only one maybe two people will actually ever have to log in to enter the race data.
For the login I created a form that asks for user name and password. Then I do a sql query to see if that matches the information in the database. If it does it grabs their role which would be admin.
Here is my code for this:
[code=cpp]
protected void Button1_Click(o bject sender, EventArgs e)
{
if (txtUser.Text != "" && txtPassword.Tex t != "")
{
OleDbConnection conn = new OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;data source=" + Server.MapPath( "~/App_Data/pfrpc.mdb"));
OleDbCommand cmd = new OleDbCommand("s elect RunnerID, Role from Runner WHERE UserName = @UserName and Pass = @Password", conn);
cmd.Parameters. Add("@UserName" , OleDbType.VarCh ar, 50);
cmd.Parameters["@UserName"].Value = txtUser.Text;
cmd.Parameters. Add("@Password" , OleDbType.VarCh ar, 50);
cmd.Parameters["@Password"].Value = txtPassword.Tex t;
conn.Open();
OleDbDataReader reader = cmd.ExecuteRead er();
string test = "";
if (reader.Read())
{
Session["userRole"] = reader["Role"];
Session["RunnerID"] = reader["RunnerID"];
test = Session["userRole"] + "";
if (test == "admin")
{
Response.Redire ct("RaceTimes.a spx");
}
}
else
{
lblError.Text = "Invalid Login";
}
reader.Close();
conn.Close();
}
else
{
lblError.Text = "You must fill in both the username and password fields";
}
}
[/code]
Once the session variable is equal to admin the other pages become available. By default in all of the admin pages the controls are not visible and if the session variable is equal to admin it will make the items visable. Here is a sample of one of the pages:
[code=cpp]
string test = Session["UserRole"] + "";
//verifies if user logged in
if (test == "admin")
{
txtMinutes.Visi ble = true;
txtSeconds.Visi ble = true;
txtYear.Visible = true;
ddlBibNum.Visib le = true;
ddlRace.Visible = true;
lblBibNum.Visib le = true;
lblMinutes.Visi ble = true;
lblSeconds.Visi ble = true;
lblYear.Visible = true;
lblRaceNum.Visi ble = true;
btnSave.Visible = true;
}
else
{
lblError.Text = "You must login to have access to this page!";
}
[/code]
Anyways this works on my pc through development but after I put these files on a server (which is on a free hosting server that I am using for testing) it times out really fast. After about 30 seconds to a minute my session loses that it is an admin and the pages tell me that I am not an admin anymore. Any thoughts on this? Are session variables the best way to go in this case? Hopefully I provided enough code for you to have an understanding of what I am trying to do. Any help would be much appreciated!
Thanks!
For the login I created a form that asks for user name and password. Then I do a sql query to see if that matches the information in the database. If it does it grabs their role which would be admin.
Here is my code for this:
[code=cpp]
protected void Button1_Click(o bject sender, EventArgs e)
{
if (txtUser.Text != "" && txtPassword.Tex t != "")
{
OleDbConnection conn = new OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;data source=" + Server.MapPath( "~/App_Data/pfrpc.mdb"));
OleDbCommand cmd = new OleDbCommand("s elect RunnerID, Role from Runner WHERE UserName = @UserName and Pass = @Password", conn);
cmd.Parameters. Add("@UserName" , OleDbType.VarCh ar, 50);
cmd.Parameters["@UserName"].Value = txtUser.Text;
cmd.Parameters. Add("@Password" , OleDbType.VarCh ar, 50);
cmd.Parameters["@Password"].Value = txtPassword.Tex t;
conn.Open();
OleDbDataReader reader = cmd.ExecuteRead er();
string test = "";
if (reader.Read())
{
Session["userRole"] = reader["Role"];
Session["RunnerID"] = reader["RunnerID"];
test = Session["userRole"] + "";
if (test == "admin")
{
Response.Redire ct("RaceTimes.a spx");
}
}
else
{
lblError.Text = "Invalid Login";
}
reader.Close();
conn.Close();
}
else
{
lblError.Text = "You must fill in both the username and password fields";
}
}
[/code]
Once the session variable is equal to admin the other pages become available. By default in all of the admin pages the controls are not visible and if the session variable is equal to admin it will make the items visable. Here is a sample of one of the pages:
[code=cpp]
string test = Session["UserRole"] + "";
//verifies if user logged in
if (test == "admin")
{
txtMinutes.Visi ble = true;
txtSeconds.Visi ble = true;
txtYear.Visible = true;
ddlBibNum.Visib le = true;
ddlRace.Visible = true;
lblBibNum.Visib le = true;
lblMinutes.Visi ble = true;
lblSeconds.Visi ble = true;
lblYear.Visible = true;
lblRaceNum.Visi ble = true;
btnSave.Visible = true;
}
else
{
lblError.Text = "You must login to have access to this page!";
}
[/code]
Anyways this works on my pc through development but after I put these files on a server (which is on a free hosting server that I am using for testing) it times out really fast. After about 30 seconds to a minute my session loses that it is an admin and the pages tell me that I am not an admin anymore. Any thoughts on this? Are session variables the best way to go in this case? Hopefully I provided enough code for you to have an understanding of what I am trying to do. Any help would be much appreciated!
Thanks!
Comment