Wednesday, January 9, 2008

Session State (Web.Config)

Does anyone know how to programmatically change the web.config's timeout property for Forms Authentication?

I have a few different seminar types and only one of them gets a 30 minute timeout the others are 60 minute timeouts.

I tried this:

if (seminarcode == 2)
{
Session.Timeout = 1; (for testing purposes)
}

This only sets it so that the page timesout after 1 minute of no use where I want to make everything cease after a minute no matter what the user does.

I thought I had the answer but it appears I do not.

PLEASE HELP!

also... what does "hth" mean?

Dave

3 comments:

Tiger said...

I found this:
http://forums.asp.net/t/1201816.aspx

Hope it will help.

Bills Fan Dave said...

Thanks Jinghua,

However, I saw that what that guy is doing, I was doing as well. In his case, once his session times out, after one minute of inactivity, (it defaults to 20 min, correct me if im wrong!) !isPostback == false, which would allow the session text ("test") to be given to the Session["test"] making it not null and then resetting the Session.Timeout to 1 minute.

I just want to make the program redirect after 1 minute (testing only) regardless of activity by the user.

Can this happen?

Dave

Bills Fan Dave said...

I just wanted to thank Jinghua for coming over and brainstorming with me and helping me come up with a way to do this:

We figured out that it was not the Session that was in question at all.

The first idea was to use a javascript timer or maybe a System.Threading "Sleep" timer.

Then Jinghua came up with a great idea and that was to check the start time of the Seminar (in the database) and go from there.

So what I did was:

protected bool getTimeLimit(int seminarid, int sessionid)
{
bool timesup = false;

DateTime currtime = DateTime.Now;
DateTime semtime = new DateTime();
TimeSpan ts = new TimeSpan();

if (sessionid == 2)
{
ts = new TimeSpan(0, 30, 0);
}
else
{
ts = new TimeSpan(0, 60, 0);
}
string sql = "SELECT sesstime FROM seminar WHERE id = " + seminarid + "";
using (SqlConnection conn = new SqlConnection(dsn))
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
{
semtime = rdr.GetDateTime(0);
}
rdr.Close();
conn.Close();
}
if (semtime.Add(ts) < currtime)
{
timesup = true;
}

return timesup;
}

and if that returns true... then Time is Up and I redirect them to a page that tells them that they are no longer able to sign up for this Seminar.