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.
55 lines
2.0 KiB
55 lines
2.0 KiB
using Microsoft.Extensions.Options;
|
|
using Microsoft.Extensions.Primitives;
|
|
using QYMessageCenter.Common.Models;
|
|
|
|
namespace QYMessageCenter.Middlewares
|
|
{
|
|
public class ClientVersionValidationMiddleWare
|
|
{
|
|
/// <summary>
|
|
/// 管道请求委托
|
|
/// </summary>
|
|
private RequestDelegate _next;
|
|
|
|
private IDictionary<string, int> apiVersionDictionary;
|
|
|
|
private IOptionsMonitor<List<ClientVersionValidationModel>> _monitor;
|
|
|
|
public ClientVersionValidationMiddleWare(RequestDelegate requestDelegate, IOptionsMonitor<List<ClientVersionValidationModel>> monitor)
|
|
{
|
|
_next = requestDelegate;
|
|
_monitor = monitor;
|
|
apiVersionDictionary = new Dictionary<string, int>();
|
|
}
|
|
|
|
public async Task Invoke(HttpContext context)
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine(context.Request.Path);
|
|
var apiRequirement = _monitor.CurrentValue.FirstOrDefault(x => x.Api.Equals(context.Request.Path, StringComparison.CurrentCultureIgnoreCase));
|
|
if (apiRequirement != null)
|
|
{
|
|
if (!context.Request.Headers.TryGetValue("ClientVersion", out StringValues clientVersionStr))
|
|
throw new BusinessException("缺少版本信息,请更新步步为盈");
|
|
if (!int.TryParse(clientVersionStr, out int clientVersion))
|
|
throw new BusinessException("版本信息不正确,请更新步步为盈");
|
|
if (clientVersion < apiRequirement.MinimumVersion)
|
|
throw new BusinessException("当前请求需更新步步为盈~!");
|
|
}
|
|
await _next(context); //调用管道执行下一个中间件
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class ClientVersionValidationModel
|
|
{
|
|
public string Api { get; set; }
|
|
|
|
public int MinimumVersion { get; set; }
|
|
}
|
|
}
|
|
|