码迷,mamicode.com
首页 > Web开发 > 详细

ASP.Net MVC 登录授权验证

时间:2019-10-07 00:46:46      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:pos   hand   res   new   style   glob   use   auth   authorize   



public
class Verify : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { var user = filterContext.HttpContext.Session["CurrentUser"]; if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)) //判断是否Action判断是否跳过授权过滤器 { return; } else if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)) //判断是否Controller判断是否跳过授权过滤器 { return; } else if (user == null || string.IsNullOrWhiteSpace(user.ToString())) //判断用户是否登录 { filterContext.Result = new RedirectResult("../Login/Login"); } else { return; } } }
 
全局授权验证
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); filters.Add(new Verify()); } }
   控制器授权验证
[Verify]
public class LoginController : Controller { }
 public class LoginController : Controller
    {
     方法授权验证 [Verify]
public ActionResult UserInfo(Models.UserInfo userInfo) { } }
public class LoginController : Controller
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]//方法忽略验证
        public ActionResult Login(Models.UserInfo userInfo)
        {
         }
      }

 

ASP.Net MVC 登录授权验证

标签:pos   hand   res   new   style   glob   use   auth   authorize   

原文地址:https://www.cnblogs.com/YunChao/p/11629077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!