You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.7 KiB
77 lines
2.7 KiB
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
|
|
namespace Infrastructure.Web
|
|
{
|
|
public class DeviceAttribute : ActionFilterAttribute
|
|
{
|
|
public static string DeviceId { get; private set; }
|
|
|
|
static DeviceAttribute()
|
|
{
|
|
DeviceId = $"{Helper.Instance.GetCPUNumber()}{Helper.Instance.GetMacAddress()}".Md5();
|
|
}
|
|
|
|
public override void OnResultExecuting(ResultExecutingContext context)
|
|
{
|
|
var result = true;
|
|
var message = "";
|
|
var sn = DeviceId;
|
|
try
|
|
{
|
|
var config = context.HttpContext.RequestServices.GetService<IConfiguration>();
|
|
var hashCode = config["code"];
|
|
var code = hashCode.DESDecrypt(sn);
|
|
var values = code.Split('-');
|
|
if (sn != values[0])
|
|
{
|
|
message = $"授权码不匹配当前设备{sn}";
|
|
result = false;
|
|
}
|
|
else
|
|
{
|
|
var timeSeconds = Convert.ToInt64(values[1]);
|
|
if (timeSeconds != 0)
|
|
{
|
|
var endTime = DateTimeOffset.FromUnixTimeSeconds(timeSeconds);
|
|
if (endTime < DateTimeOffset.UtcNow)
|
|
{
|
|
message = $"当前设备{sn}的授权码已过期";
|
|
result = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
message = $"当前设备{sn}的授权码无效";
|
|
result = false;
|
|
}
|
|
if (!result)
|
|
{
|
|
if (context.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
|
|
{
|
|
context.Result = new JsonResult(new { code = 1, message = message });
|
|
}
|
|
else
|
|
{
|
|
var queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
|
|
queryString["message"] = message;
|
|
queryString["returnUrl"] = "/Admin/Configuration";
|
|
var url = $"/Admin/Configuration/RedirectTo?{queryString.ToString()}";
|
|
context.Result = new RedirectResult(url);
|
|
}
|
|
}
|
|
}
|
|
|
|
private string GetCode(string deviceId)
|
|
{
|
|
return DeviceId.Base64UrlEncode().Md5();
|
|
}
|
|
}
|
|
} |