简体   繁体   中英

How to get domain controller IP Address

如何使用C#编程方式获取域控制器IP地址?

Here's how I would do it.

You'll need to use System.Net and System.DirectoryServices.

// get root of the directory data tree on a directory server
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://rootDSE");
// get the hostname of the directory server of your root (I'm assuming that's what you want)
string dnsHostname = dirEntry.Properties["dnsHostname"].Value.ToString();
IPAddress[] ipAddresses = Dns.GetHostAddresses(dnsHostname);

Thanks All,

I done it as in this code

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.DirectoryServices.AccountManagement;
    using System.DirectoryServices.ActiveDirectory;
public doIt()
        {
            DirectoryContext mycontext = new DirectoryContext(DirectoryContextType.Domain,"project.local");
            DomainController dc = DomainController.FindOne(mycontext);
            IPAddress DCIPAdress = IPAddress.Parse(dc.IPAddress);
        }

Thanks again

Well here is the general workflow of how to get it as described at the MS site:

http://support.microsoft.com/kb/247811

Here is the link from PInvoke.net to call the referenced DsGetDcName function:

http://pinvoke.net/default.aspx/netapi32/DsGetDcName.html

You could go down and dirty and do a raw DNS A Record query as described in the first link, but I think the PInvoke call should do the trick.

Hope that helps,

Mike

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM