I need to change the password of a windows user in a machine which is connected in the same network. For that I am connecting the machine using PrincipalContext
method, then get the specified user and finally change the password.
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();
}
}
The problem is that sometimes I won't get the connection to the server when I am providing 'ServerName' but works with 'ServerIp' and vice versa.
If it is connected context.ConnectedServer
will show the server name, if not it is showing unknown path or bad username error.
If connection is not made I could use serverIp as parameter
using (var context = new PrincipalContext(ContextType.Machine, serverIp, adminUserName, adminUserPassword))
For that I tried if( context.ConnectedServer != serverName)
to know the connection is not working and put the above code inside it.But it will return error since there is no ConnectedServer.
How can I check the connection to the server is made or not?