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