7

I'm trying to figure out how if there's a way to make a hostname resolve to a certain IP without DNS or host file modification.

Using C#, I'm making a request from client to server, but I have to use the hostname in the request so that the certificates will properly authenticate the request. However, my program is meant to run without admin rights, so I can't modify the host file. I have the correct IP and the host name, is there any other way to make the computer resolve the host name to the IP?

Cannoliopsida
  • 3,044
  • 5
  • 36
  • 61

4 Answers4

2

It looks like the simplest way to solve this is to create a service with the rights to modify the host file, then invoke that service from the main program. The service runs a single command and exits. Since a service can have elevated status, you can essentially encapsulate admin rights inside a standard user program.

Cannoliopsida
  • 3,044
  • 5
  • 36
  • 61
1

If you're making an HTTP request, then you don't need to resolve the hostname; use the IP address in the URL and pass the host header in your HTTP request.

HttpWebRequest.Host Property

Update: sorry didn't see the certificates requirements. I think you should be able to modify the hosts file during installation (because installation usually happens under admin rights). Add the host name you're interested in to point to 127.0.0.1 (local machine). Then, your app can open a listening socket and act as a proxy, channeling the data to the actual Web server. This may or may not work depending on the client having a firewall enabled.

Mr. TA
  • 5,230
  • 1
  • 28
  • 35
  • 1
    If he does that SSL certificate validation will fail because the name on certificate likely has an actual DNS name on it, which will not match the IP used in the `Host` property. This is what he's trying to avoid. – CodingGorilla Jun 11 '12 at 18:31
  • I'm using RasDial to make a connection, so it looks like (I'm no RAS expert) I can give a "phone number" that is either an IP or a hostname. The certification on the connection won't work unless I use the hostname as the phone number. – Cannoliopsida Jun 11 '12 at 18:32
  • Doing it during installation doesn't work (so straight-forwardly), because I have to request the IP and hostname from an external source during the run of the program. That's when I realized the solution (I posted it as an answer); once I have all the necessary info, I create a second service, and do the admin stuff there (you could do it either in the installation or in the run, depending on wheether or not it's a network service or what). – Cannoliopsida Jun 11 '12 at 21:28
-1
   public bool ModifyHostsFile(string sEntryIPAddr, string sEntryURL)
    {
        try
        {
            using (StreamWriter w = File.AppendText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"drivers\etc\hosts")))
            {
                w.WriteLine(sEntryIPAddr+" "+ sEntryURL);
                return true;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }
    }
-5

this worked for me:

Step 1. Open your Windows start menu, search for the notepad application and then right click the notepad icon.

Step 2. Choose “Run as administrator” and then, while inside notepad, browse to folder (/windows/system32/drivers/etc) that contains the hosts file.

Hayden Thring
  • 1,718
  • 17
  • 25