[英]Display HTML code depending on which group a user is in
我想根据用户所在的 AD 组有选择地在页面上显示一段文本。该页面是一个简单的 ATM 链接列表,不需要控制器。 我有下面的代码,当我在本地开发(我登录到 AD)时,它可以完美运行 - 一旦我将应用程序发布到 IIS 服务器,我就会收到 404 错误 - 我已经能够找到导致的确切行错误 -> 在 ActiveDirectory.IsInGroup() 行 group.Translate 是罪魁祸首。
我已经检查了 IIS 服务器上的事件查看器(以及 IIS 的日志),但根本没有记录任何内容?
这是 index.html:
@page
@using System.Security.Principal
@{
ViewData["Title"] = "Landing Page";
}
@{
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool itUser = ActiveDirectory.IsInGroup(principal, "IT Department");
}
<h4>Header</h4>
@if (itUser || adminUser)
{
<div class="card mb-4 shadow-sm">
<div class="card-header">
<h4 class="my-0 font-weight-normal">IT</h4>
</div>
<div class="card-body">
<a target="_blank" href="http://www.test.com/Configuration/Index" class="btn btn-secondary my-2">Application Config</a><br />
</div>
</div>
}
这是 C# 代码:
public static class ActiveDirectory
public static bool IsInGroup(ClaimsPrincipal checkUser, string checkGroupName)
{
var identity = (WindowsIdentity)checkUser.Identity;
if (identity?.Groups == null) return false;
foreach (var group in identity.Groups)
{
var groupName = group.Translate(typeof(NTAccount)).ToString(); // this gives me a 404 error
if (groupName.ToLower().Contains(checkGroupName.ToLower())) return true;
}
return false;
}
}
不使用控制器是不好的做法(并且违背了使用 MVC 框架(模型视图控制器)的目的),尤其是在建立可审计性和身份验证/授权检查方面。
最佳实践是让您的控制器上的默认 ActionResult(在您的 RouteConfig 文件中设置)调用您定义的函数以获取最终用户所属组的列表
例子:
在每个控制器 ActionResult 中传递以下内容:
// what will be needed:
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
var userSignOn = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name;
List<string> activedirectoryGroupList = new List<string>();
activedirectoryGroupList = GetGroupsFromSignOn(userSignOn);
被调用的函数:
internal static List<string> GetGroupsFromSignOn(string signOn)
{
string searchText = "(&(objectCategory=person)(objectClass=user)(sAMAccountName={0}))";
searchText = searchText.Replace("{0}", signOn);
Domain domain = Domain.GetCurrentDomain();
DirectoryEntry userEntry = domain.GetDirectoryEntry();
DirectorySearcher searcher = new DirectorySearcher(userEntry, searchText);
SearchResult result = searcher.FindOne();
DirectoryEntry currentUserEntry = result.GetDirectoryEntry();
List<string> activedirectoryGroupList = new List<string>();
if (currentUserEntry != null)
{
int propertyCount = currentUserEntry.Properties["memberOf"].Count;
string activedirectoryGroup;
for (int i = 0; i < propertyCount; i++)
{
activedirectoryGroup = Convert.ToString(currentUserEntry.Properties["memberOf"][i]);
int startIndex = activedirectoryGroup.IndexOf('=');
startIndex += 1;
int endIndex = activedirectoryGroup.IndexOf(',');
int stringLength = endIndex - startIndex;
activedirectoryGroup = activedirectoryGroup.Substring(startIndex, stringLength);
activedirectoryGroupList.Add(activedirectoryGroup);
}
return activedirectoryGroupList;
}
else
{
return null;
}
}
从那里,您将获得您的用户所属的 AD 组的列表,并且您可以将这些组与您可以在 Web 配置文件中设置的授权组名称交叉引用,或者在您可以调用的 SQL 数据库。
至于确定将显示什么,您可能应该设置一个主视图,根据您的用户对该信息的授权状态在该页面上呈现部分视图。
编辑:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.