0
是否可以创建一个由Active Directory组严格控制的页面(View)?ASP.NET MVC 5页由Active Directory Group控制
没有登录这个页面,如果你是“VIP”Active Directory组的成员,那么该页面被渲染,否则如果没有,那么你看不到它。
是否可以创建一个由Active Directory组严格控制的页面(View)?ASP.NET MVC 5页由Active Directory Group控制
没有登录这个页面,如果你是“VIP”Active Directory组的成员,那么该页面被渲染,否则如果没有,那么你看不到它。
首先让你的当前用户的Windows登录
var windowsUserName= HttpContext.Current.User.Identity.WindowsLogin();
然后,使用的System.DirectoryServices
using System.DirectoryServices;
public List<string> GetUsersActiveDirectoryGroups(string windowsUserName)
{
try
{
var allUserGroups = new List<string>();
if (windowsUserName == null) return allUserGroups;
var domainConnection = new DirectoryEntry();
var samSearcher = new DirectorySearcher
{
SearchRoot = domainConnection,
Filter = "(samAccountName=" + windowsUserName + ")"
};
samSearcher.PropertiesToLoad.Add("displayName");
var samResult = samSearcher.FindOne();
if (samResult == null) return allUserGroups;
var theUser = samResult.GetDirectoryEntry();
theUser.RefreshCache(new[] { "tokenGroups" });
_bet365EmployeeFullName = theUser.Properties["CN"].Value.ToString();
foreach (byte[] resultBytes in theUser.Properties["tokenGroups"])
{
var mySid = new SecurityIdentifier(resultBytes, 0);
var sidSearcher = new DirectorySearcher
{
SearchRoot = domainConnection,
Filter = "(objectSid=" + mySid.Value + ")"
};
sidSearcher.PropertiesToLoad.Add("name");
var sidResult = sidSearcher.FindOne();
if (sidResult != null)
{
allUserGroups.Add((string)sidResult.Properties["name"][0]);
}
}
return allUserGroups;
}
现在,您需要映射哪些群体有机会获得所有用户的AD组该应用程序中的哪个视图。 完成后,下一步是限制“视图”的查看。
您需要设置使用MVC AuthorizeAttribute的权限过滤器。像下面的东西。
public class PermissionsFilter : AuthorizeAttribute
{
private readonly string _viewName;
public PermissionsFilter(string viewName)
{
_viewName = viewName;
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
//Check to see if users groups has access to the view
//If not redirect to unauthorized page
}
}
我通过在session中拥有一个用户对象来完成上述任务。这包含我的用户可以访问的所有应用程序权限的列表。这是你需要做的映射。我将所有视图名称与AD组可以访问的ID一起存储在数据库中。
然后最后在控制器中,相应地为视图装饰获取动作。
[HttpGet]
[PermissionsFilter("ViewName")]
public ActionResult ReturnMyView(int currentFolderID)
{
return View(); //Etc..
}
好希望帮助!
这是肯定的......但你需要存储你自己的映射,哪些组可以访问哪些视图。 – Wheels73
你有任何代码示例或指向我在哪里看? – fes
当然..我会给你一个答案。你已经有访问AD组的代码了吗? – Wheels73