初始化项目

This commit is contained in:
2025-11-26 17:42:45 +08:00
commit 65ecc68767
2513 changed files with 1313954 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
namespace Services.Tool
{
public class IPHelper
{
public static string GetIP()
{
if (HttpContext.Current == null)
{
throw new Exception("HttpContext.Current为null");
}
string ip = string.Empty;
if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
ip = Convert.ToString(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
if (string.IsNullOrEmpty(ip))
ip = Convert.ToString(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
return ip;
}
/// <summary>获取客户端的IP可以取到代理后的IP
///
/// </summary>
/// <returns></returns>
public static string GetClientIp()
{
string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(result))
{
//可能有代理
if (result.IndexOf(".", StringComparison.Ordinal) == -1)
result = null;
else
{
if (result.IndexOf(",", StringComparison.Ordinal) != -1)
{
//有“,”估计多个代理。取第一个不是内网的IP。
result = result.Replace(" ", "").Replace("'", "");
string[] temparyip = result.Split(",;".ToCharArray());
foreach (string t in temparyip)
{
if (IsIpAddress(t)
&& t.Substring(0, 3) != "10."
&& t.Substring(0, 7) != "192.168"
&& t.Substring(0, 7) != "172.16.")
{
return t; //找到不是内网的地址
}
}
}
else if (IsIpAddress(result)) //代理即是IP格式
return result;
else
result = null; //代理中的内容 非IP取IP
}
}
if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.UserHostAddress;
return result;
}
/// <summary>判断是否是IP地址格式 0.0.0.0
///
/// </summary>
/// <param name="str">待判断的IP地址</param>
/// <returns>true or false</returns>
public static bool IsIpAddress(string str)
{
if (string.IsNullOrEmpty(str) || str.Length < 7 || str.Length > 15) return false;
const string regformat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}$";
Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
return regex.IsMatch(str);
}
}
}

View File

@@ -0,0 +1,76 @@
using JWT;
using JWT.Algorithms;
using JWT.Serializers;
using Newtonsoft.Json;
using Services.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Services.Tool
{
/// <summary>
/// Token辅助类
/// 需要引用jwt包
/// </summary>
public class TokenHelper
{
//私钥
static string Private_key = "CWLCJLWQZYY";
//创建一个世界协调时间提供对象 utc时间协调时间
static IDateTimeProvider provider = new UtcDateTimeProvider();
//时间戳起点时间
static DateTime timeEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
//序列化对象
static JsonNetSerializer serializer = new JsonNetSerializer();
//编码序列化对象
static IBase64UrlEncoder encoder = new JwtBase64UrlEncoder();
//编码算法
static IJwtAlgorithm algorithm = new HMACSHA384Algorithm();
//jwt编码
static JwtEncoder jwtEncoder = new JwtEncoder(algorithm, serializer, encoder);
//验证对象
static IJwtValidator validator = new JwtValidator(serializer, provider);
//jwt解码
static JwtDecoder decoder = new JwtDecoder(serializer, validator, encoder, algorithm);
/// <summary>
/// 加密的数据键值对传入
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
public static string GetToken(Dictionary<string, object> keys, float day)
{
//荷载,负载,最重要的信息,token中保存的信息 keys
var now = provider.GetNow();
//获取当前时间距离时间戳的差秒
var epochSecons = Math.Round((now - timeEpoch).TotalSeconds);
keys.Add("exp", (epochSecons + 3600) * 24 * day);//设置过期时间
//jwt同一加密
var token = jwtEncoder.Encode(keys, Private_key);
return token;
}
public static string CheckToken(string token)
{
try
{
//解码 tonken 私钥 验证是否过期
//过期时间验证 系统默认起始时间为1970,1,1,0,0,0 如果验证过期需要保证起始时间一致
//要么手动验证过期,现在时间与起始时间的差值如果大于设置exp则过期
return JsonConvert.DeserializeObject<Datainfo>(decoder.Decode(token, Private_key, verify: true)).data.ToString();//str数据转成Dictionary<string,object>类型即可
}
catch (Exception ex)
{
Logs.WriteLog("Error" + ex);
return null;
}
}
public class Datainfo
{
public dynamic data { get; set; }
public dynamic exp { get; set; }
}
}
}