添加节点开发板CPU序列号验证

TangShanKaiPing
wanggang 6 years ago
parent f07761eaf4
commit e106f63e74

@ -1,14 +1,42 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Management;
using System.Net; using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Net.Sockets; using System.Net.Sockets;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace Infrastructure.Extensions namespace Infrastructure.Extensions
{ {
public static class HelperExtensions 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) public static string GetMacAddress(this Helper helper)
{ {
var physicalAddress = NetworkInterface var physicalAddress = NetworkInterface

@ -26,6 +26,7 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" /> <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
<PackageReference Include="System.Drawing.Common" Version="4.5.1" /> <PackageReference Include="System.Drawing.Common" Version="4.5.1" />
<PackageReference Include="System.Management" Version="4.5.0" />
<PackageReference Include="ValueInjecter" Version="3.1.3" /> <PackageReference Include="ValueInjecter" Version="3.1.3" />
<PackageReference Include="Flurl" Version="2.8.1" /> <PackageReference Include="Flurl" Version="2.8.1" />
</ItemGroup> </ItemGroup>

@ -1,9 +1,8 @@
using System.Collections.Generic;
using Infrastructure.Application; using Infrastructure.Application;
using Infrastructure.Configuration; using Infrastructure.Configuration;
using Infrastructure.Web.Hosting; using Infrastructure.Web.Hosting;
using Microsoft.AspNetCore; using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using System.Collections.Generic;
namespace IoTNode namespace IoTNode
{ {

@ -32,6 +32,10 @@ namespace IoTNode
public Startup(IConfiguration configuration, IHostingEnvironment env) : base(configuration, env) 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) public override void ConfigureServices(IServiceCollection services)

@ -13,5 +13,6 @@
"AppSettings": { "AppSettings": {
"database": "sqlite", "database": "sqlite",
"UseCookieSessionStore": false "UseCookieSessionStore": false
} },
"id": "BFEBFBFF000506E3"
} }

@ -1,10 +1,8 @@
using System.Collections.Generic;
using Infrastructure.Application; using Infrastructure.Application;
using Infrastructure.Configuration; using Infrastructure.Configuration;
using Infrastructure.Extensions;
using Infrastructure.Web.Hosting; using Infrastructure.Web.Hosting;
using Microsoft.AspNetCore; using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using System.Collections.Generic;
namespace IoTCenter namespace IoTCenter
{ {

Loading…
Cancel
Save