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.
-
4is this important, SLaks? i think this is a reasonable question...Atmocreations– Atmocreations2009-09-11 18:38:20 +00:00Commented Sep 11, 2009 at 18:38
-
It would help us give a better answer, especially if we knew what kind of intranets he's looking for.SLaks– SLaks2009-09-11 19:18:36 +00:00Commented Sep 11, 2009 at 19:18
11 Answers
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.
3 Comments
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
- 10.xxx.xxx.xxx,
- 172.16.xxx.xxx through 172.31.xxx.xxx, and
- 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
2 Comments
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.
Comments
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)
2 Comments
.local used before. I've seen one environment where the intranet is hosted on subdomains of the company's external domain name.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.
Comments
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 Comment
There is no way to determine if the DNS entry is internal or public by just analyzing the hostname. You will first have to resolve the hostname to its IP address, you can find an example using C# here: Resolve HostName to IP
After that, you can use the following regex against the returned IP address. If there is a match, the address is internal.
(^127.)|(^10.)|(^172.1[6-9].)|(^172.2[0-9].)|(^172.3[0-1].)|(^192.168.)
You can read the following SO answer for more information about the above regex and internal IP addresses: Private IP Address Identifier in Regular Expression
Comments
As mentioned here you can use System.Security.Policy.Zone.CreateFromUrl to check as which zone Windows considers a url.
Comments
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.