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

  • dirkgently1

    dirkgently1

    13 NİSAN 2006
  • ETrade Supply

    ETrade Suppl

    23 Temmuz 2011
  • Tutorials Junction

    Tutorials Ju

    1 Ocak 2014