using System;
using System.Web.Mvc;
namespace CommonTool
{
///
/// 表示需要用户登录才可以使用的特性
/// 如果不需要处理用户登录,则指定AllowAnonymousAttribute属性
///
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class AuthorizationAttribute : FilterAttribute, IAuthorizationFilter
{
#region 使用方法
// 在Web.Config文件里面加入下面几句用于配置登陆验证的一些信息:
//
//
//
//
//
//使用实例:
////...省略引用
//namespace MrHuo.Framework.Blog
// {
// [Authorization]//如果将此特性加在Controller上,那么访问这个Controller里面的方法都需要验证用户登录状态
// public class UserController : Controller
// {
// [AllowAnonymous]//这里是一个特例,有这个特性,表示这个方法不需要验证用户登录状态
// public ActionResult Index()
// {
// //...省略具体代码
// }
// //这里的方法需要验证登录状态,以下雷同
// public ActionResult Create()
// {
// //...省略具体代码
// }
// }
//}
#endregion
#region 构造函数
///
/// 默认构造函数
///
public AuthorizationAttribute()
{
string authUrl = System.Configuration.ConfigurationManager.AppSettings["AuthUrl"];
string authSaveKey = System.Configuration.ConfigurationManager.AppSettings["AuthSaveKey"];
string authSaveType = System.Configuration.ConfigurationManager.AppSettings["AuthSaveType"];
_authUrl = string.IsNullOrEmpty(authUrl) ? "/User/Login" : authUrl;
_authSaveKey = string.IsNullOrEmpty(authSaveKey) ? "LoginedUser" : authSaveKey;
_authSaveType = string.IsNullOrEmpty(authSaveType) ? "Session" : authSaveType;
}
///
/// 构造函数重载
///
/// 表示没有登录跳转的登录地址
public AuthorizationAttribute(string authUrl)
: this()
{
_authUrl = authUrl;
}
///
/// 构造函数重载
///
/// 表示没有登录跳转的登录地址
/// 表示登录用来保存登陆信息的键名
public AuthorizationAttribute(string authUrl, string authSaveKey)
: this(authUrl)
{
_authSaveKey = authSaveKey;
}
///
/// 构造函数重载
///
/// 表示没有登录跳转的登录地址
/// 表示登录用来保存登陆信息的键名
/// 表示登录用来保存登陆信息的方式
public AuthorizationAttribute(string authUrl, string authSaveKey, string authSaveType)
: this(authUrl, authSaveKey)
{
_authSaveType = authSaveType;
}
#endregion
#region 字段属性
private string _authUrl;
///
/// 获取或者设置一个值,改值表示登录地址
/// 如果web.config中未定义AuthUrl的值,则默认为/User/Login
///
public string AuthUrl
{
get { return _authUrl.Trim(); }
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException($"用于验证用户登录信息的登录地址不能为空!");
_authUrl = value.Trim();
}
}
private string _authSaveKey;
///
/// 获取或者设置一个值,改值表示登录用来保存登陆信息的键名
/// 如果web.config中未定义AuthSaveKey的值,则默认为LoginedUser
///
public string AuthSaveKey
{
get { return _authSaveKey.Trim(); }
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException($"用于保存登录信息的键名不能为空");
_authSaveKey = value.Trim();
}
}
private string _authSaveType;
///
/// 获取或者设置一个值,该值表示用来保存登陆信息的方式
/// 如果web.config中未定义AuthSaveType的值,则默认为Session保存
///
public string AuthSaveType
{
get { return _authSaveType.Trim(); }
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException($"于保存登陆信息的方式不能为空,只能为【Cookie】或者【Session】!");
_authSaveType = value.Trim();
}
}
#endregion
#region 实现接口方法
///
/// 处理用户登录
///
///
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext.HttpContext == null)
throw new Exception("此特性只适合于Web应用程序使用");
switch (AuthSaveType)
{
case "Session":
if (filterContext.HttpContext.Session == null)
throw new Exception("服务器Session不可用");
if (!filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) &&
!filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
if (filterContext.HttpContext.Session[_authSaveKey] == null)
filterContext.Result = new RedirectResult(_authUrl);
break;
case "Cookie":
if (!filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) &&
!filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
if (filterContext.HttpContext.Request.Cookies[_authSaveKey] == null)
filterContext.Result = new RedirectResult(_authUrl);
break;
default:
throw new ArgumentNullException($"用于保存登陆信息的方式不能为空,只能为【Cookie】或者【Session】!");
}
}
#endregion
}
}