C# 如何获取域控制器IP地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/492391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:30:40  来源:igfitidea点击:

How to get domain controller IP Address

c#networkingactive-directorynetwork-programming

提问by netseng

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

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

采纳答案by netseng

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

再次感谢

回答by Mike Marshall

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

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

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

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

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

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

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

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

麦克风

回答by barneytron

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);