13

The Windows Hosts file allows you to associate an IP to a host name that has far greater freedom than a normal Internet domain name. I'd like to create a function that determines if a given name would be a valid "host" file domain name.

Based on this answer and experimentation of what works and doesn't, I came up with this function:

private static bool IsValidDomainName(string domain)
{
    if (String.IsNullOrEmpty(domain) || domain.Length > 255)
    {
        return false;
    }

    Uri uri;

    if (!Uri.TryCreate("http://" + domain, UriKind.Absolute, out uri))
    {
        return false;
    }

    if (!String.Equals(uri.Host, domain, StringComparison.OrdinalIgnoreCase) || !uri.IsWellFormedOriginalString())
    {
        return false;
    }

    foreach (string part in uri.Host.Split('.'))
    {
        if (part.Length > 63)
        {
            return false;
        }
    }

    return true;
}

It also has the benefit that it should work with Unicode names (where a basic regex would fail).

Is there a better/more elegant way to do this?

UPDATE: As suggested by Bill, the Uri.CheckHostName method almost does what I want, but it doesn't allow for host names like "-test" that Windows allows in a "hosts" file. I would special case the "-" part, but I'm concerned there are more special cases.

Community
  • 1
  • 1
Jeff Moser
  • 18,820
  • 6
  • 58
  • 83

2 Answers2

37

How about the System.Uri.CheckHostName() method?

private static bool IsValidDomainName(string name)
{
    return Uri.CheckHostName(name) != UriHostNameType.Unknown;
}

Why do the work yourself?

reformed
  • 3,922
  • 9
  • 51
  • 77
Bill
  • 2,261
  • 4
  • 20
  • 24
  • 6
    Note that even passing null or empty string returns the correct answer. – Bill Jun 09 '09 at 00:07
  • I hadn't seen this method before. I missed it in my Reflector-ing. It's better than my "http" prefix hack, but the hosts file allows "-test" for a name, but this function says it's invalid. – Jeff Moser Jun 09 '09 at 00:15
  • The "http" prefix hack correctly allows the "-test" case, but just using Uri.CheckHostName does not allow it. – Jeff Moser Jun 09 '09 at 00:18
  • 1
    I am not aware of all that is allowed in the hosts file, for example, "-test". – Bill Jun 09 '09 at 10:15
  • this is no usefull function. actualy it accepts almost all what you type in. So `afdasdfa` is valid as well. Tested it in all thinkable ways! – Dwza Feb 24 '17 at 14:46
  • 1
    @Dwza While `afdasdfa` is not a [fully qualified domain name](https://en.wikipedia.org/wiki/Fully_qualified_domain_name), it is still a valid [hostname](https://en.wikipedia.org/wiki/Hostname) like, for example, [`localhost`](https://en.wikipedia.org/wiki/Localhost) host name. – Lightman Mar 26 '17 at 11:19
0

These methods are not reliable as you get some response even if the domain name is fake like "fasdfasdfasd.com".

The best way is to send a WebResponse and wait for the response from the domain. Here is the complete code and explanations of this process (long code snippet so not copy-pasting here).

http://www.dotnetfunda.com/articles/show/1072/validating-domain-name-in-aspnet

Thanks

Community
  • 1
  • 1
Sheo Narayan
  • 1,170
  • 2
  • 13
  • 16
  • 5
    I disagree - this is not the best way. it is the most robust way (because now you know without a shadow of a doubt that the domain is valid). However.. what if the domain isn't registered or hosted anywhere, or isn't available to you over the network, or is just down for some reason? This will fail on a valid domain in those cases at least and possible more. – Richard Barker Nov 02 '16 at 13:30
  • You should query the whois instead to make sure that the domain has been registered – Yanga Jul 07 '17 at 16:07