如何以编程方式检查我连接到哪个域?

发布于 2024-07-26 15:00:17 字数 511 浏览 6 评论 0原文

如果我在工作中连接到本地 LAN,我需要让我的应用程序通过内部 IP 访问我们的服务器,否则,我需要在野外时使用我们的外部 IP。

目前,我只是尝试通过本地IP进行连接,如果失败则尝试外部...但是超时时间有点太长,我想知道是否有一种方法可以在尝试之前找出机器连接到的域。

编辑:帕特里克> 本质上,该应用程序在每天连接到本地网络几次的平板电脑上运行。 通过网络连接的次数与本地连接的次数大致相等。

所有计算机在连接​​到网络时都有一个域帐户(并且具有类似于“LOCTabletx”的命名约定的域帐户,其中 x 是在机器幻影时给定的数字。

我正在寻找一种快速的方法查看机器是否连接到我们的本地网络或互联网。使用 Environment.UserDomainName 获取 LOCTabletx 而不是域名

编辑

如果它对任何人有帮助,我只是。尝试 DNS 解析我可以保证将在网络上的计算机的名称(其中一台服务器),它对我来说效果很好。

If I'm connected to the local LAN here at work, I need to have my app access our server via an internal IP, otherwise, I'll need to use our external IP when out in the wild.

Currently, I just try to connect via the local IP and then try the external if it fails... but the timeout takes a bit too long and I was wondering if there's a way to find out what domain the machine is connected to before trying.

Edit: Patrick> Essentially, the app runs on a tablet pc that is connected to the local network a couple of times a day. It's roughly equal between the number of times it connects over the network and the times that it connects locally.

All machines have a domain account when they are connected to the network (and have domain accounts with a naming convention of like "LOCTabletx" where x is a number given to the machine when it's ghosted.

What I'm looking for is a fast way to see if the machine is connected on our local network or the internet. Using Environment.UserDomainName gets me LOCTabletx and not the domain name.

EDIT

If it helps anyone, I just try to DNS Resolve the name of a machine that I can guarantee will be on the network (one of the servers). It works sufficiently well for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

榆西 2024-08-02 15:00:17

您是否尝试过:

Environment.UserDomainName

您还可以查看计算机上的活动 IP 地址,并查询在您的本地网络上运行的 IP 地址...

var x = NetworkInterface.GetAllNetworkInterfaces()
    .Where(ni => ni.OperationalStatus == OperationalStatus.Up)
    .SelectMany(ni => ni.GetIPProperties().UnicastAddresses);

// do something with the collection here to determine if you're on the right network.
// just looping & printing here for example.
foreach (var item in x)
{        
    Console.WriteLine(item.Address);
}

确定您所在的网络后,您可以订阅到 System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged 事件,用于在应用程序运行时处理计算机跳跃网络。

Have you tried:

Environment.UserDomainName

You could also take a look at the active IP addresses on the machine, and query for one that works on your local network...

var x = NetworkInterface.GetAllNetworkInterfaces()
    .Where(ni => ni.OperationalStatus == OperationalStatus.Up)
    .SelectMany(ni => ni.GetIPProperties().UnicastAddresses);

// do something with the collection here to determine if you're on the right network.
// just looping & printing here for example.
foreach (var item in x)
{        
    Console.WriteLine(item.Address);
}

And after you've figured out the network that you're on, you could subscribe to the System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged event to handle your computer jumping networks while your app is running.

九八野马 2024-08-02 15:00:17
System.Environment.UserDomainName
System.Environment.UserDomainName
娇俏 2024-08-02 15:00:17
System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType 

当未连接到网络时,这将返回“NTLM”。

System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType 

This will return "NTLM" when not connected to the network.

油饼 2024-08-02 15:00:17

您想要查看网络位置感知 API。 它适用于 Windows Vista 或更高版本,它允许您以编程方式发现您连接到的网络,并在网络发生变化时收到通知。

您可能对“这是家庭/工作/公共网络吗?”的形式很熟悉。 对话框。

You want to look at the Network Location Awareness API. Available on Windows Vista or later, it allows you to programmatically discover which network you're connected to, and be notified when this changes.

It might be familiar to you in the form of the "Is this a home / work / public network?" dialog box.

明月松间行 2024-08-02 15:00:17

另一种方法,但我不知道它实际上是否比其他解决方案更好是:

System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType

这是一个在活动目录下返回“Kerberos”的字符串。 但不确定未连接到域时会说什么。

Another way, but I don't know if it is actually any better than the other solutions is:

System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType

this is a string that returns "Kerberos" under active directory. Not sure what it would say when not connected to the domain though.

メ斷腸人バ 2024-08-02 15:00:17

Environment.UserDomainName ...如果您未加入域,则为您提供计算机名称。 如果您加入域,它会为您提供域名。 如果您将加入某个域的计算机从网络中取出并“放入野外”,即使您重新启动并重新登录(到您的计算机),Environment.UserDomainName 将继续提供域名。域帐户)。 您的计算机会将域凭据缓存大约 30 天。

如果您登录到您的机器帐户,那么您将获得机器名称。

Environment.UserDomainName ...gives you the machine name if you are not joined to a domain. It gives you the domain name if you are joined to a domain. If you take a machine that is joined to a domain off the network and out "into the wild", Environment.UserDomainName will continue to provide the domain name even if you reboot and log back in (to your domain account). Your machine caches the domain credentials for about 30 days.

If you log in to your machine account, then you will get the machine name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文