using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Web; namespace JsonRx.Helper { public static class HttpRequestMessageExtensions { private const string HttpContext = "MS_HttpContext"; private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty"; public static string GetClientIpAddress(this HttpRequestMessage request) { if (request == null) return string.Empty; if (request.Properties.ContainsKey(HttpContext)) { dynamic ctx = request.Properties[HttpContext]; if (ctx != null) { return ctx.Request.UserHostAddress; } } if (request.Properties.ContainsKey(RemoteEndpointMessage)) { dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage]; if (remoteEndpoint != null) { return remoteEndpoint.Address; } } return null; } private static readonly ILog Log = LogManager.GetLogger(typeof(HttpRequestMessageExtensions)); public static string GetJWTToken(this HttpRequestMessage request) { string jwt = string.Empty; try { var id = request.Headers.GetValues("Authorization").FirstOrDefault(); return id.StartsWith("Bearer") ? id.Substring(7) : id; } catch(Exception ex) { Log.Error("INVALID_JWT_TOKEN", ex); } return jwt; } } }