登录用户的身份验证 权限验证
//权限的验证
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;
}
}