9

Using C# how would one determine if a URL is an intranet URL? I would like some code to do something if a URL is an intranet one vs public.

Omar Shahine
  • 1,935
  • 3
  • 22
  • 30

9 Answers9

13

you cannot implicitely know. if your intranet urls look like fully qualified domain names then it's difficult to tell. the only way to tell is to query two different DNS-servers (your own and a public one). If both return the same result, then it's an internet domain. if the public DNS-server isn't able to resolve the address, then it's most likely an intranet domain.

Atmocreations
  • 9,923
  • 15
  • 67
  • 102
  • I've seen insane setups where the intranet site is named something like "intranet.com", which is internally resolved to the intranet, but is a real live domain owned by an arbitrary company on the outside. You'll have to not only check that it resolves from both DNS servers, but that it resolves to the same thing. – rmeador Sep 11 '09 at 18:38
  • This solution (resolving the name to an IP) will work if you have a priori knowledge of what is an internal IP versus an external IP. If you add a little bit of configuration (a list of valid IP ranges for the intranet) you have the problem solved. Not a pretty solution, but it will work. – Blue Toque Sep 11 '09 at 19:02
  • Another problem is if the host resolves to a routable IP because your organization has valid Class A IP's which it uses as intranet IP's. It makes life very hard with .Net and the like. – user7116 Dec 04 '09 at 21:53
10

Do you know the internal subnets (in terms of IP addresses)? If so, I'd just resolve the host name and see if it's internal that way.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
8

if the url resolves to a tcpIp address which is one of the IP addresses set aside as a private IPAddress, then it is definitely on your Intranet. these are

  1. 10.xxx.xxx.xxx,
  2. 172.16.xxx.xxx through 172.31.xxx.xxx, and
  3. 192.168.xxx.xxx

if it resolves to any other IP address it might still be on your intranet, but it has a public IP address so it is potentially accessible from outside the Intranet

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • +1, As i detailed in a comment to another response some organizations, as you point out, use routable IP's as intranet IP's. Confusingly enough .Net's security is based in a large part on these when using network shares. If you're lucky enough to have those IP's, then I find it to be a valid way to check. – user7116 Dec 04 '09 at 21:57
  • This solution will also break for IP6. Although IP6 has reserved private IP address, the general recommendation is not to use them to avoid address translation. – Reza Oct 13 '14 at 17:36
1

The simple, and not perfect but works for the 80% case is to simply check if the URL has a period in it. I know Intranet URLs can be fully qualified, but in my experience (at Microsoft) most do not, and this works pretty well.

Omar Shahine
  • 1,935
  • 3
  • 22
  • 30
0

In general, there is no reliable way to tell an intranet URL from an Internet URL. If the intranet is available to your program, then it will look just like the Internet, and if not, then you still won't know whether the URL is supposed to be a working intranet URL or is just a (temporarily) broken Internet URL.

You will need some special knowledge, such as the domain names or IPs of the servers that are providing the intranet, in order to tell them apart.

Jeremy Bourque
  • 3,533
  • 1
  • 21
  • 18
0

If you want to determine whether any given URL is an intranet url in any company (as opposed to specializing your code for one particular company), I wish you luck.

Usually, but not always, itranet urls do not have a TLD (Top Level Domain, such as .com). However, I've seen some that do.

Almost always (AFAIK), intranet domains will resolve to a similar IP address as the computer's current address. Note that I did not say the same subnet; large intranets can have multiple subnets. Also note that if the computer is not in a corporate intranet, there will be regualr domain names that resolve to similar IP addresses. (Unless the computer is behind NAT)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I always use .local (http://en.wikipedia.org/wiki/.local) for internal namespaces. i.e. supermegacorp.com would be supermegacorp.local internally. If everyone did this, the solution to this problem would be trivial. Of course .local might not work for everyone, however I've yet to find somewhere where it wouldn't work. – Bryan Sep 11 '09 at 18:39
  • 1
    I've never seen `.local` used before. I've seen one environment where the intranet is hosted on subdomains of the company's external domain name. – SLaks Sep 11 '09 at 19:24
0

I am not a C# programmer, so I can't offer you code, but the basic method would involve comparing the hostname part of the URL to that of server(s) in your intranet. Or, if your URL just uses an IP not a DNS name, compare the IP to that of your intranet server(s).

Crack the URL using string manipulation - though I imagine C# must have a URL class that will do this for you - and extract the hostname, compare it to a list of servers.

Eric M
  • 1,027
  • 2
  • 8
  • 21
0

You could interrogate internet explorer to see if the domain would match the list of accepted intranet domains.

Registry key is HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains

There if the domain has a value for 1 for the protocol it will be deemed an intranet domain. You'll need to perform a nslookup to gather the 'real' address.

-1

To add to what others have said, Intranet do not have any of the know top-level domain in its address. I've worked in a few companies and all the Intranet were something like:

http://developments/admin - you will notice that there's no top-level domain at all. So, it resolves to a computer within the network. Again, as the name suggests, you are not likely to access it beyond the corporate environment.

Helen Neely
  • 4,666
  • 8
  • 40
  • 64