how to add session abandon features in global.asax file

Posted by Shanky11 under ASP.NET on 9/24/2013 | Points: 10 | Views : 2289 | Status : [Member] | Replies : 4
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
Session.Abandon();

}

i have written this code but after closing browser even i am getting the last session value . i want to destroy them




Responses

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Try this instead of Session.abandon()

Session.Add("MySession", Value);
Session.Remove("MySession");


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
This one... ?

<script language="C#" runat="server">
public void Application_OnStart()
{
Application["UsersOnline"] = 0;
}

public void Session_OnStart()
{
Application.Lock();
Application["UsersOnline"] = (int)Application["UsersOnline"] + 1;
Application.UnLock();
}

public void Session_OnEnd()
{
Application.Lock();
Application["UsersOnline"] = (int)Application["UsersOnline"] - 1;
Application.UnLock();
}
</script>


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
When you call the Session.Abandon method, it will cancle the current session, namely the current is not valid and a new session can be started. However, that don't means the sessionid has been reset, the subsequent request starts a new session and still uses the same sessionid. This behavior occurs due to the Session.Abandon method just disable the current session but not delete the cookie storing session identifier(sessionid). If you want to reset the sessionid after calling the Session.Abandon method, you just need to set like this:
Session.Abandon();

Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));

Note: ASP.NET_SessionId is the default name of cookie storing the session identifier.


REFERENCE:
http://www.codeproject.com/Questions/552949/WhenplusweplususeplusSession-ClearplusandplusSessi

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
To incremnet the variable while page loading :

Create this property within the page

public int TestCounter
{
get
{
int iCounter = 0;
if (Session["TestCounter"] != null)
{
iCounter = (int)Session["TestCounter"];
}
return iCounter;
}


set
{
Session.Add("TestCounter", value);
}
}

Then on your page_load event (page load example)

protected void Page_Load(object sender, EventArgs e)

{
TestCounter++;

}


To retrieve the value of the counter =

int iPageLoadCount = TestCounter;



Reference:
http://msdn.microsoft.com/en-us/library/ms952597.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response