Step 1:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
#region for session validation
Guid guid = Guid.NewGuid();
if (HttpContext.Current != null)
{
if (HttpContext.Current.Request.Cookies["ASP.NET_SessionId"] != null)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["ASP.NET_SessionId"];
cookie.Value = guid.ToString();
HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddDays(-2.0);
HttpContext.Current.Request.Cookies.Remove("ASP.NET_SessionId");
HttpContext.Current.Request.Cookies.Add(cookie);
}
}
}
Step 2: Add to Global.ascx
if you have any logout button then add it to Button Click event to remove session. otherwise only add this section to Global.ascx
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
Session.Clear();
Session.Abandon();
Guid guid = Guid.NewGuid();
if (HttpContext.Current != null)
{
if (HttpContext.Current.Request.Cookies["ASP.NET_SessionId"] != null)
{
string text1 = HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value;
HttpCookie cookie = HttpContext.Current.Request.Cookies["ASP.NET_SessionId"];
cookie.Value = guid.ToString();
HttpContext.Current.Request.Cookies.Set(cookie);
string text2 = HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value;
}
if (HttpContext.Current.Response.Cookies["ASP.NET_SessionId"] != null)
{
HttpCookie cookie2 = HttpContext.Current.Response.Cookies["ASP.NET_SessionId"];
cookie2.Value = guid.ToString();
HttpContext.Current.Response.Cookies.Set(cookie2);
string text3 = HttpContext.Current.Response.Cookies["ASP.NET_SessionId"].Value;
}
HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddDays(-2.0);
HttpContext.Current.Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddDays(-2.0);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
#region for session validation
Guid guid = Guid.NewGuid();
if (HttpContext.Current != null)
{
if (HttpContext.Current.Request.Cookies["ASP.NET_SessionId"] != null)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["ASP.NET_SessionId"];
cookie.Value = guid.ToString();
HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddDays(-2.0);
HttpContext.Current.Request.Cookies.Remove("ASP.NET_SessionId");
HttpContext.Current.Request.Cookies.Add(cookie);
}
}
}
Step 2: Add to Global.ascx
if you have any logout button then add it to Button Click event to remove session. otherwise only add this section to Global.ascx
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
Session.Clear();
Session.Abandon();
Guid guid = Guid.NewGuid();
if (HttpContext.Current != null)
{
if (HttpContext.Current.Request.Cookies["ASP.NET_SessionId"] != null)
{
string text1 = HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value;
HttpCookie cookie = HttpContext.Current.Request.Cookies["ASP.NET_SessionId"];
cookie.Value = guid.ToString();
HttpContext.Current.Request.Cookies.Set(cookie);
string text2 = HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value;
}
if (HttpContext.Current.Response.Cookies["ASP.NET_SessionId"] != null)
{
HttpCookie cookie2 = HttpContext.Current.Response.Cookies["ASP.NET_SessionId"];
cookie2.Value = guid.ToString();
HttpContext.Current.Response.Cookies.Set(cookie2);
string text3 = HttpContext.Current.Response.Cookies["ASP.NET_SessionId"].Value;
}
HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddDays(-2.0);
HttpContext.Current.Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddDays(-2.0);
}
}
No comments:
Post a Comment