验证类

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)验证用户是否登录 登录认证的更多相关文章

  1. Asp.net mvc验证用户登录之Forms实现

    这里我们采用asp.net mvc 自带的AuthorizeAttribute过滤器验证用户的身份,也可以使用自定义过滤器,步骤都是一样. 第一步:创建asp.net mvc项目, 在项目的App_S ...

  2. [转]MVC 检测用户是否已经登录

    本文转自:http://blog.csdn.net/jayzai/article/details/41252137 当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null) ...

  3. Filter应用之-验证用户是否已经登录

    过滤器: public class LoginFilter implements Filter{ @Override public void init(FilterConfig filterConfi ...

  4. Struts2+Spring+Hibernate step by step 11 ssh拦截验证用户登录到集成

    注意:该系列文章从教师王健写了一部分ssh集成开发指南 引言: 之前没有引入拦截器之前,我们使用Filter过滤器验证用户是否登录,在使用struts2之后,全然能够使用拦截器,验证用户是否已经登录, ...

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

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

  6. MVC学习笔记:MVC实现用户登录验证ActionFilterAttribute用法并实现统一授权

    在项目下新建一个文件夹来专门放过滤器类,首先创建一个类LoginFilter,这个类继承ActionFilterAttribute.用来检查用户是否登录和用户权限.: using System; us ...

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

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

  8. asp.net mvc 自定义全局过滤器 验证用户是否登录

    一般具有用户模块的系统都需要对用户是否登录进行验证,如果用户登录了就可以继续操作,否则退回用户的登录页面 对于这样的需求我们可以通过自定义一个独立的方法来完成验证的操作,但是这样代码的重复率就大大提高 ...

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

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

随机推荐

  1. Mesh.Bake Scaled Mesh PhysX CollisionData的性能问题

    最近在做项目优化时,遇到Mesh.Bake Scaled Mesh PhysX CollisionData这个问题,随手记录一下. profiler中显示的cpu波峰瓶颈中,Mesh.Bake Sca ...

  2. KVM地址翻译流程及EPT页表的建立过程

    本博文为原创,遵循CC3.0协议,转载请注明出处:http://blog.csdn.net/lux_veritas/article/details/9284635 ------------------ ...

  3. UVa 567: Risk

    这是一道很简单的图论题,只要使用宽度优先搜索(BFS)标记节点间距离即可. 我的解题代码如下: #include <iostream> #include <cstdio> #i ...

  4. android 75 新闻列表页面

    new.xml <?xml version="1.0" encoding="UTF-8" ?> <newslist> <news& ...

  5. redis持久化和常见故障

    https://segmentfault.com/a/1190000004135982 redis 主从复制 Redis主从复制的原理 当建立主从关系时,slave配置slaveof <mast ...

  6. python学习笔记--Django入门二 Django 的模板系统

    为了使网站更干净简洁更容易维护,页面的设计和Python的代码必须分离开.我们可以使用Django的 模板系统 (Template System)来实现这种模式. 几个简单的模板标签(tag):   ...

  7. 聊聊 iOS 中的网络加密

    介绍下 公司的接口一般会两种协议的,一种HTTP,一种HTTPS的,HTTP 只要请求,服务器就会响应,如果我们不对请求和响应做出加密处理,所有信息都是会被检测劫持到的,是很不安全的,客户端加密可以使 ...

  8. mina编解码(摘录)

    一.Mina对编解码的支持 我们知道网络通讯过程实际是对二进制数据进行处理的过程,二进制数据是计算机认识的数据.对于接收到的二进制数据我们需要将其转换成我们所熟悉的数据格式,此过程称为解码(decod ...

  9. Python爬虫获取知乎图片

    前段时间想抓点知乎问题中的图片,了解了下爬虫,发现还是Python的简单方便,于是做了点尝试. #coding=utf-8 import urllib import re def getHtml(ur ...

  10. Grant-Permission.ps1

    Grant-Permission.ps1 Download the EXE version of SetACL 3.0.6 for 32-bit and 64-bit Windows. Put set ...