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.
79 lines
2.8 KiB
79 lines
2.8 KiB
using Infrastructure.Application.Services.Settings;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Globalization;
|
|
|
|
namespace Infrastructure.Web
|
|
{
|
|
public sealed class DeviceAttribute : ActionFilterAttribute
|
|
{
|
|
public static string DeviceId { get; private set; }
|
|
|
|
static DeviceAttribute()
|
|
{
|
|
DeviceId = Helper.Instance.GetMacAddress().Md5();
|
|
}
|
|
|
|
public override void OnResultExecuting(ResultExecutingContext context)
|
|
{
|
|
if (context is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(context));
|
|
}
|
|
|
|
var result = true;
|
|
var message = "";
|
|
var sn = DeviceId;
|
|
try
|
|
{
|
|
var settingService = context.HttpContext.RequestServices.GetService<ISettingService>();
|
|
var hashCode = settingService.GetSetting("code").Value;
|
|
var code = hashCode.DESDecrypt(sn);
|
|
var values = code.Split('-');
|
|
if (sn != values[0])
|
|
{
|
|
message = $"授权码不匹配当前设备{sn}";
|
|
result = false;
|
|
}
|
|
else
|
|
{
|
|
var timeSeconds = Convert.ToInt64(values[1], CultureInfo.CurrentCulture);
|
|
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 });
|
|
}
|
|
else
|
|
{
|
|
var queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
|
|
queryString["rawMesage"] = message;
|
|
queryString["returnUrl"] = "/Admin/Setting";
|
|
var url = $"/Admin/Setting/RedirectTo?{queryString}";
|
|
context.Result = new RedirectResult(url);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |