Best way to identify a computer in LAN C# I have a web application to manage computers. Every registered computer is locally querying the latest information and updates the database with it in a 10 minute cycle.

But the computer is only allowed to update its information and nothing else. Currently I'm identifying the computer like this:

I get its hostname from the "remote_addr" variable

string senderHostname = (Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName).Split('.')[0];


Then I compare the hostname of the sender computer with the hostname of the computer which should be updated.

if (senderHostname.Equals(targetHostname, StringComparison.InvariantCultureIgnoreCase)) {
 // allowed
}
else {
 // forbidden
}


If they are the same, then I'm allowing the update, otherwise not.

I know that this code is very very ugly and I would be interested in a best practice to do this.

All the code:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        clientMachineName = (Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName).Split('.')[0];
    }
    catch (Exception)
    {
        clientMachineName = "n/a";
    }

    if (!String.IsNullOrEmpty(Request.QueryString["status"]) &amp;&amp; !String.IsNullOrEmpty(Request.QueryString["value"]))
    {
        String macAddress = Request.QueryString["status"];
        String statusCode = Request.QueryString["value"];

        if (clientMachineName.Equals(getHostName(macAddress), StringComparison.InvariantCultureIgnoreCase))
        {
            ComputerDbCommunicator communicator = new ComputerDbCommunicator();
            communicator.insertIntoComputerDb("UPDATE [Computer] SET [statuscode] = " + statusCode + " WHERE [macaddress] = '" + macAddress + "'");

            communicator.closeConnections();
            communicator = null;

            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Type", "text/plain");
            Response.Write(statusCode);
            Response.Flush();
            Response.End();
        }
        else
        {
            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Type", "text/plain");
            Response.Write("Operation not allowed");
            Response.Flush();
            Response.End();
        }
    } 
    else if (!String.IsNullOrEmpty(Request.QueryString["status"]))
    {
        String macAddress = Request.QueryString["status"];
        String statusCode = null;
        ComputerDbCommunicator communicator = new ComputerDbCommunicator();
        SqlDataReader dr = communicator.queryComputerDb("SELECT [statuscode] FROM [Computer] WHERE [macaddress] = '" + macAddress + "'");
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                statusCode = dr[0].ToString();
            }
            dr.Close();

            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Type", "text/plain");
            Response.Write(statusCode);
            Response.Flush();
            Response.End();

            communicator.closeConnections();
            communicator = null; 
        }
    }
}

protected String getHostName(String macAddress)
{
    String hostName = null;
    ComputerDbCommunicator communicator = new ComputerDbCommunicator();
    SqlDataReader dr = communicator.queryComputerDb("SELECT [hostname] FROM [Computer] WHERE [macaddress] = '" + macAddress + "'");
    while (dr.Read())
    {
        hostName = dr[0].ToString();
    }
    dr.Close();

    communicator.closeConnections();
    communicator = null;

    return hostName;
}
You've got some duplication in there;

Perhaps you can create a function with a string argument that will handle response writing?

protected void CreateResponse(string response){
    Response.Clear();
    Response.ClearHeaders();
    Response.AddHeader("Content-Type", "text/plain");
    Response.Write(response);
    Response.Flush();
    Response.End();
}


To me that seems far more concerning than the way in which you retrieve the hostname.