一、使用MVC中的Filter来对Session进行验证

(1)方法1:

public class MyAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
var loginUser = filterContext.HttpContext.Session["User"];
//When user has not login yet
if (loginUser == null)
{
var redirectUrl = ConstantProvider.LoginURL + "?RedirectPath=" + filterContext.HttpContext.Request.Url;
filterContext.Result = new RedirectResult(redirectUrl);
return;
} }
}

(2)方法二:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace GiftWeb.Controllers
{ public class BaseController : Controller
{ protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext); if (filterContext.HttpContext.Session["sysuserInfo"] == null)
{ string currentUrl = filterContext.HttpContext.Request.RawUrl; currentUrl = HttpUtility.UrlEncode(currentUrl); filterContext.HttpContext.Response.Redirect("/Login/Index?returnUrl="+currentUrl);
} } }
}

二、对于Ajax请求的中,Session失效的处理

Ajax请求中,如果遇到session过期,使用上面的方法是不能够达到效果的。实现的思路是,如果发现是Ajax请求,就返回 特定格式的Json数据 ,客户端对于这个数据进行处理,发现有Session失效的情况,就跳转到登录页面。

首先,扩展我们的MyAuthorizeAttribute

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class MyAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
var loginUser = filterContext.HttpContext.Session["User"];
//When user has not login yet
if (loginUser == null)
{
var redirectUrl = ConstantProvider.LoginURL + "?RedirectPath=" + filterContext.HttpContext.Request.Url;
if(!filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result = new RedirectResult(redirectUrl);
}
else
{
filterContext.Result = new JsonResult
{
Data = new
{
Success = false,
Message = string.Empty,
Redirect = redirectUrl
}
};
}
return;
}
}

上面判断如果请求是来自于Ajax, 就返回一个Json Result,客户端处理的代码如下:

 $.ajax({
type: "POST",
url: "@Url.ContactInfoAjax()",
success: function (msg) {
if (msg.Success) {
…….
}
if (msg.Redirect) {
window.location = msg.Redirect;
}
}
});

三、处理Session过期异常的重要性

说实话,开发人员开始项目的时候,往往关注在代码实现的功能上面,也就是程序"正常 "工作时候的状态关注的更多,而对于" 非正常 "情况下,考虑的很少。

而对于这些方面的考虑,才能让我们成为更加专业的程序员,摆脱菜鸟的处境。

Asp.net MVC Session过期异常的处理的更多相关文章

  1. [转]菜鸟程序员之Asp.net MVC Session过期异常的处理

    本文转自:http://www.cnblogs.com/JustRun1983/p/3377652.html 小赵是刚毕业的计算机专业方面的大学生,4年的大学时间里面,他读过了很多编程方面的数据,也动 ...

  2. 菜鸟程序员之Asp.net MVC Session过期异常的处理

    小赵是刚毕业的计算机专业方面的大学生,4年的大学时间里面,他读过了很多编程方面的数据,也动手也了很多代码.现在毕业了,他如愿的加入了T公司,开始了自己的程序员生涯.他信心满满,相信自己4年的学习到的东 ...

  3. ASP.NET MVC Session 过期验证跳转至登入页面

    一.在要检查登入的控制器上继承 CheckLoginController 类 2. CheckLoginController 类的写法 using System; using System.Colle ...

  4. 判断asp.net中session过期的方法

    判断asp.net中session过期的方法 转载自:http://www.cnblogs.com/xilipu31/archive/2013/04/12/3016830.html 方法一:最麻烦也是 ...

  5. asp中设置session过期时间方法总结

    http://www.jb51.net/article/31217.htm asp中设置session过期时间方法总结 作者: 字体:[增加 减小] 类型:转载   asp中默认session过期时间 ...

  6. MVC session过期如何处理跳转

    以前我们总是会写一个基类也叫父类来判断session是否已过期然后跳转到指定的错误页面或者登陆界面,然后让所有的页面都继承这个基类,但是当我们应用到MVC项目中时,发现该方法并不会起作用.这时我们可以 ...

  7. 《Pro ASP.NET MVC 4》异常整理

    最近在和同学一起研究毕业设计,准备模仿<Pro ASP.NET MVC 4>里面的SportsStore设计模式和测试驱动开发. 由于和书中VS版本不同,发现不少问题,在此总结. 用户代码 ...

  8. Asp.NET设置Session过期时间的四种方式

    在Asp.net中,可以有四处设置Session的过期时间: 一.全局网站(即服务器)级 IIS-网站-属性-Asp.net-编辑配置-状态管理-会话超时(分钟)-设置为120,即为2小时,即120分 ...

  9. asp.net中Session过期设置方法

    在Asp.net应用中,很多人会遇到Session过期设置有冲突.其中,可以有四处设置Session的过期时间: 一.全局网站(即服务器)级 IIS-网站-属性-Asp.net-编辑配置-状态管理-会 ...

随机推荐

  1. 归并排序详解(python实现)

    因为上个星期leetcode的一道题(Median of Two Sorted Arrays)所以想仔细了解一下归并排序的实现. 还是先阐述一下排序思路: 首先归并排序使用了二分法,归根到底的思想还是 ...

  2. Linux基础学习(5)--文本编辑器Vim

    第五章——文本编辑器Vim 一. Vim常用操作 1.Vim简介:              Vim是一个功能强大的全屏幕文本编辑器,是Linux/UNIX上最常用的文本编辑器,它的作用是建立.编辑. ...

  3. vue-cli webpack项目npm run dev启动过程

    前言 通过vue init webpack和npm install命令初始化项目后,执行npm run dev就打开了网站http://localhost:8080.初学者不知道index.html. ...

  4. list1与list2求交集的方法总结!

    一.有序集合求交集的方法有 a)二重for循环法,时间复杂度O(n*n) b)拉链法,时间复杂度O(n) c)水平分桶,多线程并行 d)bitmap,大大提高运算并行度,时间复杂度O(n) e)跳表, ...

  5. codeforces146A

    Lucky Ticket CodeForces - 146A Petya loves lucky numbers very much. Everybody knows that lucky numbe ...

  6. poj2632 【模拟】

    In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure ...

  7. javascript history.go(-1) 返回刷新不起作用

    js返回刷新两种方式: 方式一:有提示框 <input type="button"  onClick="javascript:history.go(-1);loca ...

  8. 洛谷 P2587 [ZJOI2008]泡泡堂 解题报告

    P2587 [ZJOI2008]泡泡堂 题目描述 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表队由n名选手组成,比赛的项目是老少咸宜的网络游戏 ...

  9. A1005. Spell It Right

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...

  10. 快速幂&快速乘法

    尽管快速幂与快速乘法好像扯不上什么关系,但是东西不是很多,就一起整理到这里吧 快速幂思想就是将ax看作x个a相乘,用now记录当前答案,然后将指数每次除以2,然后将当前答案平方,如果x的2进制最后一位 ...