代码

using Common;
using Service;
using Service.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc; namespace WebPage.Controllers
{
public class BaseController : Controller
{
//public log4net.Ext.IExtLog log = log4net.Ext.ExtLogManager.GetLogger("dblog"); #region 公用变量
/// <summary>
/// 查询关键词
/// </summary>
public string keywords { get; set; }
/// <summary>
/// 视图传递的分页页码
/// </summary>
public int page { get; set; }
/// <summary>
/// 视图传递的分页条数
/// </summary>
public int pagesize { get; set; }
/// <summary>
/// 用户容器,公用
/// </summary>
public IUserInfoManage UserInfoManage = Spring.Context.Support.ContextRegistry.GetContext().GetObject("Service.UserInfoManage") as IUserInfoManage;
#endregion #region 用户对象
/// <summary>
/// 获取当前用户对象
/// </summary>
public Account CurrentUser
{
get
{
//从Session中获取用户对象
if (SessionHelper.GetSession("CurrentUser") != null)
{
return SessionHelper.GetSession("CurrentUser") as Account;
}
//Session过期 通过Cookies中的信息 重新获取用户对象 并存储于Session中
var account = UserInfoManage.GetAccountByCookie();
SessionHelper.SetSession("CurrentUser", account);
return account;
}
}
#endregion #region 登录验证 OnActionExecuting
/// <summary>
/// 重写控制器 OnActionExecuting(ActionExecutingContext filterContext)方法 实现登录验证和公共变量的获取
/// </summary>
/// <param name="filterContext"></param>
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
#region 登录用户验证
//1、判断Session对象是否存在
if (filterContext.HttpContext.Session == null)
{
filterContext.HttpContext.Response.Write("<script type='text/javascript'> alert('~登录已过期,请重新登录');window.top.location='/'; </script>");
filterContext.RequestContext.HttpContext.Response.End();
filterContext.Result = new EmptyResult();
return;
}
//2、登录验证
if (this.CurrentUser == null)
{
filterContext.HttpContext.Response.Write("<script type='text/javascript'> alert('登录已过期,请重新登录'); window.top.location='/';</script>");
filterContext.RequestContext.HttpContext.Response.End();
filterContext.Result = new EmptyResult();
return;
}
#endregion #region 公共Get变量
//分页页码
object p = filterContext.HttpContext.Request["page"];
if (p == null || p.ToString() == "") { page = ; } else { page = int.Parse(p.ToString()); } //搜索关键词
string search = filterContext.HttpContext.Request.QueryString["Search"];
if (!string.IsNullOrEmpty(search)) { keywords = search; }
//显示分页条数
string size = filterContext.HttpContext.Request.QueryString["example_length"];
if (!string.IsNullOrEmpty(size) && System.Text.RegularExpressions.Regex.IsMatch(size.ToString(), @"^\d+$"))
{
pagesize = int.Parse(size.ToString());
}
else
{
pagesize = ;
}
#endregion
}
#endregion //public void WriteLog(string Operator,string Msg, Common.Enums.enumLog4net level)
//{
// log.Error(Utils.GetIP(), this.CurrentUser.LogName, HttpContext.Request.Url.ToString(), "Module/Detail", Msg);
//}
}
}

OnActionExecuting验证用户登录的更多相关文章

  1. .net MVC使用Session验证用户登录(转载)

    .net MVC使用Session验证用户登录   用最简单的Session方式记录用户登录状态 1.添加DefaultController控制器,重写OnActionExecuting方法,每次访问 ...

  2. easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)

    easyui datagrid 禁止选中行   没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...

  3. MVC4项目中验证用户登录一个特性就搞定

    在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性. // 摘要: // 表示一个特性,该特性用于限制调用方对操作方法的访问. [AttributeUsage(Attribu ...

  4. MVC4验证用户登录特性实现方法

    在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性. // 摘要: // 表示一个特性,该特性用于限制调用方对操作方法的访问. [AttributeUsage(Attribu ...

  5. 单点登录CAS使用记(三):实现自定义验证用户登录

    问题: CAS自带的用户验证逻辑太过简单,如何像正常网站一样,通过验证DB中的用户数据,来验证用户以及密码的合法性呢? 方案1:CAS默认的JDBC扩展方案: CAS自带了两种简单的通过JDBC方式验 ...

  6. 转:C4项目中验证用户登录一个特性就搞定

    转:C4项目中验证用户登录一个特性就搞定   在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性.     // 摘要:    //     表示一个特性,该特性用于限制调用 ...

  7. How to use the windows active directory to authenticate user via logon form 如何自定义权限系统,使用 active directory验证用户登录

    https://www.devexpress.com/Support/Center/Question/Details/Q345615/how-to-use-the-windows-active-dir ...

  8. Asp.Net使用加密cookie代替session验证用户登录状态 源码分享

    首先 session 和 cache 拥有各自的优势而存在.  他们的优劣就不在这里讨论了. 本实例仅存储用户id于用户名,对于多级权限的架构,可以自行修改增加权限字段   本实例采用vs2010编写 ...

  9. 【转】MVC4验证用户登录特性实现方法

    在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性. // 摘要: // 表示一个特性,该特性用于限制调用方对操作方法的访问. [AttributeUsage(Attribu ...

随机推荐

  1. ubuntu 该软件包现在的状态极为不妥 error

    rm -rf /var/lib/dpkg/info/yourerrorsofware* dpkg --remove --force-remove-reinstreq yourerrorsoftware ...

  2. wx小程序获取组件属性数据data-prop

    在微信小程序中有时会在组件上定义一些属性,使用data-来定义 <view data-idvalue="id" data-Index-Name="IndexName ...

  3. Linux 多进程实现方法

    1.需求 查找192.168.0.*网段中所有未使用过的IP 2.实现     我们知道查找未使用IP的方法可以使用ping命令完成.对于单个IP的判断,使用命令如下 $ 192.168.0.1 PI ...

  4. webpack异步加载业务模块

    虽然把我们用到的JS文件全部打包一个可以节省请求数,但如果打包后的JS文件过大,那么也容易出现白屏现象,许多操作失灵.而且一些区域是点到才出现,那么相关的JS其实可以剥离出这个大JS文件外.这就涉及到 ...

  5. python判断任务是CPU密集型还是IO密集型

    目前已经知道,在需要并发执行任务的时候,需要使用多线程或者多进程;如果是IO密集型任务,使用多线程,如果是CPU密集型任务,使用多进程;但问题是,经常我们会遇到一种情况就是:需要被执行的任务既有IO操 ...

  6. JSTL标签不起作用的解决办法

    JSP页面中的EL标签直接成字符串输出(如:${user.id}),说明el标签没有被识别,造成的原因有: 1.jdk+jstl的组合不匹配 2.web.xml版本不匹配 但我们解决以上这个问题时,先 ...

  7. 学习JS的心路历程-类型

    前言 之前学JS时候都是靠着谷狗一路跌跌撞撞的学过来,从来没有去翻过MDN的文件,导致留了许多技术债给自己. 最近有幸遇到一位前辈并开始从头学JS,前辈表示学程序不看文件是想作死自己?于是我的第一份功 ...

  8. Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test)

    这是因为测试代码时遇到错误,它会停止编译.只需要在pom.xml的<project>里添加以下配置,使得测试出错不影响项目的编译.<build> <plugins> ...

  9. 昆虫之膜翅目(Hymenoptera)

    1.简介 膜翅目昆虫(sawflies, wasps, ants, and bees,叶蜂.黄蜂目.蚂蚁目和蜜蜂目)是四大种类繁多的昆虫目之一,包括15.3万多种已知昆虫,可能还有多达100万种尚未发现 ...

  10. Frame animation

    [Frame animation] An animation defined in XML that shows a sequence of images in order (like a film) ...