Search code examples
javascriptjqueryasp.netasp.net-identity-3

How to give a notice message before redirect to login view when use Asp.Net Identity?


I have used the Asp.Net Identity framework in my app.There is a need, when the session expires give a prompt message, and then jump to the login page instead of directly jump to the login page.Prompt information using custom styles. Because my app's left menus load the view with ajax,so I overried the AuthorizeAttribute.HandleUnauthorizedRequest methord to return a json.Now when users click left menus, it can work properly.But if users refresh the page by click F5,the page will still jump directly to the login page.

I have already overrided AuthorizeAttribute.HandleUnauthorizedRequest

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        var httpContext = filterContext.HttpContext;
        string sintab = httpContext.Request["inTab"];            

        if (!string.IsNullOrEmpty(sintab) && bool.Parse(sintab))
        {
            var result = new JsonResult();
            result.Data = new
            {
                Authorize = false,
                Url = LOGIN_URL
            };
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            filterContext.Result =result;
            return;
        }
        if (filterContext.Controller.GetType() != typeof(Controllers.HomeController) && 
            !filterContext.ActionDescriptor.ActionName.Equals("Index", StringComparison.OrdinalIgnoreCase))
        {
            string returnUrl = "/" + filterContext.Controller.GetType().Name.Replace("Controller","") + "/Index" ;
            returnUrl = httpContext.Server.UrlEncode(returnUrl);
            httpContext.Response.Redirect("~/Account/Login?ReturnUrl="+returnUrl);
            return;
        }

        base.HandleUnauthorizedRequest(filterContext);
    }

The code of left menus' loadView js

        $.get(url, null, function (html) {
        html = html.replace(/#%/g, "\"").replace(/%#/g, "\"");
        var json;
        try {
            json = eval("(" + html + ")");
        } catch (e) {

        }
        if (json && !json.Authorize) {
            // give an message
            layer.alert("Session timeout, please re login.", function (index) {
                window.location.href = json.Url + "?returnurl=" + encodeURI(hash);
            });
        }
        else {
            $("#content").empty().html(html);
            _initModalButton();
            $("#content").show();
        }

    }, 'html');

The page looks like this image enter image description here

I want to know if there are some better ways to do this because there are a lot of other button need to check authorize status and show message before jump to the login page,and how to give the message when users refresh the page?

Thanks very much!


Solution

  • I think you're looking for are Global Ajax Events. Please, check this, I think this make your job easier.