I want show some message to user when his session time out.
How can I do that?
You can't. At least, you can't make the client browser respond to its server
session timing out. What you can do, however, is do repond to the first
action of a client browser after their session has timed out - there's a big
difference.
E.g. if the session timeout is 20 minutes (the default for IIS), and the
client browser is idle, after 20 minutes the session will time out. However,
nothing will happen on the client browser until they next try to make a
round trip to the server - this could be hours and hours after their session
has timed out.
However, what I usually do to cater for this is:
1) Create an HTML (not ASPX) page in the web root called e.g.
sessionTimedOut.htm, with the following content:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script>
alert('You have been idle for too long. You must log in again.');
top.location.href = 'home/';
</script>
</head>
</html>
Obviously, replace the top.location.href='home/'; line with wherever you
want the users to end up, typically the login screen...
2) At the top of the OnInit code of every ASPX page where you want to trap a
session timeout (typically, every page apart from the home page), add the
following:
if(Session.IsNewSession)
{
Response.Redirect("~/sessionTimedOut.htm", false);
return;
}
Basically, this is saying that this page is not the home page so, if it's
being accessed in a new session, the browser must have timed out.