How to Track Session using asp.net Csharp

4. March 2010

Author: Aamir Hasan



Create StateCollection Class;

public class StateCollection
{
private string _sessionId;
private string _path;
private string _username;
private int _pageview = 1;

public string SessionID { get { return _sessionId; } set { _sessionId = value; } }
public string Path { get { return _path; } set { _path = value; } }
public string Username { get { return _username; } set { _username = value; } }
public int PageView { get { return _pageview; } set { _pageview = value; } }
}



Create SessionTrack Class;

public static class SessionTrack
{
public static ArrayList States = new ArrayList();

static SessionTrack() { }

public static void Add(StateCollection state)
{
int StateIndex = Index(state);
if (StateIndex < 0)
States.Add(state);
else
{
int PageView = ((StateCollection)States[StateIndex]).PageView;
States[StateIndex] = state;
((StateCollection)States[StateIndex]).PageView = PageView + 1;
}
}

public static void Remove(StateCollection state)
{
int StateIndex = Index(state);
if (StateIndex >= 0)
States.RemoveAt(StateIndex);
}

private static int Index(StateCollection state)
{
for (int i = 0; i < States.Count; i++)
if (((StateCollection)States[i]).SessionID == state.SessionID)
return i;
return -1;
}
}



Create default.aspx Page;

Copy Paste Label and Repeater;

<asp:Label ID="LSTotal" runat="server" Text="Visitors: "></asp:Label>
<hr />
<table>
<tr>
<td style="width:200px;">Session ID</td>
<td style="width:100px;">User name</td>
<td style="width:100px;">Page View</td>
<td style="width:300px;">Path</td>
</tr>

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"SessionID") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "Username")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "PageView")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "Path")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>



In default.aspx.cs Page Load

protected void Page_Load(object sender, EventArgs e)
{
LSTotal.Text += SessionTrack.States.Count;

Repeater1.DataSource = SessionTrack.States;
Repeater1.DataBind();
}



Create Global.aspx Page

replace this function with Session_End if it is Empty
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.
SessionTrack.Remove(new StateCollection() { SessionID = Session.SessionID });
}


now any one who is connected to Your site will be seen in this page and also you can track of the any page.
this is how simple you can track session in your web admin site.

 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net , ,

Keep Session Alive Trick in asp.net csharp

29. January 2010

Few days ago some one ask me about his session Lose.he was working on Page filling a form and he went out and came back after 1 hour and click the button to submit the form and he saw the message timeout  when he click on button after an half hour.So i decided to write an article on Keep Session Alive.

Shortest and simple answer.

My Analysis

1) Increasing session time out for the site is bad idea in my anaylsis.

2) Build a custom control or Page that allows keeps  session  Alive for your working page.

aspx Page

Create a aspx Page e.g KeepSessionAlive.aspx

In this Page only write One Line of code in code behind.

 

 

protected void Page_Load(object sender, System.EventArgs e)
{
    Response.AddHeader("Refresh", Convert.ToString(( Session.Timeout * 60 ) -120 ));
}

 

Just add Refesh Header Attribute to the webForm

 

Now goto your Master Page and add this line of code given below

 

<IFRAME id=Defib src="/SessionKeepAlive.aspx"
     frameBorder=no width=0 height=0 runat="server"></IFRAME>

 

This iframe will be invisible in the webform.This iframe will call back  to server side 60 second before session timeout to keep session from expiring.

 

 

 

 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net ,

why minutes session expires with in 3-6 mintues

9. January 2010

hi every one good morning from Aamir Hasan A real program Hacker

when recycle worker process to expire the session there are many reasons.

1- Modify the web.config file or

2- Replace files in the bin folder at runtime

3- Delete folders from inside the application root folder at runtime.

4- Virus scan on the server.

5- Memory leaks

6- IIS log files?

The location of IIS log files is below

Windows Xp:
C:\WINDOWS\system32\Logfiles\W3SVC1

Windows NT/2000:
C:\WINNT\system32\Logfiles\W3SVC1

#Software: Microsoft Internet Information Services 6.0
#Version: 1.0
#Date: 2009-10-22 01:47:01
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) sc-status sc-substatus sc-win32-status

 

Debug Diagnostics 1.1 from Microsoft

http://www.microsoft.com/downloads/details.aspx?FamilyID=28bd5941-c458-46f1-b24d-f60151d875a3&DisplayLang=en

 

Author:Aamir Hasan


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net ,

Keeping ASP.NET Session

8. January 2010

Jquery Simple call the ashx page

function setHeartbeat() {
    setTimeout
("heartbeat()", 300000); // every 5 min
}

function heartbeat() {
    $
.get(
       
"/SessionIsLive.ashx",
       
null,
       
function(data) {
           
//$("#heartbeat").show().fadeOut(1000); // just a little "red flash" in the corner :)
            setHeartbeat
();
       
},
       
"json"
   
);

class
public class SessionIslive : IHttpHandler, IRequiresSessionState
{
   
public bool IsReusable { get { return false; } }

   
public void ProcessRequest(HttpContext context)
   
{
        context
.Session["Heartbeat"] = DateTime.Now;
   
}
}

JSON serializd return Object if data is avaible.web config
<httpHandlers>
   
<add verb="GET,HEAD" path="SessionHeartbeat.ashx" validate="false" type="SessionHeartbeatHttpHandler"/>
</httpHandlers>



Author:Aamir Hasan


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, asp.net 4.0 ,



User Name: Guest

Your Ip: 38.107.191.91
Time: