53 строки
1.7 KiB
C#
53 строки
1.7 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Web;
|
|||
|
using System.Net.Http;
|
|||
|
|
|||
|
namespace AppvNext.Throttlebird.Extensions
|
|||
|
{
|
|||
|
public static class HttpRequestMessageExtensions
|
|||
|
{
|
|||
|
private const string HttpContext = "MS_HttpContext";
|
|||
|
private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty";
|
|||
|
private const string OwinContext = "MS_OwinContext";
|
|||
|
|
|||
|
public static bool IsLocal(this HttpRequestMessage request)
|
|||
|
{
|
|||
|
var localFlag = request.Properties["MS_IsLocal"] as Lazy<bool>;
|
|||
|
return localFlag != null && localFlag.Value;
|
|||
|
}
|
|||
|
|
|||
|
public static string GetClientIpAddress(this HttpRequestMessage request)
|
|||
|
{
|
|||
|
//Web-hosting
|
|||
|
if (request.Properties.ContainsKey(HttpContext))
|
|||
|
{
|
|||
|
dynamic ctx = request.Properties[HttpContext];
|
|||
|
if (ctx != null)
|
|||
|
{
|
|||
|
return ctx.Request.UserHostAddress;
|
|||
|
}
|
|||
|
}
|
|||
|
//Self-hosting
|
|||
|
if (request.Properties.ContainsKey(RemoteEndpointMessage))
|
|||
|
{
|
|||
|
dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
|
|||
|
if (remoteEndpoint != null)
|
|||
|
{
|
|||
|
return remoteEndpoint.Address;
|
|||
|
}
|
|||
|
}
|
|||
|
//Owin-hosting
|
|||
|
if (request.Properties.ContainsKey(OwinContext))
|
|||
|
{
|
|||
|
dynamic ctx = request.Properties[OwinContext];
|
|||
|
if (ctx != null)
|
|||
|
{
|
|||
|
return ctx.Request.RemoteIpAddress;
|
|||
|
}
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|