You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.7 KiB
61 lines
1.7 KiB
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;
|
|
}
|
|
}
|
|
}
|