Skip to content

Question: how can I determine that the client sent a request to the web server from the same computer where the web server is running? #117

Answered by jchristn
user4000 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi Timur! Good to hear from you. I would use HttpContext.Request.Source which includes an IpAddress property. You can use a method like the below to indicate if the request came from localhost.

bool IsLocalRequest(HttpContext ctx)
{
    var host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            if (ip.ToString().Equals(ctx.Request.Source.IpAddress) return true;
        }
    }
    return false;
}

Obviously this could be optimized in a number of ways: 1) pre-caching the result of Dns.GetHostEntry(Dns.GetHostName()) into a variable and re-using it and 2) using LINQ to test if

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@user4000
Comment options

@user4000
Comment options

Answer selected by user4000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #116 on July 07, 2023 15:11.