2021-10-12 16:52:28 +08:00
|
|
|
|
using Yi.Framework.Model;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Yi.Framework.Model.Models;
|
2022-04-30 21:48:18 +08:00
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2022-07-13 10:32:43 +08:00
|
|
|
|
using System.IO;
|
2022-10-01 23:53:43 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
2022-10-02 14:02:21 +08:00
|
|
|
|
using UAParser;
|
2022-10-03 17:04:59 +08:00
|
|
|
|
using IPTools.Core;
|
2021-10-10 17:30:31 +08:00
|
|
|
|
|
2021-10-12 16:52:28 +08:00
|
|
|
|
namespace Yi.Framework.WebCore
|
|
|
|
|
|
{
|
2022-10-01 23:53:43 +08:00
|
|
|
|
public static class HttpContextExtend
|
2021-10-12 16:52:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <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);
|
|
|
|
|
|
}
|
2021-10-10 17:30:31 +08:00
|
|
|
|
|
2022-09-18 17:22:47 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 通过鉴权完的token获取用户id
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-09-15 18:44:12 +08:00
|
|
|
|
public static long GetUserIdInfo(this HttpContext httpContext)
|
|
|
|
|
|
{
|
2022-09-15 18:59:01 +08:00
|
|
|
|
var p = httpContext;
|
2022-10-11 16:48:25 +08:00
|
|
|
|
var value = httpContext.User.Claims.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid)?.Value;
|
|
|
|
|
|
if (value is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Convert.ToInt64(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
2022-09-18 17:22:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 通过鉴权完的token获取用户名
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-10-17 18:08:16 +08:00
|
|
|
|
public static string? GetUserNameInfo(this HttpContext httpContext)
|
2022-09-18 17:22:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
var p = httpContext;
|
2022-10-01 23:53:43 +08:00
|
|
|
|
return httpContext.User.Claims.FirstOrDefault(u => u.Type == "userName")?.Value;
|
2022-09-18 17:22:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 通过鉴权完的token获取用户部门
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-10-17 18:08:16 +08:00
|
|
|
|
public static string? GetDeptIdInfo(this HttpContext httpContext)
|
2022-09-18 17:22:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
var p = httpContext;
|
2022-10-17 18:08:16 +08:00
|
|
|
|
return httpContext.User.Claims.FirstOrDefault(u => u.Type == "deptId")?.Value;
|
2022-09-15 18:44:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-18 17:22:47 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 通过鉴权完的token获取权限code
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-10-17 18:08:16 +08:00
|
|
|
|
public static string? GetPermissionInfo(this HttpContext httpContext)
|
2022-09-18 17:22:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
var p = httpContext;
|
2022-10-17 18:08:16 +08:00
|
|
|
|
return httpContext.User.Claims.FirstOrDefault(u => u.Type == "permission")?.Value;
|
2022-09-18 17:22:47 +08:00
|
|
|
|
}
|
2022-10-01 23:53:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
2021-10-12 16:52:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 基于HttpContext,当前鉴权方式解析,获取用户信息
|
2022-01-11 16:40:15 +08:00
|
|
|
|
/// 现在使用redis作为缓存,不需要将菜单存放至jwt中了
|
2021-10-12 16:52:28 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-09-15 18:44:12 +08:00
|
|
|
|
public static UserEntity GetUserEntityInfo(this HttpContext httpContext, out List<Guid> menuIds)
|
2021-10-12 16:52:28 +08:00
|
|
|
|
{
|
2022-10-17 18:08:16 +08:00
|
|
|
|
IEnumerable<Claim>? claimlist = null;
|
2022-04-30 21:48:18 +08:00
|
|
|
|
long resId = 0;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-09-15 18:40:24 +08:00
|
|
|
|
claimlist = httpContext.User.Claims;
|
2022-10-03 17:04:59 +08:00
|
|
|
|
resId = Convert.ToInt64(claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid)?.Value);
|
2022-04-30 21:48:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception("未授权,Token鉴权失败!");
|
|
|
|
|
|
}
|
2022-04-03 23:21:53 +08:00
|
|
|
|
menuIds = claimlist.Where(u => u.Type == "menuIds").ToList().Select(u => new Guid(u.Value)).ToList();
|
|
|
|
|
|
return new UserEntity()
|
2021-10-24 17:13:28 +08:00
|
|
|
|
{
|
2022-04-02 17:44:50 +08:00
|
|
|
|
Id = resId,
|
2022-04-30 21:48:18 +08:00
|
|
|
|
//Name = claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Name).Value
|
2021-10-12 16:52:28 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
2022-07-13 10:32:43 +08:00
|
|
|
|
|
2022-10-01 23:53:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置文件下载名称
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
|
/// <param name="fileName"></param>
|
2022-07-13 10:32:43 +08:00
|
|
|
|
public static void FileInlineHandle(this HttpContext httpContext, string fileName)
|
|
|
|
|
|
{
|
|
|
|
|
|
string encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("UTF-8"));
|
|
|
|
|
|
httpContext.Response.Headers.Add("Content-Disposition", "inline;filename=" + encodeFilename);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2022-10-01 23:53:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置文件附件名称
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
|
/// <param name="fileName"></param>
|
2022-07-13 10:32:43 +08:00
|
|
|
|
public static void FileAttachmentHandle(this HttpContext httpContext, string fileName)
|
|
|
|
|
|
{
|
|
|
|
|
|
string encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("UTF-8"));
|
|
|
|
|
|
httpContext.Response.Headers.Add("Content-Disposition", "attachment;filename=" + encodeFilename);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2022-10-01 23:53:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取语言种类
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-07-13 10:32:43 +08:00
|
|
|
|
public static string GetLanguage(this HttpContext httpContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
string res = "zh-CN";
|
|
|
|
|
|
var str = httpContext.Request.Headers["Accept-Language"].FirstOrDefault();
|
2022-10-17 18:08:16 +08:00
|
|
|
|
if (str is not null)
|
2022-07-13 10:32:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
res = str.Split(",")[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-29 18:01:19 +08:00
|
|
|
|
|
2022-10-01 23:53:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取请求Body参数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <param name="reqMethod"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-09-29 18:01:19 +08:00
|
|
|
|
public static string GetRequestValue(this HttpContext context, string reqMethod)
|
|
|
|
|
|
{
|
|
|
|
|
|
string param;
|
|
|
|
|
|
|
|
|
|
|
|
if (HttpMethods.IsPost(reqMethod) || HttpMethods.IsPut(reqMethod))
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Request.Body.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
|
|
|
|
|
|
//需要使用异步方式才能获取
|
|
|
|
|
|
param = reader.ReadToEndAsync().Result;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2022-10-17 18:08:16 +08:00
|
|
|
|
param = context.Request.QueryString.Value is null?"": context.Request.QueryString.Value.ToString();
|
2022-09-29 18:01:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
return param;
|
|
|
|
|
|
}
|
2022-10-01 23:53:43 +08:00
|
|
|
|
|
2022-10-02 14:02:21 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取客户端信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static ClientInfo GetClientInfo(this HttpContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var str = GetUserAgent(context);
|
|
|
|
|
|
var uaParser = Parser.GetDefault();
|
|
|
|
|
|
ClientInfo c = uaParser.Parse(str);
|
|
|
|
|
|
return c;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-01 23:53:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <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规则效验
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-02 14:02:21 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取浏览器标识
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static string GetUserAgent(this HttpContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
return context.Request.Headers["User-Agent"];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 记录用户登陆信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static LoginLogEntity GetLoginLogInfo(this HttpContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var ipAddr = context.GetClientIp();
|
2022-10-29 10:33:08 +08:00
|
|
|
|
IpInfo location;
|
|
|
|
|
|
if (ipAddr == "127.0.0.1")
|
|
|
|
|
|
{
|
|
|
|
|
|
location = new IpInfo() { Province = "本地", City = "本机" };
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
location = IpTool.Search(ipAddr);
|
|
|
|
|
|
}
|
2022-10-02 14:02:21 +08:00
|
|
|
|
ClientInfo clientInfo = context.GetClientInfo();
|
|
|
|
|
|
LoginLogEntity entity = new()
|
|
|
|
|
|
{
|
|
|
|
|
|
Browser = clientInfo.Device.Family,
|
|
|
|
|
|
Os = clientInfo.OS.ToString(),
|
|
|
|
|
|
LoginIp = ipAddr,
|
|
|
|
|
|
//登录是没有token的,所有是获取不到用户名,需要在控制器赋值
|
|
|
|
|
|
//LoginUser = context.GetUserNameInfo(),
|
2022-10-03 17:04:59 +08:00
|
|
|
|
LoginLocation = location.Province + "-" + location.City,
|
2022-10-02 14:02:21 +08:00
|
|
|
|
IsDeleted = false
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-12 16:52:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|