ASP.NET.4.5.1+MVC5.0设置系统角色与权限(2)_.Net教程
推荐:使用ASP.NET.4.5.1+MVC5.0 搭建一个包含 Ninject框架 项目这篇文章主要介绍了使用ASP.NET.4.5.1+MVC5.0 搭建一个包含 Ninject框架 项目的方法,需要的朋友可以参考下 1.创建一个空白解决方案 2.添加一个类库 名称为XXX.Domain 3.添加一个ASP.MVC 名称为XXX.WebUI 4.选着空模版,勾选MVC核心引用 5.添加单元测试项目 XXX.UntiTes
代码如下:
namespace HR
{
public abstract class ControllerBase:HR.Helpers.ControllerBase
{
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
}
}
}
5.在项目中新建RoleControllerBase.cs
代码如下:
namespace HR
{
public class RoleControllerBase : ControllerBase
{
SystemUserRepository sysuserrepository = new SystemUserRepository();
/// <summary>
/// 用户权限
/// </summary>
public virtual List<EnumMoudle> PermissionList
{
get
{
var permissionList = new List<EnumMoudle>();
return permissionList;
}
}
public string BusinessPermissionString { get; set; }
[NotMapped]
public List<EnumMoudle> BusinessPermissionList
{
get
{
if (string.IsNullOrEmpty(BusinessPermissionString))
return new List<EnumMoudle>();
else
return BusinessPermissionString.Split(",".ToCharArray()).Select(p => int.Parse(p)).Cast<EnumMoudle>().ToList();
}
set
{
BusinessPermissionString = string.Join(",", value.Select(p => (int)p));
}
}
/// <summary>
/// Action方法执行前没有权限提示信息
/// </summary>
/// <param name="filterContext"></param>
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var noAuthorizeAttributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AuthorizeIgnoreAttribute), false);
if (noAuthorizeAttributes.Length > 0)
return;
base.OnActionExecuting(filterContext);
bool hasPermission = true;
var permissionAttributes = filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(PermissionAttribute), false).Cast<PermissionAttribute>();
permissionAttributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof(PermissionAttribute), false).Cast<PermissionAttribute>().Union(permissionAttributes);
var attributes = permissionAttributes as IList<PermissionAttribute> ?? permissionAttributes.ToList();
if (permissionAttributes != null && attributes.Count() > 0)
{
string cookie = CookieHelper.GetValue("SystemUserID");
if (string.IsNullOrEmpty(cookie))
{
filterContext.Result = Content("您没有登录!");
}
else
{
int mid = int.Parse(CookieHelper.GetValue("SystemUserID"));
var model = sysuserrepository.GetModel(mid);
BusinessPermissionString = model.BusinessPermissionString;
hasPermission = true;
foreach (var attr in attributes)
{
foreach (var permission in attr.Permissions)
{
if (!BusinessPermissionList.Contains(permission))
{
hasPermission = false;
break;
}
}
}
if (!hasPermission)
{
if (Request.UrlReferrer != null)
filterContext.Result = this.Stop("您没有权限!", "/default/ng");
else
filterContext.Result = Content("您没有权限!");
}
}
}
}
}
}
6.在每个Controller继承RoleControllerBase类
public class EmployeesController : RoleControllerBase
7.在HR.Helpers文件夹下添加PermissionAttribute.Cs ,并继承 FilterAttribute, IActionFilter
分享:ASP.NET中的URL映射技巧在项目中经常要进行如下映射 将 http://yyjcw.com/a.aspx?name=llinzzi 映射到 http://yyjcw.com/llinzzi 首先要解决2个问题 第一是支持通配符的映射 URL映射是asp.net2.0新增的功能,不支持通配符 于是采用了NBEAR的 NBear.Web.Modules.UrlRewriteRules 进行通配符映射
- 相关链接:
- 教程说明:
.Net教程-ASP.NET.4.5.1+MVC5.0设置系统角色与权限(2)。