Search code examples
.nethttpglobalization

What .net StringComparison type is best for hostname comparisons?


For Equals, should I do

hostname.Equals(theHostName, StringComparision.OrdinalIgnoreCase) or hostname.Equals(theHostName, StringComparision.InvariantCultureIgnoreCase) ?

In 'IsAllowedHostName' should I do

hostname.IndexOf(badSubstring, StringComparison.OrdinalIgnoreCase) >= 0 or hostname.IndexOf(badSubstring, StringComparison.InvariantCultureIgnoreCase) >= 0 ?

Or for hostnames, is the correct answer neither of these?


Solution

  • You should read the RFCs that define the DNS system. DNS host labels are case insensitive and may contain only the letters A-Z, 0-9 and '-'. A DNS name consists of 1 or more labels connected with an ASCII '.' (period, full stop).

    Use StringComparision.OrdinalIgnoreCase.