liaoxujun@qq.com
2024-03-28 621e7691fc0cdce02c708531a37abfa7413a123a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
 
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;
using SqlSugar.Extensions;
 
namespace CoreCms.Net.Configuration
{
    /// <summary>
    /// »ñÈ¡AppsettingsÅäÖÃÐÅÏ¢
    /// </summary>
    public class AppSettingsHelper
    {
        static IConfiguration Configuration { get; set; }
 
        public AppSettingsHelper(string contentPath)
        {
            string Path = "appsettings.json";
            Configuration = new ConfigurationBuilder().SetBasePath(contentPath).Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true }).Build();
        }
 
        /// <summary>
        /// ·â×°Òª²Ù×÷µÄ×Ö·û
        /// AppSettingsHelper.GetContent(new string[] { "JwtConfig", "SecretKey" });
        /// </summary>
        /// <param name="sections">½ÚµãÅäÖÃ</param>
        /// <returns></returns>
        public static string GetContent(params string[] sections)
        {
            try
            {
 
                if (sections.Any())
                {
                    return Configuration[string.Join(":", sections)];
                }
            }
            catch (Exception) { }
 
            return "";
        }
 
 
 
 
        /// <summary>
        /// »ñÈ¡µçÄÔ MAC£¨ÎïÀí£© µØÖ·
        /// </summary>
        /// <param name="needToken">ÊÇ·ñÖ»ÊÇΪÁËÌ×È¡keyÉú³ÉÒ»¸ö²»Í¬²¿Êð»·¾³²»Í¬µÄÐòÁд®</param>
        /// <returns></returns>
        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;
        }
 
        /// <summary>
        /// »ñÈ¡µçÄÔ¼ÆËã»úÃû
        /// </summary>
        /// <returns></returns>
        public static string GetHostName()
        {
            //±¾µØ¼ÆËã»úÍøÂçÁ¬½ÓÐÅÏ¢
            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
 
            //»ñÈ¡±¾»úµçÄÔÃû
            var hostName = computerProperties.HostName;
 
            return !string.IsNullOrEmpty(hostName) ? hostName : "CoreShop.Professional";
 
        }
 
 
 
 
        /// <summary>
        /// ×ªMD5
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        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();
        }
 
 
    }
}