Search code examples
c#functionthread-safetypublichttpcontext

Is this public static get IP function thread safe ? usage of HttpContext


IIS 7.5 ASP.net 4.0 C# 4.0

Here the function is it thread safe ? assume 1000 different calls made from different visitors at the same time

public static string ReturnIpAddress()
{
    string srUserIp = "";
    try
    {
        if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
        {
            srUserIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
        }
        if (string.IsNullOrEmpty(srUserIp))
        {
            srUserIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
        }
    }
    catch
    {

    }
    return srUserIp;
}

Solution

  • This is super safe. You not changing any data, it is static and it queries a request object, which is immutable.