第一步,当时是登录成功之后把用户相关信息存放到Session当中去:
SetValue<DZ_UserModel>(SessionName.User, userInfo);
public static void SetValue<T>(SessionName name, T t)
{
if (HttpContext.Current == null) return;
HttpContext.Current.Session[name.ToString()] = t;
}
第二部就是检验每次访问后台Controller的时候校验这个Session失效。
这里我们需要定义与i=一个基类Controlleer:BaseController
其中,BaseControll当中最重要的一个函数如下:(过滤器j)
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
User = SessionUtil.GetUser();
if (User == null)
{
filterContext.Result = new RedirectResult("/Login/Index");
}
else if (User.UserCode == "" || User.UserCode == null)
{
filterContext.Result = new RedirectResult("/Login/Index");
}
base.OnActionExecuting(filterContext);
}
如果Session没有失效,则不做处理,否则,重新跳转到登录页面。