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
{
///
/// »ñÈ¡AppsettingsÅäÖÃÐÅÏ¢
///
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();
}
///
/// ·â×°Òª²Ù×÷µÄ×Ö·û
/// AppSettingsHelper.GetContent(new string[] { "JwtConfig", "SecretKey" });
///
/// ½ÚµãÅäÖÃ
///
public static string GetContent(params string[] sections)
{
try
{
if (sections.Any())
{
return Configuration[string.Join(":", sections)];
}
}
catch (Exception) { }
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();
}
}
}