From e106f63e7415a5ab8c487172c051d3b37c95fbff Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Mon, 20 May 2019 10:38:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8A=82=E7=82=B9=E5=BC=80?= =?UTF-8?q?=E5=8F=91=E6=9D=BFCPU=E5=BA=8F=E5=88=97=E5=8F=B7=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/HelperExtensions.cs | 28 +++++++++++++++++++ projects/Infrastructure/Infrastructure.csproj | 1 + projects/IoT/IoTNode/Program.cs | 3 +- projects/IoT/IoTNode/Startup.cs | 4 +++ projects/IoT/IoTNode/appsettings.json | 3 +- projects/IoTCenter/Program.cs | 4 +-- 6 files changed, 37 insertions(+), 6 deletions(-) diff --git a/projects/Infrastructure/Extensions/HelperExtensions.cs b/projects/Infrastructure/Extensions/HelperExtensions.cs index da6a0aef..19d35c07 100644 --- a/projects/Infrastructure/Extensions/HelperExtensions.cs +++ b/projects/Infrastructure/Extensions/HelperExtensions.cs @@ -1,14 +1,42 @@ using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Management; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Runtime.InteropServices; +using System.Text.RegularExpressions; namespace Infrastructure.Extensions { public static class HelperExtensions { + public static string GetCPUNumber(this Helper helper) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + //bash:cat /proc/cpuinfo + return Regex.Match(File.ReadAllText("/proc/cpuinfo"), @"Serial\s*:\s*([^\s]+)").Groups[1].Value; + } + else + { + //cmd:WMic CPU get PRocessorID + string cpuID = string.Empty; + var mc = new ManagementClass("win32_processor"); + var moc = mc.GetInstances(); + + foreach (ManagementObject mo in moc) + { + if (cpuID == "") + { + cpuID = mo.Properties["processorID"].Value.ToString(); + } + } + return cpuID; + } + } + public static string GetMacAddress(this Helper helper) { var physicalAddress = NetworkInterface diff --git a/projects/Infrastructure/Infrastructure.csproj b/projects/Infrastructure/Infrastructure.csproj index b6e41ff5..123556f5 100644 --- a/projects/Infrastructure/Infrastructure.csproj +++ b/projects/Infrastructure/Infrastructure.csproj @@ -26,6 +26,7 @@ + diff --git a/projects/IoT/IoTNode/Program.cs b/projects/IoT/IoTNode/Program.cs index d81365be..ab1bb7b4 100644 --- a/projects/IoT/IoTNode/Program.cs +++ b/projects/IoT/IoTNode/Program.cs @@ -1,9 +1,8 @@ -using System.Collections.Generic; using Infrastructure.Application; using Infrastructure.Configuration; using Infrastructure.Web.Hosting; using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using System.Collections.Generic; namespace IoTNode { diff --git a/projects/IoT/IoTNode/Startup.cs b/projects/IoT/IoTNode/Startup.cs index 9791de12..bdbbc4bf 100644 --- a/projects/IoT/IoTNode/Startup.cs +++ b/projects/IoT/IoTNode/Startup.cs @@ -32,6 +32,10 @@ namespace IoTNode public Startup(IConfiguration configuration, IHostingEnvironment env) : base(configuration, env) { + if (configuration["id"].Md5().Md5() != Helper.Instance.GetCPUNumber().Md5().Md5()) + { + throw new Exception("设备id无效"); + } } public override void ConfigureServices(IServiceCollection services) diff --git a/projects/IoT/IoTNode/appsettings.json b/projects/IoT/IoTNode/appsettings.json index 8e7a0265..5dbaaaab 100644 --- a/projects/IoT/IoTNode/appsettings.json +++ b/projects/IoT/IoTNode/appsettings.json @@ -13,5 +13,6 @@ "AppSettings": { "database": "sqlite", "UseCookieSessionStore": false - } + }, + "id": "BFEBFBFF000506E3" } \ No newline at end of file diff --git a/projects/IoTCenter/Program.cs b/projects/IoTCenter/Program.cs index 1c650c8f..688e9ad6 100644 --- a/projects/IoTCenter/Program.cs +++ b/projects/IoTCenter/Program.cs @@ -1,10 +1,8 @@ -using System.Collections.Generic; using Infrastructure.Application; using Infrastructure.Configuration; -using Infrastructure.Extensions; using Infrastructure.Web.Hosting; using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using System.Collections.Generic; namespace IoTCenter {