C#:更改活动目录用户密码时出现代码错误

C#: code error while changing the active directory user#39;s password(C#:更改活动目录用户密码时出现代码错误)
本文介绍了C#:更改活动目录用户密码时出现代码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

C# code     

> error--->>>Unknown name. (Exception from HRESULT: 0x80020006
> (DISP_E_UNKNOWNNAME))

代码是这样的

using (DirectoryEntry entry = new DirectoryEntry("LDAP://admin-jyt69gl7t.hello/CN=Users,DC=hello"))
{
    entry.Username = username;
    entry.Password = strOldPassword;

    DirectorySearcher searcher = new DirectorySearcher(entry);

    try
    {
        searcher.FindOne();
        entry.AuthenticationType = AuthenticationTypes.Secure;
        entry.Invoke("ChangePassword", new object[] { strOldPassword, strNewPassword });
        //  oDE.Invoke("SetPassword", new object[] { strNewPassword });
        entry.CommitChanges();
    }
    catch (Exception excep)

我收到此异常

> Unknown name. (Exception from HRESULT: 0x80020006
> (DISP_E_UNKNOWNNAME))

推荐答案

只需按照下代码

using System.DirectoryServices;


private DirectoryEntry GetUser(string UserName)

{

    DirectoryEntry de = GetDirectoryObject();
    DirectorySearcher deSearch = new DirectorySearcher();
    deSearch.SearchRoot = de;

    deSearch.Filter = "(&(objectClass=user)(SAMAccountName=" + UserName + "))";
    deSearch.SearchScope = SearchScope.Subtree;
    SearchResult results = deSearch.FindOne();

    if (!(results == null))
    {
       // **THIS IS THE MOST IMPORTANT LINE**
       de = new DirectoryEntry(results.Path, "username", "password", AuthenticationTypes.Secure); 
       return de;
    }
    else
    {
       return null;
    }
}

private DirectoryEntry GetDirectoryObject()

{

    DirectoryEntry oDE;
    oDE = new DirectoryEntry("LDAP://192.168.1.101", "username", "password", AuthenticationTypes.Secure);
    return oDE;
}


 public static bool ChangePassword(string UserName, string strOldPassword, string strNewPassword)

        {

            bool passwordChanged = false;

            DirectoryEntry oDE = GetUser(UserName, strOldPassword);

            if (oDE != null)
            {
                try
                {
                    // Change the password.
                    oDE.Invoke("ChangePassword", new object[] { strOldPassword, strNewPassword });
                    passwordChanged = true;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Error changing password. Reason: " + ex.Message);

                }
            }
            return passwordChanged;
        }

这篇关于C#:更改活动目录用户密码时出现代码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

What#39;s the difference between retrieving WindowsPrincipal from WindowsIdentity and Thread.CurrentPrincipal?(从 WindowsIdentity 和 Thread.CurrentPrincipal 检索 WindowsPrincipal 之间有什么区别?)
How do I find a user#39;s Active Directory display name in a C# web application?(如何在 C# Web 应用程序中查找用户的 Active Directory 显示名称?)
How can I authenticate against Active Directory in Nancy?(如何在 Nancy 中对 Active Directory 进行身份验证?)
How to get a username in Active Directory from a display name in C#?(如何从 C# 中的显示名称获取 Active Directory 中的用户名?)
Oauth 2 token for Active Directory accounts(Active Directory 帐户的 Oauth 2 令牌)
Working with DirectoryServices in ASP.NET Core(在 ASP.NET Core 中使用 DirectoryServices)