2012-07-19 3 views
0

$ .ajax를 사용하여 컨트롤러에서 데이터를 가져 오는 MVC 응용 프로그램이 있습니다.JQuery 및 MVC 세션 시간 초과 리디렉션

세션 시간 초과가 발생하면 페이지가 로그인 페이지로 리디렉션되지 않습니다. 누군가가이 문제를 도울 수 있습니까?

감사합니다.

+0

당신이 세션 시간이 초과 할 때 당신이 얻을 아약스 응답으로 우리를 제공 할 수 있습니다 : 아약스에서

AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public class MyAuthorizeAttribute : AuthorizeAttribute { protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result = new JsonResult { Data = new { // put whatever data you want which will be sent // to the client message = "sorry, but you were logged out" }, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { base.HandleUnauthorizedRequest(filterContext); } } } 

는이 작업을 수행 할 수 있습니까? –

답변

0

기본적으로 페이지는 컨트롤러에게 로그인 클라이언트에서 리디렉션 특정 시간 제한/인증 오류를 잡으려고이 오류를 기반으로 할 수 리디렉션 가정하면

여기

이 문서

http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx에서 가져온 예

$.ajax({ 
    url: "/yourservlet", 
    data: { }, 
    complete: function(xmlHttp) { 
    if(xmlHttp.status==401) 
     { 
    // redirect to login page 

    } 
    } 
}); 
2

당신은 사용자 정의를 작성할 수 [권한 부여] 대신 클라이언트 스크립트를 처리 할 수 ​​있도록 할 권한이없는 액세스의 경우 401 예외를 던지는 JSON을 반환 속성 정상적으로 시나리오 :

$.get('@Url.Action("SomeAction")', function (result) { 
    if (result.message) { 
     alert(result.message); 
     window.location.reload(); 
    } else { 
     // do whatever you were doing before with the results 
    } 
}); 
+0

코드 샘플의 첫 번째 줄에 왼쪽 대괄호가 없어서 다음과 같아야합니다. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]] – ChrisP