我正在Active Directory中创建新用户,但在我检索创建的用户后创建它时,即将发生错误。发生操作错误"
以下是我的代码
private static DirectoryEntry GetUser(string userName)
{
var de = Utility.GetDirectoryObject();
var deSearch = new DirectorySearcher
{
SearchRoot = de,
Filter = "(&(objectClass=user)(sAMAccountName=" + userName + "))",
SearchScope = SearchScope.Subtree
};
var results = deSearch.FindOne();
if (results != null)
{
de = new DirectoryEntry(results.Path, Utility.AdUser, Utility.AdPassword, AuthenticationTypes.Secure);
return de;
}
return null;
}
答案 0 :(得分:0)
试试这个:
var deSearch = new DirectorySearcher
{
SearchRoot = de,
Filter = $"(&(objectClass=person)(sAMAccountName={userName}))",
SearchScope = SearchScope.Subtree
};