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

  • NYCarspotter

    NYCarspotter

    26 EYLÜL 2011
  • Ownage Pranks

    Ownage Prank

    13 AĞUSTOS 2007
  • Titan Lee Hai

    Titan Lee Ha

    14 Temmuz 2008