(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 ...
随机推荐
- 使用C++的开源序列化(Serialization)库cereal
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:使用C++的开源序列化(Serialization)库cereal.
- PHP环境配置综合篇
1.WNMP: http://www.wnmp.com.cn/ En: https://www.getwnmp.org/ 2.xampp:https://www.apachefriends.o ...
- SQL SERVER 查询死锁
USE mastergo CREATE PROCEDURE [dbo].[sp_who_lock]AS BEGIN DECLARE @spid INT , ...
- 修正android cocos2dx项目当点击属性时提示错误的问题
近期在用cocos2dx 3.x版本号做android版本号的时候,出现点击project-属性-C/C++ builder的时候会提示 The currently displayed paye co ...
- HDU2149-Good Luck in CET-4 Everybody!(博弈,打表找规律)
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- Makefile详解--隐含规则
Makefile详解--隐含规则(转) Makefile系列文章,这里有个前辈连续洗了一个系列来介绍,共有26篇博客文章. http://www.cppblog.com/ivenher/archive ...
- 关于weight属性使用的一些细节
之前被这个属性困扰过好久,今天一个偶然的机会,终于把这个搞清楚了,现在与大家分享一下. 假设我们要在一个LinearLayout布局中显示两个按钮,button1和button2,button2的宽度 ...
- 给EditText的drawableRight属性的图片设置点击事件 分类: 学习笔记 android 2015-07-06 13:20 134人阅读 评论(0) 收藏
这个方法是通用的,不仅仅适用于EditText,也适用于TextView.AutoCompleteTextView等控件. Google官方API并没有给出一个直接的方法用来设置右边图片的点击事件,所 ...
- Android开发之使用活动显示对话框
利用活动显示对话框,需要重写Activity中的onCreateDialog()方法,以此来显示一个对话框窗口. 效果如下: 实现代码如下: package com.example.dialog; i ...
- 利用Gulp实现JSDoc 3的文档编写过程中的实时解析和效果预览
### 利用Gulp实现JSDoc 3的文档编写过程中的实时解析和效果预览 http://segmentfault.com/a/1190000002583569