我知道这是重复的,但我无法获得可靠的解决方案(对于asp.net网页).
1.使用jquery状态码
- $.ajax({
- type: "POST",url: "stream.asmx/SomeMethod",contentType: "application/json; charset=utf-8",dataType: "json",success: function (msg) {
- //success msg
- },error: function (request,status,error) {
- if (status = 403) {
- location.href = 'login.aspx';
- }
- }
- });
问题:这返回与其他错误相同的状态代码(403),我只期望会话超时.
发送json消息会话是否过期
代码背后:
- if (!object.Equals(HttpContext.Current.Session["User"],null))
- {
- Id = int.Parse(HttpContext.Current.Session["User"].ToString());
- }
- else
- {
- result = from row in dtscrab.AsEnumerable()
- select new
- {
- redirectUrl = "login.aspx",isRedirect = true
- };
- }
在$.ajax成功:
- success: function (msg) {
- if (msg.d[0].isRedirect) {
- window.location.href = msg.d[0].redirectUrl;
- }
- else {
- //load containt
- }
- }
问题:如果会话过期,它将不会调用ajax成功行(它返回正确的json).即使这不是一个正确的方式,如果我有很多数量的ajax请求在页面(应该在全球处理).
然而,我看到这个帖子是非常好的解决方案,但它是使用AuthorizeAttribute:handling-session-timeout-in-ajax-calls的mvc
那么,我可以在asp.net web api中使用AuthorizeAttribute在mvc中使用相同的概念吗?如果没有,我可以如何解决我面临的问题(上述两个之一)?