验证类

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. 关于虚拟机VM

    装Vmware虚拟机+安装系统不再困难 不要再以为你是女生,你就可以不用学装系统,亲,我们是计算机系的女生,顶起!!呼啦呼啦,这篇我装VM+系统的记录,作为勉励. 想必很多人都会使用到虚拟机,因为我们 ...

  2. POJ 1006 Biorhythms(中国剩余定理)

    题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...

  3. 数据结构与算法/leetcode/lintcode题解

    http://algorithm.yuanbin.me/zh-hans/index.html

  4. hadoop错误java.io.IOException Failed to replace a bad datanode on the existing pipeline due to no more good datanodes being available to try

    错误: java.io.IOException: Failed to replace a bad datanode on the existing pipeline due to no more go ...

  5. The content of element type "beans" must match "(description?,(import|alias|bean)*)

    The content of element type "beans" must match "(description?,(import|alias|bean)*) - ...

  6. Shell变量之自定义变量、环境变量

    1:环境变量        环境变量可以帮我们达到很多功能-包括家目录的变换啊.提示字符的显示啊.运行文件搜寻的路径啊等等的那么,既然环境变量有那么多的功能,问一下,目前我的 shell 环境中, 有 ...

  7. Android开发之设定Dialog的位置

    今天自定义了一个对话框,但是弹出时默认是显示在屏幕中间.主要代码:menuDialog = new AlertDialog.Builder(this).create();                ...

  8. Java基础知识强化之网络编程笔记08:TCP之客户端键盘录入服务器控制台输出

    1. 客户端: package cn.itcast_08; import java.io.BufferedReader; import java.io.BufferedWriter; import j ...

  9. spring使用aop

    基于spring-framework-4.1.7使用aop >>>>>>>>>>>>>>>>>&g ...

  10. Python开发实战教程(8)-向网页提交获取数据

    来这里找志同道合的小伙伴!↑↑↑ Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知 ...