10
私はAzure関数を使ってC#で関数を書いていますが、関数を呼び出したクライアントのIPアドレスを取得する必要がありますか?AzureでクライアントのIPアドレスを取得する方法C#?
私はAzure関数を使ってC#で関数を書いていますが、関数を呼び出したクライアントのIPアドレスを取得する必要がありますか?AzureでクライアントのIPアドレスを取得する方法C#?
これは、hereに基づく回答です。
#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}");
}
あなたはこれらの機能を使用する必要がありますGet the IP address of the remote host
ローカル request.Properties [RemoteEndpointMessageProperty.Name]は紺碧 では使用できませんプリコンパイルされた関数をデバッグする場合は、[ "MS_HttpContext"]は使用できませんrequest.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;
}