weixin_33705053 2014-03-02 17:12 采纳率: 0%
浏览 92

注销MVC Ajax中的视图

I have build a website using MVC4 razor, I am using two kind of action link for redirecting a view. " @Ajax.ActionLink " and "@Html.ActionLink ". I have a separate logout view, Issue is if i am on a views which is calling as @Ajax.Action link and session expired then my logout page is opened inside to the main layout view. I want to open a log out view in a separate view the same thing is happened whenever i use partial view.

Please let me know how can i fix this issue either using ajax.actionlink or partial view logout page will open on a separate view.

Thanks in advance..

  • 写回答

1条回答 默认 最新

  • weixin_33728708 2014-03-03 04:49
    关注

    What you will need to do involves some server side and client side coding.

    Firstly on the server side you will need to capture requests where the users session has expeired AND they are using ajax.

    If you are using the standard Authentication attribute you can extend it like this:

     public class AjaxAuthorizeAttribute : AuthorizeAttribute
        { 
            private class Http401Result : ActionResult
            {
                public override void ExecuteResult(ControllerContext context)
                { 
                    context.HttpContext.Response.StatusCode = 401;
                    context.HttpContext.Response.Write("Your session has expired. Please login again to continue.");
                    context.HttpContext.Response.End();
                }
            }
    
            protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new Http401Result();
                }
                else
                {
                    base.HandleUnauthorizedRequest(filterContext);
                }
            }
    
        }
    

    Use that attribute instead of Authorize on your Controller or Action.

    Now to the client side, when a request comes back from the server with a 401 response you want to capture it and do something, lets use a Ajax Setup for this:

    Stick this someone it will always run:

    $(document).ready(function () {
    
        $.ajaxSetup({
            cache: false
        });
    
        $(document).ajaxError(
            function (e, request) {
                if (request.status == 401) {
                    alert("Your session has expired. Please login again to continue.");
                    window.location = "/account/logon"; 
                } else {
    
                }
            }
        );
    }
    

    And youre done.

    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题