Hi,
In my MVC application I used a class SessionTimeoutAttribute for handling session timeout. This attribute I used in all the controller. Hence when the session expires, it’s getting redirected to login page. It’s working as expected.attaching/forums/uploadfile/cb8dbf/01042023172233PM/cls.txt the code which I have used in the filter class
But now I need to show a popup message which should show the message “Your session got expired. Please login again” and after clicking on the ok button on the popup, it should redirect to login page. Can someone please help me to fix the issue?
Thanks,
Hanusha

Pasang TamangRank #449 · 5.6K reputationPosted Jan 4, 2023, 10:38 PM EST
Hi,
You need to have a code that checks loggedin user or session in one of your controller. The methods that check session should be out of SessionTimeoutAttribute. Check below code for example
var isTimout = false; function pingServer() { console.log("Ping server"); if (!isTimout) { $.ajax({ url: '/Home/IsSessionAlive', dataType: "json", async: false, type: "POST", success: function (data) { debugger; if (data.isAlive === false) { isTimout = true; $('#myModal').modal('show'); } } }); } } setInterval('pingServer()', 10000);[HttpPost] [AllowAnonymous] public JsonResult IsSessionAlive() { return Json(new { isAlive = Request.IsAuthenticated }, JsonRequestBehavior.AllowGet); }Regards,
Pasang
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 4, 2023, 10:51 PM EST
Hello,
Please refer this one,
https://www.c-sharpcorner.com/UploadFile/8911c4/session-time-out-warning-message-using-jquery-in-Asp-Net/
https://www.aspsnippets.com/Articles/Display-Session-Timeout-message-before-Session-expires-in-ASPNet.aspx
Rajesh GamiRank #30 · 41.5K reputationPosted Jan 5, 2023, 1:21 AM EST
Hi
Use the below links , definitely it will work for you
http://codemodus.blogspot.com/2016/06/aspnet-mvc-how-to-show-popup-warning.html
https://www.aspsnippets.com/Articles/Display-Session-Time-out-Warning-message-using-jQuery-in-ASPNet.aspx
https://www.codeproject.com/Questions/5301683/How-to-show-session-timeout-warning-message-in-ASP
https://haneefputtur.com/mvc-c-session-time-out-popup.html
Uday DodiyaRank #239 · 10.3K reputationPosted Jan 8, 2023, 11:23 PM EST
To show a popup message when the session expires and redirect to the login page when the user clicks on the OK button, you can modify your
SessionTimeoutAttributeclass to check for the session expiration and show the popup using JavaScript.Here's an example of how you could do this:
public class SessionTimeoutAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { HttpContext ctx = HttpContext.Current; // Check if session is supported if (ctx.Session != null) { // Check if a new session id was generated if (ctx.Session.IsNewSession) { // Check if the cookie value is the same as the session id string sessionCookie = ctx.Request.Headers["Cookie"]; if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) { if (ctx.Response.IsRequestBeingRedirected == false) { ctx.Response.Clear(); ctx.Response.Write("alert('Your session has expired. Please login again.'); window.location.href='/Account/Login';"); ctx.Response.End(); } } } } } }This will show an alert message with the specified message and redirect to the login page when the OK button is clicked.