我需要更改连接在同一网络中的机器上的 windows 用户的密码。为此,我使用PrincipalContext
方法连接机器,然后获取指定的用户,最后更改密码。
private bool ChangeUserPassword(string serverName,string serverip,string adminUserName, string adminUserPassword, string userName, string newIISPassword)
{
using (var context = new PrincipalContext(ContextType.Machine, serverName, adminUserName, adminUserPassword))
using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName))
{
user.SetPassword(newIISPassword);
user.Save();
}
}
问题是,当我提供“ServerName”但与“ServerIp”一起使用时,有时我无法连接到服务器,反之亦然。如果已连接context.ConnectedServer
将显示服务器名称,否则将显示未知路径或错误的用户名错误。
如果没有建立连接,我可以使用 serverIp 作为参数
using (var context = new PrincipalContext(ContextType.Machine, serverIp, adminUserName, adminUserPassword))
为此,我试图if( context.ConnectedServer != serverName)
知道连接不起作用并将上面的代码放入其中。但是由于没有 ConnectedServer,它将返回错误。
如何检查与服务器的连接是否建立?