asp.net-mvcsession-timeoutaction-filtercustom-action-filteronauthorization

Show popup from ActionFilter if session expires in mvc


We have created a website using mvc where in I have 3 controllers:

AccountController
HomeController
ContactController

We have written custom Authorization in the ActionFilters wherein we are checking whether the session has expired or not. If the session expires we are redirecting to the login page. This is the code in the ActionFilter:

public override void OnAuthorization(AuthorizationContext filterContext)
        {
            var user = filterContext.HttpContext.User;
            var UserProcesses = HttpContext.Current.Session["UserProcesses"] != null ? ((List<string>)HttpContext.Current.Session["UserProcesses"]) : null;
            var currentRequest = filterContext.ActionDescriptor.ActionName.ToLower();
            var currentController = filterContext.RouteData.Values["controller"].ToString();
            if (!string.IsNullOrEmpty(currentController))
            {
                switch (currentController.ToLower())
                {
                    case "home":
                        {
                            if (UserProcesses == null)
                            {
                                filterContext.Result = new RedirectToRouteResult(
                                        new RouteValueDictionary(
                                            new
                                            {
                                                Controller = "Account",
                                                Action = "Login"
                                            })
                                        );
                            }
                break;
         case "contact":
                        {

                            if (UserProcesses == null)
                            {
                filterContext.Result = new RedirectToRouteResult(
                                        new RouteValueDictionary(
                                            new
                                            {
                                                Controller = "Account",
                                                Action = "Login"
                                            })
                                        );
                            }
                break;

            }
        }
    }

This works perfectly i.e. when the session expires and the user clicks on any link on the website, he is redirected to the login page the address of which is

http://websitename/Account/Login

But now we want to display a popup on the page(whatever page the user is in) which will show the message "That session has expired" and on dismissing that popup, the user is then redirected to the login page.

I am unable to figure out how to call a Jquery popup from the ActionFilter itself. I have searched through many solutions on the web but couldn't find the correct solution to this. Some one suggested to use HandleUnauthorizedRequest to call an Action method (Call RedirectToAction from ActionFilter). However I am confused whether to use only HandleUnauthorizedRequest or use both this and OnAuthorization in my code. Can someone please suggest a solution?

Note: I have a common View wherein I can keep the html for popup to be used across the website.


Solution

  • I was thinking of a solution that uses a variable, and depending on that variable, you would show or not a popup. I was too lazy to actually write the code, but I've found out a link that describes a solution :

    ASP.NET MVC Authorize Attribute to launch a modal?