Determine current domain controller programmatically

前端 未结 4 1638
后悔当初
后悔当初 2020-12-18 02:20

I need to query current domain controller, probably primary to change user password.

(P)DC name should be fully qualified, i.e. DC=pdc,DC=example,DC=com

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-18 02:57

    We are using something like this for our internal applications.

    Should return something like 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;
    }
    

提交回复
热议问题