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

  • Andrew_Huang

    Andrew_Huang

    10 ŞUBAT 2006
  • LivestrongWoman

    LivestrongWo

    1 Aralık 2011
  • Shylo Sharity

    Shylo Sharit

    27 EKİM 2011