【发布时间】:2018-10-18 12:21:45
【问题描述】:
我在 IIS 上的同一台服务器上托管了一个网站和 API。在 API (.NET) 中,我需要获取正在使用该网站的用户所属的 AD 组列表。它在本地工作(邮递员调用 IIS Express 上的 API),但在我们的服务器上运行时却不能。获取广告组的代码是这样的:
string[] output = null;
string username = GetUserName();
using (var ctx = new PrincipalContext(ContextType.Domain))
using (var user = UserPrincipal.FindByIdentity(ctx, username))
{
if (user != null)
{
output = user.GetGroups() //this returns a collection of principal objects
.Select(x => x.SamAccountName) // select the name. you may change this to choose the display name or whatever you want
.ToArray(); // convert to string array
}
}
用户名被正确识别,并且在 localhost 和服务器上传递了相同的值,所以这不是问题。线路:
using (var user = UserPrincipal.FindByIdentity(ctx, username))
返回异常:
类型异常 'System.DirectoryServices.DirectoryServicesCOMException' 发生在 System.DirectoryServices.AccountManagement.dll 但未在其中处理 用户代码
这可能是 IIS 设置中的问题,但我不知道是什么。我尝试将 DefaultAppPool 的标识(Web 和 API 分配到的应用程序池)设置为 NetworkService,但没有帮助。
【问题讨论】:
-
试过了,没用。如果我使用 DefaultAppPool 的事实与它有任何关系,请不要这样做,但仍然如此。
标签: c# .net windows active-directory