【发布时间】:2015-04-29 15:10:21
【问题描述】:
我需要更改连接在同一网络中的计算机上的 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,它会返回错误。
如何检查与服务器的连接是否建立?
【问题讨论】:
标签: c# asp.net server principalcontext