EPiServer LicenseException: x.x.x.x is not a valid ip-address

I got a support question on this Exception today. First thought is that the ip-address was misstyped or the wrong license.config file was used but ipconfig /all and the IPRestiriction tag in license.config matched!

How does EPiServer validate the ip-address?

The code to check is quite simple. It is using the DNS service to lookup the name of the computer and then lookup all ip-addresses for that computer name.

foreach (IPAddress address in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
    if (!IPAddress.IsLoopback(address) && this._ip.Equals(address))
        this._isValid = true;

Troubleshooting License Exception  

A quick test is to add a small debug.aspx file to find out what values are returned.

<%@ Page Language="C#" AutoEventWireup="true"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
        foreach (System.Net.IPAddress address in System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList)
            ListBox1.Items.Add(address.ToString()+ "-" + System.Net.IPAddress.IsLoopback(address).ToString());
<html xmlns="http://www.w3.org/1999/xhtml
 " >
<head runat="server"><title></title></head>
    <form id="form1" runat="server">
    <h1>Check IP-addresses</h1>
    <p>Current host name: <%= System.Net.Dns.GetHostName() %></p>
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Test" />

Resolution to LicenseException in this case

Running the test aboved showed us that the IP-address returned was the loopback address instead of the computers ip-address.

Since this setup is an enterprise site with a lot of diffrent host names pointing to diffrent start pages the local hosts-file (at C:\Windows\System32\drivers\etc\hosts) was edited and all known host names was added pointing to to make it possible to test. One entry was accidentally added with the machine name and when we removed that line everything started to work!