2

I have an asp.net page (c#) that I execute daily.

I would like to know if there is a way to allow it to be executed only when its requested by the same ip as the server. Meaning, if another ip runs the page, nothing will happen or it will be redirected.

5 Answers 5

7

You can use Request.IsLocal to check if the request is local.

Sign up to request clarification or add additional context in comments.

1 Comment

thkx a lot! didn't know about it!
2

You can query the IsLocal property of the HttpRequest

http://msdn.microsoft.com/en-us/library/system.web.httprequest.islocal.aspx

Comments

2

Following works also with lower framework than 2.0 (but HttpRequest.IsLocal is preferable if available).

C#

public static void redirectIfNotServer(string redirectUrl)
{
    // Look for a proxy address first
    var IP = HttpContext.Current.Request.ServerVariables("HTTP_X_FORWARDED_FOR");
    //Trim and lowercase IP if not null
    if ((IP != null)) {
        IP = IP.ToLower().Trim;
    }
    if (IP == null || (IP.Equals("unknown"))) {
        //If IP is null use different detection method, else pull the correct IP from list.
        IP = HttpContext.Current.Request.ServerVariables("REMOTE_ADDR").ToLower().Trim();
    }

    List<string> IPs = null;
    if (IP.IndexOf(",") > -1) {
        IPs = IP.Split(',').ToList();
    } else {
        IPs = new string[] { IP }.ToList();
    }

    var serverIP = HttpContext.Current.Request.ServerVariables("LOCAL_ADDR");
    var ipIsServerIp = (from ipAddress in IP swhere ipAddress == serverIP).Any();
    if (!ipIsServerIp) {
        HttpContext.Current.Response.Redirect(redirectUrl);
    }
}

VB.NET

Public Shared Sub redirectIfNotServer(ByVal redirectUrl As String)
    ' Look for a proxy address first
    Dim IP = HttpContext.Current.Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    'Trim and lowercase IP if not null
    If Not IP Is Nothing Then
        IP = IP.ToLower().Trim
    End If
    If IP Is Nothing OrElse (IP.Equals("unknown")) Then
        'If IP is null use different detection method, else pull the correct IP from list.
        IP = HttpContext.Current.Request.ServerVariables("REMOTE_ADDR").ToLower().Trim
    End If

    Dim IPs As List(Of String)
    If IP.IndexOf(",") > -1 Then
        IPs = IP.Split(","c).ToList
    Else
        IPs = New String() {IP}.ToList
    End If

    Dim serverIP = HttpContext.Current.Request.ServerVariables("LOCAL_ADDR")
    Dim ipIsServerIp = (From ipAddress In IPs Where ipAddress = serverIP).Any
    If Not ipIsServerIp Then
        HttpContext.Current.Response.Redirect(redirectUrl)
    End If
End Sub

Comments

1

This:

Request.ServerVariables["REMOTE_ADDR"] 

will get you the remote ip, then just compare it to your server ip or "localhost"

Comments

1

Use the UserHostAddress property on the HttpRequest object to get the users address.

Use the LOCAL_ADDR variable with the ServerVariables property to get the servers address from the request object.

Fernando has the better solution by checking the IsLocal property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.