Notifying the users that the session is expiring in a few minutes is good practice rather than logging them out directly. Let's see how to achieve this.
The "Session Timed Out" notification is very important because sometimes when a user is idle on the website, generally we get them signed out directly once the timeout occurs and the user gets surprised. There are so many articles already written on session timeout. Therefore, in this blog, I will try to explain how to create a "Session Timeout" notification using jQuery. We can configure a session timeout and time duration for notification (the time needed to show the notification before the session expires) in a web.config file.
Let's start with some coding that will explain how we can implement a "Session Timeout" notification functionality in an ASP.NET MVC application through very simple steps.
Step 1
Create an MVC application.
Step 2
Set the Session timeout and duration for notification before the actual timeout in web.config as below.
- </system.web>
- <!--Put Value in Minutes only-->
- <sessionState timeout="30"></sessionState>
- </system.web>
- <appSettings>
- <!--Put Value in Seconds only-->
- <add key="SessionExpNotice" value="60" />
- <add key="SessionExpireTime" value="1800" />
- <add key="AppPath" value="http://localhost:10334" />
- </appSettings>
Step 3
Making a class with a private constructor to get a value of app settings is a good practice so I am doing the same here.
- using System.Configuration;
- public class AppSetting
- {
- private AppSetting()
- {}
- public static int SessionExpireTime
- {
- get { return Convert.ToInt32(ConfigurationManager.AppSettings["SessionExpireTime"]); }
- }
- public static int SessionExpNotice
- {
- get { return Convert.ToInt32(ConfigurationManager.AppSettings["SessionExpNotice"]); }
- }
- public static string AppPath
- {
- get { return Convert.ToString(ConfigurationManager.AppSettings["AppPath"]); }
- }
- }
Step 4
Now, create a common JavaScript file that will be linked to the layout(Master) page.

ishwar giriPosted Jan 26, 2020, 6:59 PM
Compile time error in @appsetting.apppath <input type="hidden" id="hdnAppPath" value="@AppSetting.AppPath" /> its not working
suresh kumarPosted Oct 26, 2018, 4:02 AM
Nice article
Rushi MehtaPosted Oct 15, 2018, 11:07 PM
Nice article written can you show the output of the above code
Satyaprakash SamantarayPosted Oct 15, 2018, 5:06 PM
That's good one but can you show output for better understanding