74 lines
2.6 KiB
C#
74 lines
2.6 KiB
C#
using Kros.Extensions;
|
|
using Microsoft.Extensions.Primitives;
|
|
|
|
namespace ApiGateway
|
|
{
|
|
public class Loggin
|
|
{
|
|
//public string GetRequestIP(HttpContext ctx, bool tryUseXForwardHeader = true)
|
|
//{
|
|
// string ip = null;
|
|
|
|
// // todo support new "Forwarded" header (2014) https://en.wikipedia.org/wiki/X-Forwarded-For
|
|
|
|
// // X-Forwarded-For (csv list): Using the First entry in the list seems to work
|
|
// // for 99% of cases however it has been suggested that a better (although tedious)
|
|
// // approach might be to read each IP from right to left and use the first public IP.
|
|
// // http://stackoverflow.com/a/43554000/538763
|
|
// //
|
|
// if (tryUseXForwardHeader)
|
|
// {
|
|
// ip = GetHeaderValueAs<string>(ctx, "X-Forwarded-For");
|
|
// ip = SplitCsv(ip).FirstOrDefault();
|
|
|
|
// }
|
|
|
|
// // RemoteIpAddress is always null in DNX RC1 Update1 (bug).
|
|
// if (IsNullOrWhitespace(ip) && ctx?.Connection?.RemoteIpAddress != null)
|
|
// ip = ctx.Connection.RemoteIpAddress.ToString();
|
|
|
|
// if (IsNullOrWhitespace(ip))
|
|
// ip = GetHeaderValueAs<string>(ctx, "REMOTE_ADDR");
|
|
|
|
// // _httpContextAccessor.HttpContext?.Request?.Host this is the local host.
|
|
|
|
// if (IsNullOrWhitespace(ip))
|
|
// throw new Exception("Unable to determine caller's IP.");
|
|
|
|
// return ip;
|
|
//}
|
|
|
|
//public T GetHeaderValueAs<T>(HttpContext ctx, string headerName)
|
|
//{
|
|
// StringValues values;
|
|
|
|
// if (ctx?.Request?.Headers?.TryGetValue(headerName, out values) ?? false)
|
|
// {
|
|
// string rawValues = values.ToString(); // writes out as Csv when there are multiple.
|
|
|
|
// if (!IsNullOrWhitespace(rawValues))
|
|
// return (T)Convert.ChangeType(values.ToString(), typeof(T));
|
|
// }
|
|
// return default(T);
|
|
//}
|
|
|
|
//public List<string> SplitCsv(string csvList, bool nullOrWhitespaceInputReturnsNull = false)
|
|
//{
|
|
// if (string.IsNullOrWhiteSpace(csvList))
|
|
// return nullOrWhitespaceInputReturnsNull ? null : new List<string>();
|
|
|
|
// return csvList
|
|
// .TrimEnd(',')
|
|
// .Split(',')
|
|
// .AsEnumerable<string>()
|
|
// .Select(s => s.Trim())
|
|
// .ToList();
|
|
//}
|
|
|
|
//public bool IsNullOrWhitespace(string s)
|
|
//{
|
|
// return String.IsNullOrWhiteSpace(s);
|
|
//}
|
|
}
|
|
}
|