7

現在のドメインコントローラーを照会する必要があります。おそらく、ユーザーパスワードを変更するためにプライマリです。

(P)DC名は完全に修飾されている必要があります。つまり、DC=pdc,DC=example,DC=com(そのような表記に適切に名前を付ける方法は?)

C#を使用してどのように行うことができますか?

4

4 に答える 4

6

DomainController自分のマシンが属していないドメインにが存在する場合に情報を取得するには、さらに何かが必要です。

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

  var domain = System.DirectoryServices.ActiveDirectory.Domain.GetDomain(domainContext);
  var controller = domain.FindDomainController();
于 2012-11-13T14:44:40.213 に答える
2

(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)));
}
于 2010-10-25T14:35:09.983 に答える
2

内部アプリケーションには、このようなものを使用しています。

次のようなものを返す必要があります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;
}
于 2010-10-25T15:23:43.113 に答える
0

Active Directoryとのやり取りを検討している場合は、ほとんどの場合、 FSMOの役割がどこにあるかを知る必要はありません。プログラムからADトポロジを変更したい場合は(私は変更しません)、DomainControllerクラスを調べてください。

ユーザーパスワードを変更する場合は、Userオブジェクトに対してこれらのアクションを呼び出すことができ、ActiveDirectoryは変更が適切に複製されていることを確認します。

http://www.rootsilver.com/2007/08/how-to-change-a-user-passwordからコピー

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");
}
于 2010-10-25T14:36:29.900 に答える