【发布时间】:2019-06-04 13:26:58
【问题描述】:
我正在尝试使用具有连接 Active Directory 的完全访问权限的服务帐户凭据连接到 Active Directory,但无法加载用户的属性详细信息。
当我使用“miminstall”帐户登录时会发生这种情况,该帐户无权从 AD 中获取用户详细信息,但在我的应用程序中,我传递了在 AD 中具有访问权限的帐户凭据。
当我使用对 Active Directory 具有完全连接访问权限的不同用户 (adma) 运行 Visual Studio 时,我能够毫无问题地连接和获取用户详细信息。
即使在代码中传递了 adma 帐户凭据,我也不知道为什么会发生这种情况。
public string getADattributes(string DN, string operation)
{
string path = "LDAP://xyz.com";
DirectoryEntry directoryEntry = new DirectoryEntry(path, "xyz\\adma", "abc", AuthenticationTypes.Secure);
using (directoryEntry)
{
DirectorySearcher objDSearcher = new DirectorySearcher();
objDSearcher.Filter = "(distinguishedName=" + DN + ")";//search user in AD using DN
objDSearcher.PropertiesToLoad.Add("whenCreated");
objDSearcher.PropertiesToLoad.Add("whenChanged");
objDSearcher.PropertiesToLoad.Add("EmployeeID");
objDSearcher.SearchScope = SearchScope.Subtree;
SearchResult result = objDSearcher.FindOne();
if (result != null)//if count!=0 that means user exist in ad
{
string createdDate = "";
string modifiedDate = "";
string employeeID = "";
if (result.Properties["whenCreated"].Count >0)
{
//able to come inside if statement when running visual studio using adma account but not when runnning with login account i.e., miminstall
createdDate = result.Properties["whenCreated"][0].ToString();
}
if(result.Properties["whenChanged"].Count>0)
{
modifiedDate = result.Properties["whenChanged"][0].ToString();
}
if(result.Properties["EmployeeID"].Count > 0)
{
employeeID = result.Properties["EmployeeID"][0].ToString();
}
}
return null;
}
}
【问题讨论】:
-
当您执行此操作时,它将使用您登录 Windows 时使用的帐户。如果您希望代码以特定用户身份执行,请在此处查看 WindowsIdentity.Impersonate:docs.microsoft.com/en-us/dotnet/api/…
标签: c# .net active-directory