我创建了自己的用户权限表,其中每个用户可以属于许多组和许多安全角色。但是我的 _layout 视图中包含以下内容:-
<li class="nav-header hidden-tablet"style="background-color:#3E9BD4 ; color:white">User Section</li>
<li><a class="ajax-link" href="~/Home/Contact"><i class="icon-home"></i><span class="hidden-tablet">contact</span></a></li>
<li class="nav-header hidden-tablet"style="background-color:#3E9BD4 ; color:white">Administration Section</li>
<li><a class="ajax-link" href="~/SecurityGroup/"><i class="icon-home"></i><span class="hidden-tablet">Security Groups</span></a></li>
<li><a class="ajax-link" href="~/SecurityRole/"><i class="icon-home"></i><span class="hidden-tablet">Security Roles</span></a></li>
<li><a class="ajax-link" href="~/AuditInfo"><i class="icon-home"></i><span class="hidden-tablet">Audit</span></a></li>
<li><a class="ajax-link" href="~/SecurityGroup/ADUsers"><i class="icon-home"></i><span class="hidden-tablet">Active Directory</span></a></li>
因此,只有当当前用户属于与名为“Admin”的预定义安全角色相关联或直接链接到此安全角色的组时,我才能显示管理部分。
我没有在我的应用程序中存储任何用户信息,因为用户存在于 Active Directory 中。所以我有以下模型类: -
public partial class Group
{
public Group()
{
this.UserGroups = new HashSet<UserGroup>();
this.SecurityRoles = new HashSet<SecurityRole>();
}
public int GroupID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public byte[] timestamp { get; set; }
public virtual ICollection<UserGroup> UserGroups { get; set; }
public virtual ICollection<SecurityRole> SecurityRoles { get; set; }
}
public partial class UserGroup
{
public int GroupID { get; set; }
public string UserName { get; set; }
public virtual Group Group { get; set; }
}
}
public partial class SecurityRole
{
public SecurityRole()
{
this.SecurityroleTypePermisions = new HashSet<SecurityroleTypePermision>();
this.SecurityRoleUsers = new HashSet<SecurityRoleUser>();
this.Groups = new HashSet<Group>();
}
public int SecurityRoleID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public byte[] timestamp { get; set; }
public virtual ICollection<SecurityroleTypePermision> SecurityroleTypePermisions { get; set; }
public virtual ICollection<SecurityRoleUser> SecurityRoleUsers { get; set; }
public virtual ICollection<Group> Groups { get; set; }
}
public partial class SecurityRoleUser
{
public int SecurityRoleID { get; set; }
public string UserName { get; set; }
public virtual SecurityRole SecurityRole { get; set; }
}
请记住 _layout 视图不是强类型视图,因此无法在模型级别定义辅助方法,例如 public bool IsAdmin(string userName) 。
提前感谢您的帮助。