如何获取域控制器IP地址

时间:2022-06-15 05:24:56

How do you get the Domain Controller IP address programmatically using C#?

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

3 个解决方案

#1


Here's how I would do it.

这是我怎么做的。

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

您需要使用System.Net和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);

#2


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

#3


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

那么这里是如何获得它的一般工作流程,如MS网站所述:

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

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

以下是来自PInvoke.net的链接,用于调用引用的DsGetDcName函数:

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.

您可以按照第一个链接中的说明进行操作并执行原始DNS A记录查询,但我认为PInvoke调用应该可以解决问题。

Hope that helps,

希望有所帮助,

Mike

#1


Here's how I would do it.

这是我怎么做的。

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

您需要使用System.Net和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);

#2


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

#3


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

那么这里是如何获得它的一般工作流程,如MS网站所述:

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

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

以下是来自PInvoke.net的链接,用于调用引用的DsGetDcName函数:

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.

您可以按照第一个链接中的说明进行操作并执行原始DNS A记录查询,但我认为PInvoke调用应该可以解决问题。

Hope that helps,

希望有所帮助,

Mike