SORU
5 Mart 2012, PAZARTESİ


Uzak ana bilgisayarın IP adresini almak

ASP.NET bize REMOTE_ADDR özellik değeri IP adresi sağlayan ServerVariables özellik içeren System.Web.HttpRequest bir sınıf yoktur.

Ancak, ASP.NET Web API, uzak ana bilgisayarın IP adresini almak için benzer bir yol bulamadım.

Nasıl istekte uzak ana bilgisayarın IP adresini alabilir miyim?

CEVAP
5 Mart 2012, PAZARTESİ


Bu mümkün, ama çok keşfedilebilir - tesiste, marketten gelen istek ve özelliği gereken erişim bağlı olup olmadığını kullanıyorsun Web API altında IIS (webhosted) ya da kendi kendine ev sahipliği yaptı. Aşağıdaki kod, bunun nasıl yapılacağını gösterir.

private string GetClientIp(HttpRequestMessage request)
{
    if (request.Properties.ContainsKey("MS_HttpContext"))
    {
        return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
    }

    if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
    {
        RemoteEndpointMessageProperty prop;
        prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
        return prop.Address;
    }

    return null;
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • RiceBunny

    RiceBunny

    16 ŞUBAT 2006
  • sinumatic

    sinumatic

    19 Aralık 2006
  • Crossover

    Crossover

    18 HAZİRAN 2007