• 登录用户的身份验证 权限验证


    //权限的验证 

    public class CheckRoleAttribute : ActionFilterAttribute, IActionFilter
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                try
                {
                    SougeUser su = filterContext.HttpContext.Session["SystemUser"] as SougeUser;
                    if (su == null)
                    {
                        filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { Controller = "Account", action = "Login" }));
                    }
                    else
                    {
                        if (!su.TypeID.Equals((int)AccountTypeEnum.SurperAdmin))
                        {
                            List<PlateEnum> plates = filterContext.HttpContext.Session["SystemPlates"] as List<PlateEnum>;
                            if (plates == null)
                            {
                                filterContext.HttpContext.Session["SystemPlates"] = RoleFunction.GetRoles(su.TypeID);
                            }
                            if (!plates.Contains(Plate))
                            {
                                throw new Exception();
                            }
                        }
                    }
                }
                catch
                {
                    filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { Controller = "Page", action = "Index" }));
                }
                base.OnActionExecuting(filterContext);
            }

            public PlateEnum Plate { get; set; }
        }

    //验证

      [CheckRole(Plate = PlateEnum.添加账户)]
            public ActionResult AccountCreate()
            {
                ViewData["Roles"] = _accountR.GetRols();
                return View();
            }

     public static class RoleFunction
        {
            public static List<PlateEnum> GetRoles(int accountType)
            {
                List<PlateEnum> roles = new List<PlateEnum>();
                SouGeDBDataContext db = new SouGeDBDataContext();
                try
                {
                    List<SystemPlateRole> plateRoles = db.SystemPlateRole.Where(m => m.UserTypeID.Equals(accountType)).ToList();
                    if (plateRoles != null)
                    {
                        foreach (SystemPlateRole plateRole in plateRoles)
                        {
                            try
                            {
                                object obj = Enum.Parse(typeof(PlateEnum), plateRole.PlateType.ToString());
                                if (obj != null)
                                {
                                    roles.Add((PlateEnum)obj);
                                }
                            }
                            catch { }
                        }
                    }
                }
                catch { }
                return roles;
            }
        }

  • 相关阅读:
    dd的用法
    od的用法
    Windows 7安装Oracle 10g的方法
    Ubuntu下的iptux和Windows下的飞秋互传文件
    c++ 12
    c++ 11
    c++ 10
    c++ 09
    c++ 08
    c++ 07
  • 原文地址:https://www.cnblogs.com/nanxiaoxiang/p/2725591.html
Copyright © 2020-2023  润新知