MVC AuthorizeAttribute 登录验证
2017-08-30 22:57
148 查看
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class,Inherited =true)] public class AuthorityFilter : AuthorizeAttribute { /// <summary> /// 未登录时返还的地址 /// </summary> private string _loginPath = ""; public AuthorityFilter() { //可以从web.config中读取 this._loginPath = "/User/Login"; } public AuthorityFilter(string loginPath) { this._loginPath = loginPath; } /// <summary> /// 检查用户登录 /// </summary> /// <param name="filterContext"></param> public override void OnAuthorization(AuthorizationContext filterContext) { if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)) { return; //表示支持控制器、action的AllowAnonymousAttribute } var sessionUser = filterContext.HttpContext.Session["User"];//使用session //var memberValidation = HttpContext.Current.Request.Cookies.Get("CurrentUser");//使用cookie if (sessionUser == null) { HttpContext.Current.Session["returnUrl"] = filterContext.HttpContext.Request.RawUrl; filterContext.Result = new RedirectResult(this._loginPath); } } }
相关文章推荐
- mvc中的action验证登录(ActionFilterAttribute)
- mvc 基于basecontroller、ActionFilterAttribute及路由的访问登录验证
- 【记录】ASP.NET MVC AuthorizeAttribute OnAuthorization 验证跳转
- mvc中的action验证登录(ActionFilterAttribute)
- MVC ValidationAttribute 服务器端自定义验证
- ASP.NET MVC 3 Validation - 正则表达式验证RegularExpressionAttribute之日期验证
- MVC用户权限管理AuthorizeAttribute
- ASP.NET MVC使用AuthenticationAttribute验证登录
- ASP.Net MVC Filter验证用户登录
- MVC使用Controller代替Filter完成登录验证(Session校验)学习笔记5
- asp.net MVC 通用登录验证模块
- MVC 自定义过滤器/特性来实现登录授权及验证
- ASP.NET MVC 3 Validation - 正则表达式验证RegularExpressionAttribute之日期验证
- Asp.net mvc验证用户登录之Forms实现-转
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(二)基于SpringMVC+Shiro的用户登录权限验证
- MVC 登录验证设置form验证
- 使用SSM(Spring+SpringMVC+Mybatis)框架搭建服务器实现登录验证
- 使用MVC模型的用户登录及注册(注册验证)
- .net MVC使用Session验证用户登录(转载)
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(三)给Shiro登录验证加上验证码