(MVC)验证用户是否登录 登录认证
验证类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace CommonHelper
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class AuthorizationAttribute
{
private String _AuthUrl = String.Empty; /// <summary>
/// 获取或者设置一个值,改值表示登录地址
/// 如果web.config中未定义AuthUrl的值,则默认为login
/// </summary>
public String AuthUrl
{
get { return _AuthUrl; }
set
{
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("用于验证用户登录信息的登录地址不能为空");
else
_AuthUrl = value.Trim();
}
} private String _AuthSaveKey = String.Empty;
/// <summary>
/// 获取或者设置一个值,改值表示登录用来保存登录信息的键名
/// 如果web.config中未定义AuthSaveKey的值,则默认为LoginedUser
/// </summary>
public String AuthSaveKey
{
get { return _AuthSaveKey; }
set {
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("用于保存登录信息的键名不能为空");
else
this._AuthSaveKey = value.Trim();
}
} private String _AuthSaveType = String.Empty;
/// <summary>
/// 获取或者设置一个值,该值表示用来保存登录信息的方式
/// </summary>
public String AuthSaveType {
get { return _AuthSaveType; }
set {
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("用于保存登录信息的方式不能为空,只能为【cookie】或者【session】");
else
this._AuthSaveType = value.Trim();
}
} /// <summary>
/// 默认构造函数
/// </summary>
public AuthorizationAttribute()
{
String authUrl=System.Configuration.ConfigurationManager.AppSettings["AuthUrl"];
String saveKey = System.Configuration.ConfigurationManager.AppSettings["AuthSaveKey"];
String saceType = System.Configuration.ConfigurationManager.AppSettings["AuthSaveType"]; if (String.IsNullOrEmpty(authUrl))
this._AuthUrl = "/User/Login";
else
this._AuthUrl = authUrl; if (String.IsNullOrEmpty(saveKey))
this._AuthSaveKey = "LoginedUser";
else
this._AuthSaveKey = saveKey; if (String.IsNullOrEmpty(saceType))
this._AuthSaveType = "Session";
else
this._AuthSaveType = saceType;
} /// <summary>
/// 构造函数重载
/// </summary>
/// <param name="authUrl">表示没有登录跳转的登录地址</param>
public AuthorizationAttribute(String authUrl)
: this()
{
this._AuthUrl = authUrl;
} /// <summary>
/// 构造函数重载
/// </summary>
/// <param name="authUrl">表示没有登录跳转的登录地址</param>
/// <param name="saveKey">表示登录用来保存登录信息的键名</param>
public AuthorizationAttribute(String authUrl, String saveKey)
: this(authUrl)
{
this._AuthSaveKey = saveKey;
this._AuthSaveType = "Session";
} /// <summary>
/// 构造函数重载
/// </summary>
/// <param name="authUrl">表示没有登录跳转的登录地址</param>
/// <param name="saveKey">表示登录用来保存登录信息的键名</param>
/// <param name="saveType">表示登录用来保存登录信息的方式</param>
public AuthorizationAttribute(String authUrl, String saveKey, String saveType)
: this(authUrl, saveKey)
{
this._AuthSaveType = saveType;
} /// <summary>
/// 处理用户登录
/// </summary>
/// <param name="fileterContext"></param>
public void OnAuthorization(AuthorizationContext fileterContext)
{
if (fileterContext.HttpContext == null)
throw new Exception("次特性只适合于Web应用程序使用!");
else {
switch (AuthSaveType)
{
case "Session":
if (fileterContext.HttpContext.Session == null)
throw new Exception("服务器Session不可用!");
else if (!fileterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) && !fileterContext.ActionDescription.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
{
if (fileterContext.HttpContext.Session[_AuthSaveKey] == null)
fileterContext.Result = new RedirectResult(_AuthUrl);
}
break;
case "Cookie":
if (!fileterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) && !fileterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
{
if (fileterContext.HttpContext.Request.Cookies[_AuthSaveKey] == null)
fileterContext.Result = new RedirectResult(_AuthUrl);
}
break;
default:
throw new ArgumentNullException("用于保存登录信息的方式不能为空,只能为【Cookie】或者【Session】");
}
}
}
}
}
类
配置文件中
<appSettings>
<add key="AuthUrl" value="/User/Login">
<appSettings>
using后
在需要的页面控制器前加
[Authorization]
(MVC)验证用户是否登录 登录认证的更多相关文章
- Asp.net mvc验证用户登录之Forms实现
这里我们采用asp.net mvc 自带的AuthorizeAttribute过滤器验证用户的身份,也可以使用自定义过滤器,步骤都是一样. 第一步:创建asp.net mvc项目, 在项目的App_S ...
- [转]MVC 检测用户是否已经登录
本文转自:http://blog.csdn.net/jayzai/article/details/41252137 当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null) ...
- Filter应用之-验证用户是否已经登录
过滤器: public class LoginFilter implements Filter{ @Override public void init(FilterConfig filterConfi ...
- Struts2+Spring+Hibernate step by step 11 ssh拦截验证用户登录到集成
注意:该系列文章从教师王健写了一部分ssh集成开发指南 引言: 之前没有引入拦截器之前,我们使用Filter过滤器验证用户是否登录,在使用struts2之后,全然能够使用拦截器,验证用户是否已经登录, ...
- .net MVC使用Session验证用户登录(转载)
.net MVC使用Session验证用户登录 用最简单的Session方式记录用户登录状态 1.添加DefaultController控制器,重写OnActionExecuting方法,每次访问 ...
- MVC学习笔记:MVC实现用户登录验证ActionFilterAttribute用法并实现统一授权
在项目下新建一个文件夹来专门放过滤器类,首先创建一个类LoginFilter,这个类继承ActionFilterAttribute.用来检查用户是否登录和用户权限.: using System; us ...
- easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)
easyui datagrid 禁止选中行 没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...
- asp.net mvc 自定义全局过滤器 验证用户是否登录
一般具有用户模块的系统都需要对用户是否登录进行验证,如果用户登录了就可以继续操作,否则退回用户的登录页面 对于这样的需求我们可以通过自定义一个独立的方法来完成验证的操作,但是这样代码的重复率就大大提高 ...
- MVC4项目中验证用户登录一个特性就搞定
在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性. // 摘要: // 表示一个特性,该特性用于限制调用方对操作方法的访问. [AttributeUsage(Attribu ...
随机推荐
- jQuery判断页面滚动条滚动方向
废话不多说,直接上代码 $(window).scroll(function(){ var before = $(window).scrollTop(); $(window).scroll(functi ...
- Apache Shiro 集成-Cas
http://blog.csdn.net/peterwanghao/article/details/8825008 Shiro集成CAS是在1.2版本里新增的功能. Shiro-cas模块将应用作为C ...
- HTML5 API 浏览器支持情况检测
HTML5发展到现在,虽说没有大规模的普及,但在我们日常生活中,也很容易见到了,HTML5的游戏.网站.应用也是层出不穷.而作为前端人员,也应该多了解这些API为以后应用打基础,下面我将给大家介绍 H ...
- UIImage载入图片的几种方式及差别
用UIImage载入图像的方法非常多.最经常使用的是几种: 1.使用imageNamed函数载入: <span style="font-size:14px;">[UI ...
- linux下so动态库一些不为人知的秘密 系列
http://blog.chinaunix.net/uid-27105712-id-3313293.html http://www.cnblogs.com/gulvzhe/archive/2012/0 ...
- mysql 交互式连接和非交互式连接
交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE选项的客户端 mysql_real_connect() 函数介绍 函数原型描述: MYSQL *m ...
- Java实现直接插入查找
import java.util.Scanner; /*算法思想:每趟将一个待排序的元素作为关键字,按照关键字值大小插入到已排好序的那部分序列的适当位置上,直到插入完成,*/ /*平均时间复杂度O(n ...
- centos could not retrieve mirrorlist
centos could not retrieve mirrorlist >>>>>>>>>>>>>>>> ...
- 正则表达式工具类,正则表达式封装,Java正则表达式
正则表达式工具类 正则表达式封装 Java正则表达式 >>>>>>>>>>>>>>>>>>& ...
- HTML5吧
一.为了能使IE9以下的IE浏览器也能支持html5的标签,所以首先得在文档头部用条件注释的方法引入那段著名的代码. 1 2 3 <!--[if lt IE 9]> <script ...