Active Directory 用户密码更改错误
- 作者: 听说名字长的都长的非常非常的叼是么
- 来源: 51数据库
- 2022-10-28
问题描述
我正在尝试重置 Active Directory 用户的密码,但出现错误,以下是我的代码:
Hi I am trying to reset password of Active Directory User But I Am getting error,Following is my Code:
public string ChangePassword(string Identity,string OldPassword, string Password)
{
string success = "Success";
try
{
DirectoryEntry UserEntry = null;
DirectoryEntry entry = new DirectoryEntry("LDAP://.../DC=Domain,DC=COM", Identity, OldPassword);
DirectorySearcher search = new DirectorySearcher(entry);
SearchResult resultsearch = search.FindOne();
if (resultsearch == null)
{
success = "User Not Found In This Domain";
}
else
{
success = "find";
UserEntry = resultsearch.GetDirectoryEntry();
UserEntry.Username = @"Domain*****istrator";
UserEntry.Password = "password";
UserEntry.AuthenticationType = AuthenticationTypes.None;
if (UserEntry == null)
success = "User Not Found In This Domain";
else
{
try
{
success = UserEntry.Username.ToString();
UserEntry.Invoke("ChangePassword", new object[] { OldPassword, Password });
UserEntry.CommitChanges();
}
catch (Exception ex)
{
success = ex.ToString();
}
}
}
}
catch (Exception ex)
{
success = ex.ToString();
}
所以我在 UserEntry.Invoke("ChangePassword", new object[] { OldPassword, Password }); 中遇到错误UserEntry.CommitChanges();
错误:
System.Runtime.InteropServices.COMException (0x80020006): Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)
at WebService.ChangePassword(String Identity, String OldPassword, String Password) in c:inetpubwwwrootWebSite1App_CodeWebService.cs:line 370
推荐答案
如果您使用的是 .NET Framework 3.5 或更高版本,下面的代码将解决问题.省略类定义.
If you are using .NET Framework 3.5 or later, the code below will solve the problem. Class definition is omitted.
using System.DirectoryServices.AccountManagement;
public static string ChangePassword(string *****User, string *****Password,
string domain, string container, string userName, string newPassword)
{
try
{
PrincipalContext principalContext =
new PrincipalContext(ContextType.Domain, domain, container,
*****User, *****Password);
UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, userName);
if (user == null) return "User Not Found In This Domain";
user.SetPassword(newPassword);
return user.Name;
}
catch (Exception ex)
{
return ex.Message;
}
}
用法:
ChangePassword(@"DOMAIN*****istrator", "password", "DOMAIN", "DC=Domain,DC=COM", userName, newPassword);
为 .NET 2.0 框架添加了一个版本.
Added a version for .NET 2.0 framework.
.NET 2.0 的更改密码方法:
A change password method for .NET 2.0:
public static string ChangePassword20(string *****User, string *****Password,
string container, string domainController, string userName, string newPassword)
{
const AuthenticationTypes authenticationTypes = AuthenticationTypes.Secure |
AuthenticationTypes.Sealing | AuthenticationTypes.ServerBind;
DirectoryEntry searchRoot = null;
DirectorySearcher searcher = null;
DirectoryEntry userEntry = null;
try
{
searchRoot = new DirectoryEntry(String.Format("LDAP://{0}/{1}",
domainController, container),
*****User, *****Password, authenticationTypes);
searcher = new DirectorySearcher(searchRoot);
searcher.Filter = String.Format("sAMAccountName={0}", userName);
searcher.SearchScope = SearchScope.Subtree;
searcher.CacheResults = false;
SearchResult searchResult = searcher.FindOne(); ;
if (searchResult == null) return "User Not Found In This Domain";
userEntry = searchResult.GetDirectoryEntry();
userEntry.Invoke("SetPassword", new object[] { newPassword });
userEntry.CommitChanges();
return "New password set";
}
catch (Exception ex)
{
return ex.ToString();
}
finally
{
if (userEntry != null) userEntry.Dispose();
if (searcher != null) searcher.Dispose();
if (searchRoot != null) searchRoot.Dispose();
}
}
用法:
ChangePassword20(@"DOMAIN*****istrator", "password", "DC=Domain,DC=COM",
"domainControllerName", "userName", "newPassword");
推荐阅读
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
热点文章
团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
0
使用 MSBuild.exe 在发布模式下构建 C# 解决方案
0
当我发布 Web 应用程序时,AfterPublish 脚本不运行
0
构建时 T4 转换的产品仅在下一个构建中使用
0
ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
0
新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
0
如何将条件编译符号(DefineConstants)传递给 msbuild
0
MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
0
NuGet 包还原找不到包,没有源
0
使用 C# 6.0 功能运行 TFS 构建
0
