7
votes

I am hosting an Asp.Net website in IIS 6.0

We have to reset the session timeout in web.config

My client want me to reset it only if no session is running (no one is using the site).

We have not used Membership and SessionState is set to InProc

How I get to know if anybody using the site or any session is running.

I can't make change in source code or any other file except web.config in the hosted website.

5
So he wants to add a new feature, without changing the code? This isn't really possible, in any case, but you can get a best-guess by tracking activity (not sessions.) That said, the only other option is as mentioned, look at the log files. Also, since it is a shared host, maybe they have some features in the site control panel (if they gave you one) that show you the logs/activity? Perhaps call them and request something like that, or see how they can help you.Andrew Backer

5 Answers

3
votes

I'm not great at PowerShell, so hopefully you can look up the proper syntax. But ...

One option is to run a Powershell script and check the count of the session like this:

UPDATE: Changed 'Sessions Total' to 'Sessions Active'

write-host Getting performance counters ...

$perfCounterString = "\asp.net applications(__total__)\sessions active" 
$counter = get-counter -counter $perfCounterString 
$rawValue = $counter.CounterSamples[0].CookedValue 

write-host Session Count is $rawValue

if( $rawValue -gt 0)
{
   write-host Session are active - will not stop IIS
   exit
}

write-host Stopping IIS
stop-service "IISAdmin"

# Set values 
$webConfig = "d:\web.config"
$newTimeout = "20"

# Open file and change value
$doc = new-object System.Xml.XmlDocument
$doc.Load($webConfig)
$doc.SelectSingleNode("//sessionState").timeout = $newTimeout
$doc.Save($webConfig)

write-host Starting IIS
start-service "IISAdmin"
write-host Done!

Save this on the desktop as "ChangeIIS.ps1".

Now Powershell doesn't like you just running scripts like .bat files. You have to grant access to the process or your user for security reasons. So do the following:

  1. Open a command prompt and Run As Administrator
  2. Type powershell
  3. Type Set-ExecutionPolicy -scope process Bypass
  4. Type sl <path to directory of .sp1 file> . sl [set-location] is like cd in command prompt
  5. Type $ '.\ChangeIIS.ps1'

It will run now and reset the value.

Here is a link to my blog on how I created the PowerShell script in a more Step-by-step fashion

2
votes

Check the IIS log files for your site and see when the last hit was made (by actual users, rather than search bots).

If the last hit was older than the current session timeout, then no active sessions exist. That is what you are actually looking for, rather than a count.

No programming or hacks required.

1
votes

Hey Session timeout its not execuate if some one is using site..

so no need to worry

(The Timeout property specifies the time-out period assigned to the Session object for the application, in minutes. If the user does not refresh or request a page within the time-out period, the session ends.

) Session WIth IIS http://msdn.microsoft.com/en-us/library/ms525473(v=vs.90).aspx

Session with Config File http://msdn.microsoft.com/en-us/library/ms178586.aspx

1
votes

I think the simple answer here is "not possible".. You live you learn, see answer on ASP.NET Performance counters. I'm very interested on how that goes (if it works for you), and if it gives you application specific data (vs. aggregate of all applications on the box?).

Even the suggestion to look at log files will not get you what you want - as you're looking at the log file, how would you know there isn't a user starting a session?

Sessions are cookies to a browser. So if visit 1 page in your web site, I leave your website and go to some other web site, and then come back (before whatever timeout value you set)? How would you define that? At the point I left your web site, you will not see "any" activity (whatever monitor you use), but that doesn't mean my session is "expired".

It doesn't even have to be that complicated - I visit your web site, and start reading your content (not navigating, not doing anything that can be measure/monitored).

I guess there's more clarity needed on the "goal" - as in what are you after? The objective of resetting isn't the real "goal" of what you want to achieve.

Update:

I know you said you can't modify the source code, so you can hope that it implements Google Analytics. If so, you can look at the real time activity of your web site. This at least gets you "somewhere" in some trivial fashion. BUT just the same, the "magic" is based on cookies (which therefore means its still subject to the questions I posed above)

-1
votes

Keep increesing and decreesing an application variable at session start and session end events. then you will have the session count. if sesson count is zero, then you can reset the thing. all the best.