Azure İşlevlerini kullanarak C# işlevinde bir işlev yazıyorum ve işlevin çağrıldığı istemcinin ip adresini almalıyım, bu mümkün mü?Azure Functions C# içinde istemci IP adresi nasıl alınır?
10
A
cevap
14
İşte bir cevap here dayanmaktadır.
#r "System.Web"
using System.Net;
using System.Web;
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)
{
string clientIP = ((HttpContextWrapper)req.Properties["MS_HttpContext"]).Request.UserHostAddress;
return req.CreateResponse(HttpStatusCode.OK, $"The client IP is {clientIP}");
}
5
bu işlevi kullanmalısınız Get the IP address of the remote host
yerel request.Properties [RemoteEndpointMessageProperty.Name] masmavi kullanılamaz önceden derlenmiş işlevleri hata ayıklama eğer [ "MS_HttpContext"] kullanılamazrequest.Properties
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;
}