123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using System.Security.Claims;
- using System.Text;
- using System.Text.RegularExpressions;
- using Base;
- using Extensions;
- using IPTools.Core;
- using UAParser;
- namespace Extensions
- {
-
-
-
- public static partial class HttpContextExtension
- {
-
-
-
-
-
- public static bool IsAjaxRequest(this HttpRequest request)
- {
- if (request == null)
- {
- throw new ArgumentNullException(nameof(request));
- }
-
-
- return request.Headers["X-Requested-With"] == "XMLHttpRequest" || request.Headers != null && request.Headers["X-Requested-With"] == "XMLHttpRequest";
- }
-
-
-
-
-
- public static string GetClientUserIp(this HttpContext context)
- {
- if (context == null) return "";
- var result = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
- if (string.IsNullOrEmpty(result))
- {
- result = context.Connection.RemoteIpAddress?.ToString();
- }
- if (string.IsNullOrEmpty(result))
- throw new Exception("获取IP失败");
- if (result.Contains("::1"))
- result = "127.0.0.1";
- result = result.Replace("::ffff:", "");
- result = result.Split(':')?.FirstOrDefault() ?? "127.0.0.1";
- result = IsIP(result) ? result : "127.0.0.1";
- return result;
- }
-
-
-
-
-
- public static bool IsIP(string ip)
- {
- return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
- }
-
-
-
-
-
- public static long GetUId(this HttpContext context)
- {
- var uid = context.User.FindFirstValue(ClaimTypes.PrimarySid);
- return !string.IsNullOrEmpty(uid) ? long.Parse(uid) : 0;
- }
-
-
-
-
-
- public static long GetDeptId(this HttpContext context)
- {
- var deptId = context.User.FindFirstValue(ClaimTypes.GroupSid);
- return !string.IsNullOrEmpty(deptId) ? long.Parse(deptId) : 0;
- }
-
-
-
-
-
- public static string GetName(this HttpContext context)
- {
- var uid = context.User?.Identity?.Name;
- return uid;
- }
-
-
-
-
-
- public static bool IsAdmin(this HttpContext context)
- {
- var userName = context.GetName();
- return userName == GlobalConstant.AdminRole;
- }
-
-
-
-
-
- public static IEnumerable<ClaimsIdentity> GetClaims(this HttpContext context)
- {
- return context.User?.Identities;
- }
-
-
-
-
-
- public static string GetUserAgent(this HttpContext context)
- {
- return context.Request.Headers["User-Agent"];
- }
-
-
-
-
-
- public static string GetToken(this HttpContext context)
- {
- return context.Request.Headers["Authorization"];
- }
-
-
-
-
-
- public static string GetRequestUrl(this HttpContext context)
- {
- return context != null ? context.Request.Path.Value : "";
- }
-
-
-
-
-
- public static string GetQueryString(this HttpContext context)
- {
- return context != null ? context.Request.QueryString.Value : "";
- }
-
-
-
-
-
- public static string GetBody(this HttpContext context)
- {
- context.Request.EnableBuffering();
-
-
-
-
- string body = string.Empty;
- var buffer = new MemoryStream();
- context.Request.Body.Seek(0, SeekOrigin.Begin);
- context.Request.Body.CopyToAsync(buffer);
- buffer.Position = 0;
- try
- {
- using StreamReader streamReader = new(buffer, Encoding.UTF8);
- body = streamReader.ReadToEndAsync().Result;
- }
- finally
- {
- buffer?.Dispose();
- }
- return body;
- }
-
-
-
-
-
- public static ClientInfo GetClientInfo(this HttpContext context)
- {
- var str = context.GetUserAgent();
- var uaParser = Parser.GetDefault();
- ClientInfo c = uaParser.Parse(str);
- return c;
- }
-
-
-
-
- public static string GetIpInfo(string IP)
- {
- var ipInfo = IpTool.Search(IP);
- return ipInfo?.Province + "-" + ipInfo?.City + "-" + ipInfo?.NetworkOperator;
- }
-
-
-
-
-
- public static string GetRequestValue(this HttpContext context, string reqMethod)
- {
- string param = string.Empty;
- if (HttpMethods.IsPost(reqMethod) || HttpMethods.IsPut(reqMethod) || HttpMethods.IsDelete(reqMethod))
- {
- param = context.GetBody();
- string regex = "(?<=\"password\":\")[^\",]*";
- param = Regex.Replace(param, regex, "***");
- }
- if (param.IsEmpty())
- {
- param = context.GetQueryString();
- }
- return param;
- }
- }
- }
|