首页 > 代码库 > 用户验证
用户验证
#region 用户验证
/// <summary>
/// 用户验证过滤器
/// </summary>
public class UserAuthentication : AuthorizeAttribute
{
IMenu imenu = UserEngine.GetProvider<IMenu>();
/// <summary>
/// 执行前验证
/// </summary>
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (!UserContext.Instance.IsLogined)
{
filterContext.Result = new RedirectResult("/home/Login");
//filterContext.Result = new JsonResult { Data = http://www.mamicode.com/new { Result = 1, Content ="您没有权限!", Redirect = "/Home/Login" }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
else
{
IList<ActionInfo> actionList = UserContext.Instance.User.GetActions();
IList<MenuActionInfo> menuActionList = imenu.GetMenuActionListByActions(actionList);
IDictionary<int, MenuInfo> menuInDB = imenu.GetMenuDictionary();
var isMenuInDbQuery = from m in menuInDB.Values
where (!string.IsNullOrEmpty(m.TargetUrl.Trim())&&filterContext.HttpContext.Request.Url.PathAndQuery.ToUpper().Contains(m.TargetUrl.ToUpper()))
select m;
if(isMenuInDbQuery.ToList().Count()>0)
{
var menuQuery = from ma in menuActionList
where (!string.IsNullOrEmpty(ma.TargetUrl.Trim())&&filterContext.HttpContext.Request.Url.PathAndQuery.ToUpper().Contains(ma.TargetUrl.ToUpper()))
select ma.TargetUrl;
if (menuQuery.ToList().Count() < 1)
{
filterContext.Result = new RedirectResult("/Home/Login");
}
}
}
}
}
#endregion
用户验证