Skip to main content

HttpRequestOptionsExtensions

Namespace: RA.Utilities.Integrations.Extensions

The HttpRequestOptionsExtensions class provides extension members on HttpRequestOptions (declared inside an extension(HttpRequestOptions) block) for reading context that the hosting environment attaches to outgoing requests.

GetClientIpAddress()​

Extracts the original client's IP address from the request context. Finding the client IP differs between hosting environments (e.g., IIS vs. self-hosted); this method encapsulates that logic into a single call, which is invaluable for logging, auditing, or rate-limiting inside a DelegatingHandler.

It checks the option keys used by both IIS-hosted (MS_HttpContext) and self-hosted (System.ServiceModel.Channels.RemoteEndpointMessageProperty) environments, and returns an empty string when neither is present.

Parameters​

ParameterTypeDescription
httpRequestOptionsHttpRequestOptionsThe options collection of an HttpRequestMessage.
ReturnsstringThe client's IP address, or an empty string if it cannot be found.

Example​

This method is typically used inside a DelegatingHandler, where the hosting environment populates the request options. The package's own LoggingHandler uses it to fill the RemoteAddress field of the log templates.

using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using RA.Utilities.Integrations.Extensions;

public class IpLoggingHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
// The hosting environment (like ASP.NET Core) populates the request options.
var clientIp = request.Options.GetClientIpAddress();

Console.WriteLine($"Request received from IP address: {clientIp}");

return await base.SendAsync(request, cancellationToken);
}
}
v10.2.0 rename

This extension was previously declared in the HttpRequestMessageExtensions class. In v10.2.0 the class was renamed to HttpRequestOptionsExtensions to match the actual receiver type (HttpRequestOptions), and the method became a C# 14 extension member. Call sites such as request.Options.GetClientIpAddress() are unchanged; the companion ToDictionary(...) helper was removed — see the migration guide.