Frage

Ich muss Abfrage aktuellen Domänencontroller, wahrscheinlich primär auf Benutzer-Kennwort ändern.

(P) DC Name vollständig qualifiziert sein sollte, das heißt DC=pdc,DC=example,DC=com (wie man richtig eine solche Schreibweise nennen?)

Wie kann es mit C # getan werden?

War es hilfreich?

Lösung 3

(erfordert 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)));
}

Andere Tipps

Um die Informationen abzurufen, wenn der DomainController in einer Domäne vorhanden ist, in dem das Gerät gehört nicht, Sie etwas mehr brauchen.

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

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

Wir sind für unsere internen Anwendungen so etwas wie diese verwendet wird.

Wenn Rückkehr etwas wie 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;
}

Wenn Sie das Active Directory kommunizieren suchen, sollten Sie nicht wissen, wo die Domain Klasse.

Wenn Sie ein Benutzerpasswort ändern möchten, können Sie diese Aktionen auf dem Benutzerobjekt aufrufen und Active Directory werden dafür Sorge tragen, dass die Änderungen ordnungsgemäß repliziert.

kopiert von http: //www.rootsilver. com / 2007/08 / how-to-change-a-Benutzer-Passwort

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");
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top