From ef6e5a9057eb386eb85f2459b25f3e363a473156 Mon Sep 17 00:00:00 2001 From: jianweie code Date: Mon, 31 Jul 2023 02:14:55 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91JWT?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E6=A8=A1=E5=BC=8F=E8=BF=9B=E8=A1=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8CSecretKey=E5=80=BC=E5=A2=9E=E5=8A=A0=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=E7=A0=81=E3=80=81=E8=AE=A1=E7=AE=97=E6=9C=BA=E5=90=8D?= =?UTF-8?q?=E3=80=81=E7=BD=91=E5=9D=80=E7=BB=84=E5=90=88md5=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=EF=BC=8C=E9=98=B2=E6=AD=A2=E5=87=BA=E7=8E=B0=E5=A4=A7?= =?UTF-8?q?=E5=AE=B6=E4=B8=8D=E4=BF=AE=E6=94=B9=E9=BB=98=E8=AE=A4=E5=80=BC?= =?UTF-8?q?=E5=AF=BC=E8=87=B4JwtToken=E6=9D=83=E9=99=90=E8=BF=87=E5=AE=BD?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9BIssuer=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E4=BD=BF=E7=94=A8=E8=AE=A1=E7=AE=97=E6=9C=BA?= =?UTF-8?q?=E5=90=8D=E6=9B=BF=E6=8D=A2=EF=BC=8Cappsetting.json=E5=8E=BB?= =?UTF-8?q?=E9=99=A4JwtConfig=E7=9A=84=E9=BB=98=E8=AE=A4=E5=80=BC=EF=BC=8C?= =?UTF-8?q?=E8=A6=81=E6=B1=82=E8=BE=93=E5=85=A5=E8=AE=BE=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AppSettingsConstVars.cs | 5 +- .../AppSettingsHelper.cs | 99 +++++++++++++++++++ CoreCms.Net.Web.Admin/appsettings.json | 6 +- CoreCms.Net.Web.WebApi/appsettings.json | 6 +- 4 files changed, 108 insertions(+), 8 deletions(-) diff --git a/CoreCms.Net.Configuration/AppSettingsConstVars.cs b/CoreCms.Net.Configuration/AppSettingsConstVars.cs index dc79b8b8..d78feba7 100644 --- a/CoreCms.Net.Configuration/AppSettingsConstVars.cs +++ b/CoreCms.Net.Configuration/AppSettingsConstVars.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using SqlSugar.Extensions; @@ -71,8 +72,8 @@ namespace CoreCms.Net.Configuration #region Jwt授权配置================================================================================ - public static readonly string JwtConfigSecretKey = AppSettingsHelper.GetContent("JwtConfig", "SecretKey"); - public static readonly string JwtConfigIssuer = AppSettingsHelper.GetContent("JwtConfig", "Issuer"); + public static readonly string JwtConfigSecretKey = AppSettingsHelper.GetContent("JwtConfig", "SecretKey") + AppSettingsHelper.GetMachineRandomKey(DbSqlConnection + AppSettingsHelper.GetMACIp(true)); + public static readonly string JwtConfigIssuer = !string.IsNullOrEmpty(AppSettingsHelper.GetContent("JwtConfig", "Issuer")) ? AppSettingsHelper.GetContent("JwtConfig", "Issuer") : AppSettingsHelper.GetHostName(); public static readonly string JwtConfigAudience = AppSettingsHelper.GetContent("JwtConfig", "Audience"); #endregion diff --git a/CoreCms.Net.Configuration/AppSettingsHelper.cs b/CoreCms.Net.Configuration/AppSettingsHelper.cs index 4545ed73..4916d5cd 100644 --- a/CoreCms.Net.Configuration/AppSettingsHelper.cs +++ b/CoreCms.Net.Configuration/AppSettingsHelper.cs @@ -1,6 +1,10 @@ using System; using System.Linq; +using System.Net.NetworkInformation; +using System.Reflection.PortableExecutable; +using System.Security.Cryptography; +using System.Text; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json; using Newtonsoft.Json.Linq; @@ -42,5 +46,100 @@ namespace CoreCms.Net.Configuration return ""; } + + + + /// + /// ȡ MAC ַ + /// + /// ǷֻΪȡkeyһͬ𻷾ͬд + /// + public static string GetMACIp(bool needToken) + { + //ؼϢ + IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties(); + //ȡ + NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); + + //ȡ + var HostName = computerProperties.HostName; + //ȡ + var DomainName = computerProperties.DomainName; + + if (nics == null || nics.Length < 1) + { + return ""; + } + + var MACIp = needToken ? HostName + DomainName : ""; + foreach (NetworkInterface adapter in nics) + { + var adapterName = adapter.Name; + + var adapterDescription = adapter.Description; + var NetworkInterfaceType = adapter.NetworkInterfaceType; + if (adapterName == "" || needToken) + { + PhysicalAddress address = adapter.GetPhysicalAddress(); + byte[] bytes = address.GetAddressBytes(); + + for (int i = 0; i < bytes.Length; i++) + { + MACIp += bytes[i].ToString("X2"); + + if (i != bytes.Length - 1) + { + MACIp += "-"; + } + } + } + } + + return MACIp; + } + + /// + /// ȡԼ + /// + /// + public static string GetHostName() + { + //ؼϢ + IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties(); + + //ȡ + var hostName = computerProperties.HostName; + + return !string.IsNullOrEmpty(hostName) ? hostName : "CoreShop.Professional"; + + } + + + + + /// + /// תMD5 + /// + /// + /// + public static string GetMachineRandomKey(string str) + { + MD5 md5 = MD5.Create(); + // ַתֽ + byte[] byteOld = Encoding.UTF8.GetBytes(str); + // üܷ + byte[] byteNew = md5.ComputeHash(byteOld); + // ܽתΪַ + StringBuilder sb = new StringBuilder(); + foreach (byte b in byteNew) + { + // ֽת16Ʊʾַ + sb.Append(b.ToString("x2")); + } + // ؼַܵ + return sb.ToString(); + } + + } } \ No newline at end of file diff --git a/CoreCms.Net.Web.Admin/appsettings.json b/CoreCms.Net.Web.Admin/appsettings.json index decb7232..7748f2de 100644 --- a/CoreCms.Net.Web.Admin/appsettings.json +++ b/CoreCms.Net.Web.Admin/appsettings.json @@ -29,9 +29,9 @@ }, //jwt授权认证的一些设置 "JwtConfig": { - "SecretKey": "8kh2luzmp0oq9wfbdeasygj647vr531n", - "Issuer": "CoreShopProfessional", - "Audience": "CoreCms" + "SecretKey": "", //请自主填写一段英文数字等作为token令牌,16位+ + "Issuer": "", //颁发者身份标识,如CoreShop.Professional + "Audience": "coreshop" //期望的接收人,如core,cms等 }, //跨域设置 "Cors": { diff --git a/CoreCms.Net.Web.WebApi/appsettings.json b/CoreCms.Net.Web.WebApi/appsettings.json index 034a611b..067f5716 100644 --- a/CoreCms.Net.Web.WebApi/appsettings.json +++ b/CoreCms.Net.Web.WebApi/appsettings.json @@ -29,9 +29,9 @@ }, //jwt授权认证的一些设置 "JwtConfig": { - "SecretKey": "8kh2luzmp0oq9wfbdeasygj647vr531n", - "Issuer": "CoreShopProfessional", - "Audience": "CoreCms" + "SecretKey": "", //请自主填写一段英文数字等作为token令牌,16位+ + "Issuer": "", //颁发者身份标识,如CoreShop.Professional + "Audience": "coreshop" //期望的接收人,如core,cms等 }, //跨域设置 "Cors": {