|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
|
|
|
|
namespace NPin.Framework.Core.Extensions;
|
|
|
|
@ -7,93 +8,125 @@ namespace NPin.Framework.Core.Extensions;
|
|
|
|
|
public static class HttpContextExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置文件下载名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
/// <param name="fileName"></param>
|
|
|
|
|
public static void FileInlineHandle(this HttpContext httpContext, string fileName)
|
|
|
|
|
{
|
|
|
|
|
string encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
|
|
|
|
|
httpContext.Response.Headers.Add("Content-Disposition", "inline;filename=" + encodeFilename);
|
|
|
|
|
/// 设置文件下载名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
/// <param name="fileName"></param>
|
|
|
|
|
public static void FileInlineHandle(this HttpContext httpContext, string fileName)
|
|
|
|
|
{
|
|
|
|
|
string encodeFilename = HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
|
|
|
|
|
httpContext.Response.Headers.Add("Content-Disposition", "inline;filename=" + encodeFilename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置文件附件名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
/// <param name="fileName"></param>
|
|
|
|
|
public static void FileAttachmentHandle(this HttpContext httpContext, string fileName)
|
|
|
|
|
{
|
|
|
|
|
string encodeFilename = HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
|
|
|
|
|
httpContext.Response.Headers.Add("Content-Disposition", "attachment;filename=" + encodeFilename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置文件附件名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
/// <param name="fileName"></param>
|
|
|
|
|
public static void FileAttachmentHandle(this HttpContext httpContext, string fileName)
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取语言种类
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetLanguage(this HttpContext httpContext)
|
|
|
|
|
{
|
|
|
|
|
string res = "zh-CN";
|
|
|
|
|
var str = httpContext.Request.Headers["Accept-Language"].FirstOrDefault();
|
|
|
|
|
if (str is not null)
|
|
|
|
|
{
|
|
|
|
|
string encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
|
|
|
|
|
httpContext.Response.Headers.Add("Content-Disposition", "attachment;filename=" + encodeFilename);
|
|
|
|
|
|
|
|
|
|
res = str.Split(",")[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取语言种类
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetLanguage(this HttpContext httpContext)
|
|
|
|
|
{
|
|
|
|
|
string res = "zh-CN";
|
|
|
|
|
var str = httpContext.Request.Headers["Accept-Language"].FirstOrDefault();
|
|
|
|
|
if (str is not null)
|
|
|
|
|
{
|
|
|
|
|
res = str.Split(",")[0];
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断是否为异步请求
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsAjaxRequest(this HttpRequest request)
|
|
|
|
|
{
|
|
|
|
|
string header = request.Headers["X-Requested-With"];
|
|
|
|
|
return "XMLHttpRequest".Equals(header);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取客户端Ip
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetClientIp(this HttpContext? context)
|
|
|
|
|
{
|
|
|
|
|
if (context == null) return "";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断是否为异步请求
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsAjaxRequest(this HttpRequest request)
|
|
|
|
|
var result = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
|
|
|
|
|
if (string.IsNullOrEmpty(result))
|
|
|
|
|
{
|
|
|
|
|
string header = request.Headers["X-Requested-With"];
|
|
|
|
|
return "XMLHttpRequest".Equals(header);
|
|
|
|
|
result = context.Connection.RemoteIpAddress?.ToString();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取客户端IP
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetClientIp(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) || result.Contains("::1"))
|
|
|
|
|
result = "127.0.0.1";
|
|
|
|
|
|
|
|
|
|
result = result.Replace("::ffff:", "127.0.0.1");
|
|
|
|
|
// 解析 Ip String 到 Ipv4 或 MapToIPv4
|
|
|
|
|
// 若无法解析 IP String,强制转为本地 Ipv4
|
|
|
|
|
result = IPAddress.TryParse(result, out var ipAddr)
|
|
|
|
|
? ipAddr.MapToIPv4().ToString()
|
|
|
|
|
: IPAddress.Loopback.ToString();
|
|
|
|
|
|
|
|
|
|
//Ip规则效验
|
|
|
|
|
var regResult = Regex.IsMatch(result, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
|
|
|
|
|
// // 本地回环 转换为 ipv4 127.0.0.1
|
|
|
|
|
// if (string.IsNullOrEmpty(result) || result.Contains("::1"))
|
|
|
|
|
// {
|
|
|
|
|
// result = "127.0.0.1";
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// result = result.Replace("::ffff:", "127.0.0.1");
|
|
|
|
|
//
|
|
|
|
|
// // Ip规则效验
|
|
|
|
|
// var regResult = Regex.IsMatch(result, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
|
|
|
|
|
//
|
|
|
|
|
// result = regResult ? result : "127.0.0.1";
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = regResult ? result : "127.0.0.1";
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取客户端Ip
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IPAddress? GetClientIpAddress(this HttpContext? context)
|
|
|
|
|
{
|
|
|
|
|
if (context == null) return null;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取浏览器标识
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetUserAgent(this HttpContext context)
|
|
|
|
|
var result = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
|
|
|
|
|
if (string.IsNullOrEmpty(result))
|
|
|
|
|
{
|
|
|
|
|
return context.Request.Headers["User-Agent"];
|
|
|
|
|
result = context.Connection.RemoteIpAddress?.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string[]? GetUserPermissions(this HttpContext context, string permissionsName)
|
|
|
|
|
{
|
|
|
|
|
return context.User.Claims.Where(x => x.Type == permissionsName).Select(x => x.Value).ToArray();
|
|
|
|
|
}
|
|
|
|
|
// 解析 Ip String 到 IpAddress
|
|
|
|
|
// 若无法解析 IP String,强制转为本地 Ipv4
|
|
|
|
|
return IPAddress.TryParse(result, out var ipAddr)
|
|
|
|
|
? ipAddr
|
|
|
|
|
: IPAddress.Loopback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取浏览器标识
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetUserAgent(this HttpContext context)
|
|
|
|
|
{
|
|
|
|
|
return context.Request.Headers["User-Agent"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string[] GetUserPermissions(this HttpContext context, string permissionsName)
|
|
|
|
|
{
|
|
|
|
|
return context.User.Claims.Where(x => x.Type == permissionsName).Select(x => x.Value).ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|