Well! I have a server with a configured domain and AD. Name it A. I need to connect to it from another PC B , which is part of the network (however it is not associated with a domain).
So, for this case, the only change was made to the line -
using (PrincipalContext pc = new PrincipalContext(ContextType.Machine, "SERVER_IP_HERE", null, ContextOptions.Negotiate, @"DomainNameOfRemoteMachineHere\Administrator", "MyPassword")
So here is the code -
static void Main() { try { using (PrincipalContext pcRoot = new PrincipalContext(ContextType.Machine, "IP_Address", null, ContextOptions.Negotiate, @"domainNameHere\Administrator", "SomePass")) { //Get an access denied error here trying to connect to the Context var group = GroupPrincipal.FindByIdentity(pcRoot, "Administrators"); var pc = new PrincipalContext(ContextType.Domain, "FQDNOFDomainHere"); var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, "vssaini"); if (group == null) { Console.WriteLine("Group not found."); return; } if (user == null) Console.WriteLine("User not found."); else group.Members.Add(user); group.Save(); } } catch (Exception exc) { Console.WriteLine(exc); } // Wait for output Console.ReadKey(); }
And when testing, it worked smoothly.
source share