我想执行一个代码,它将在数据库中插入类似“Session expire”的内容来跟踪用户。但是,我的会话超时位于Web.config
。c# - 在超时前执行代码
<system.web>
<compilation debug="true" targetFramework="4.0" />
<sessionState timeout="20" mode="InProc" />
</system.web>
<defaultDocument>
<files>
<add value="Login.aspx" />
</files>
</defaultDocument>
然后我想要执行此代码。
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");
}
如何在超时之前执行该代码?
**警告**您的代码易受sql注入攻击! –
请参阅[本文](https://stackoverflow.com/questions/1413407/how-to-get-notified-of-session-end)了解一些见解。 – FrankerZ
@ DanielA.White嘿哟先生白。我认为有参数是好的,你可以分享为什么我的代码是脆弱的? – Wowie