C#:code错误,而更改活动目录用户的密码 [英] C#: code error while changing the active directory user's password
本文介绍了C#:code错误,而更改活动目录用户的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
C#code
>错误--->>>未知的名称。 (从HRESULT异常:0x80020006
> (DISP_E_UNKNOWNNAME))
和code是这个
使用(的DirectoryEntry条目=新的DirectoryEntry(LDAP://admin-jyt69gl7t.hello/CN=Users,DC=hello))
{
entry.Username =用户名;
entry.Password = strOldPassword;
DirectorySearcher从搜索=新DirectorySearcher从(输入);
尝试
{
searcher.FindOne();
entry.AuthenticationType = AuthenticationTypes.Secure;
entry.Invoke(的ChangePassword,新的对象[] {strOldPassword,strNewPassword});
// oDE.Invoke(SetPassword,新的对象[] {strNewPassword});
entry.CommitChanges();
}
赶上(例外excep)
我收到此异常
>未知的名称。 (从HRESULT异常:0x80020006
> (DISP_E_UNKNOWNNAME))
解决方案
只需按照以下
在code 使用System.DirectoryServices中;
私人的DirectoryEntry的getUser(用户名字符串)
{
的DirectoryEntry德= GetDirectoryObject();
DirectorySearcher从deSearch =新DirectorySearcher从();
deSearch.SearchRoot =日;
deSearch.Filter =(及(objectClass的=用户)(SAM帐户名=+用户名+));
deSearch.SearchScope = SearchScope.Subtree;
信息搜索结果的结果= deSearch.FindOne();
如果(!(结果== NULL))
{
// **这是最重要的一行**
DE =新的DirectoryEntry(results.Path,用户名,密码,AuthenticationTypes.Secure);
返回去;
}
其他
{
返回null;
}
}
私人的DirectoryEntry GetDirectoryObject()
{
的DirectoryEntry颂歌;
ODE =新的DirectoryEntry(LDAP://192.168.1.101,用户名,密码,AuthenticationTypes.Secure);
返回颂歌;
}
公共静态布尔的ChangePassword(用户名字符串,字符串strOldPassword,串strNewPassword)
{
布尔passwordChanged = FALSE;
的DirectoryEntry ODE =的getUser(用户名,strOldPassword);
如果(ODE!= NULL)
{
尝试
{
//更改密码。
oDE.Invoke(的ChangePassword,新的对象[] {strOldPassword,strNewPassword});
passwordChanged = TRUE;
}
赶上(例外前)
{
的Debug.WriteLine(错误更改密码原因:+ ex.Message);
}
}
返回passwordChanged;
}
C# code
> error--->>>Unknown name. (Exception from HRESULT: 0x80020006
> (DISP_E_UNKNOWNNAME))
and the code is this
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)
I am getting this exception
> Unknown name. (Exception from HRESULT: 0x80020006
> (DISP_E_UNKNOWNNAME))
解决方案
Simply follow the code under
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#:code错误,而更改活动目录用户的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文