我们如何在c#中创建新用户后从Active Directory获取特定用户

时间:2017-08-21 12:53:05

标签: c# asp.net-mvc web active-directory

我正在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;
    }

1 个答案:

答案 0 :(得分:0)

试试这个:

var deSearch = new DirectorySearcher
    {
        SearchRoot = de,
        Filter = $"(&(objectClass=person)(sAMAccountName={userName}))",
        SearchScope = SearchScope.Subtree
    };