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

asp.net mvc 简单实现权限控制

时间:2017-04-17 09:33:21      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:ror   direct   add   bool   base   att   name   val   bag   

    public class HttpAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            bool _pass = false;

            if (httpContext.Session["UserName"] != null) {
                _pass = true;
            }

            return _pass;
        }

        /// <summary>
        /// 登录界面
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.Result = new RedirectResult("~/");
        }
    }

 加session

        [ValidateAntiForgeryToken()]
        [HttpPost]
        [AllowAnonymous]
        public ActionResult Index(ViewUser viewUser) {
            if (ModelState.IsValid) {
                var user = _user.Find(a => a.UserName == viewUser.UserName);
                if (user == null)
                {
                    ModelState.AddModelError("UserName", "用户名不存在");
                }
                else if (viewUser.Password != user.Password)
                {
                    ModelState.AddModelError("Password", "密码不正确");
                }
                else {
                    Session.Add("UserName", viewUser.UserName);
                    Session.Add("Password", viewUser.Password);
                    //ViewBag.LoginStatus = true;
                    //ViewBag.UserName = viewUser.UserName;
                    return RedirectToAction( "Index","Welcome");
                }
            
            }
            return View(viewUser);
        }

 

asp.net mvc 简单实现权限控制

标签:ror   direct   add   bool   base   att   name   val   bag   

原文地址:http://www.cnblogs.com/MichaelWillLee/p/6721202.html

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