首页 > 代码库 > asp.net mvc 简单实现权限控制
asp.net mvc 简单实现权限控制
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 简单实现权限控制
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。