Question

Je dois interroger le contrôleur de domaine actuel, probablement primaire de changer le mot de passe de l'utilisateur.

(P) nom DC doit être qualifié, à savoir DC=pdc,DC=example,DC=com (comment nommer correctement cette notation?)

Comment peut-il être fait en utilisant C #?

Était-ce utile?

La solution 3

(nécessite System.DirectoryServices.AccountManagement.dll):

using (var context = new System.DirectoryServices.AccountManagement.PrincipalContext(ContextType.Domain))
{
    string server = context.ConnectedServer; // "pdc.examle.com"
    string[] splitted = server.Split('.'); // { "pdc", "example", "com" }
    IEnumerable<string> formatted = splitted.Select(s => String.Format("DC={0}", s));// { "DC=pdc", "DC=example", "DC=com" }
    string joined = String.Join(",", formatted); // "DC=pdc,DC=example,DC=com"

    // or just in one string

    string pdc = String.Join(",", context.ConnectedServer.Split('.').Select(s => String.Format("DC={0}", s)));
}

Autres conseils

Pour récupérer les informations lorsque le DomainController existe dans un domaine dans lequel la machine ne fait pas partie, vous avez besoin quelque chose de plus.

  DirectoryContext domainContext =  new DirectoryContext(DirectoryContextType.Domain, "targetDomainName", "validUserInDomain", "validUserPassword");

  var domain = System.DirectoryServices.ActiveDirectory.Domain.GetDomain(domainContext);
  var controller = domain.FindDomainController();

Nous utilisons quelque chose comme ça pour nos applications internes.

devrait retourner quelque chose comme DC=d,DC=r,DC=ABC,DC=com

public static string RetrieveRootDseDefaultNamingContext()
{
    String RootDsePath = "LDAP://RootDSE";
    const string DefaultNamingContextPropertyName = "defaultNamingContext";

    DirectoryEntry rootDse = new DirectoryEntry(RootDsePath)
    {
        AuthenticationType = AuthenticationTypes.Secure;
    };
    object propertyValue = rootDse.Properties[DefaultNamingContextPropertyName].Value;

    return propertyValue != null ? propertyValue.ToString() : null;
}

Si vous êtes à la recherche d'interagir Active Directory, vous ne devriez pas avoir à savoir où le class DomainController.

Si vous voulez changer un mot de passe de l'utilisateur, vous pouvez appeler ces actions sur l'objet utilisateur et Active Directory vous assurerez que les changements sont correctement reproduits.

http: //www.rootsilver. com / 2007/08 / how-to-changement un utilisateur mot de passe

public static void ChangePassword(string userName, string oldPassword, string newPassword)
{
        string path = "LDAP://CN=" + userName + ",CN=Users,DC=demo,DC=domain,DC=com";

        //Instantiate a new DirectoryEntry using an administrator uid/pwd
        //In real life, you'd store the admin uid/pwd  elsewhere
        DirectoryEntry directoryEntry = new DirectoryEntry(path, "administrator", "password");

        try
        {
           directoryEntry.Invoke("ChangePassword", new object[]{oldPassword, newPassword});
        }
        catch (Exception ex)  //TODO: catch a specific exception ! :)
        {
           Console.WriteLine(ex.Message);
        }

        Console.WriteLine("success");
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top