I want to execute a code that will insert in the DB something like "Session expire" to track the users. However, my session timeout is in the Web.config
.
<system.web>
<compilation debug="true" targetFramework="4.0" />
<sessionState timeout="20" mode="InProc" />
</system.web>
<defaultDocument>
<files>
<add value="Login.aspx" />
</files>
</defaultDocument>
Then I want to execute this code.
if (HttpContext.Current.Session["usersId"] == null && HttpContext.Current.Session["usersRole"] == null && HttpContext.Current.Session["usersDivision"] == null && HttpContext.Current.Session["notification"] == null)
{
string connstr = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmdlog = new SqlCommand("INSERT INTO Logs (logs_info,logs_date,users_id) VALUES (@info,@date,@id)", conn);
cmdlog.Parameters.AddWithValue("@info", "User " +sessionName+ " session timed out.");
cmdlog.Parameters.AddWithValue("@date", DateTime.Now);
cmdlog.Parameters.AddWithValue("@id", HttpContext.Current.Session["usersId"].ToString());
cmdlog.ExecuteNonQuery();
conn.Close();
HttpContext.Current.Response.Redirect("../Login.aspx");
}
How can I execute that code before the timeout?