创建签名验证自定义中间件 ,优化代码结构

This commit is contained in:
2021-02-26 16:40:51 +08:00
parent 7c3ce74a3b
commit d6617f47c0
8 changed files with 128 additions and 54 deletions

View File

@@ -14,30 +14,5 @@ namespace QRCodeService.Models
public int AppId { get; set; }
public string Time { get; set; }
public string Sign { get; set; }
public async Task<bool> CheckSignAsync(string appkey)
{
//除Sign字段以外按key名排序
var props = this.GetType().GetProperties();
var signStr = string.Join(null,props.Where(p=>p.Name!="Sign").OrderBy(p => p.Name).Select(p=>p.GetValue(this).ToString()));
var sign = Convert.ToBase64String((signStr + appkey).ToMD5());
return sign == Sign;
}
public bool CheckTime()
{
var timeDate = Time.ToDate("yyyyMMddhhmmss");
if (timeDate == null)
{
return false;
}
if (Math.Abs((timeDate.Value - DateTime.Now).TotalSeconds) > 60)
{
return false;
}
return true;
}
public async Task<bool> CheckValidAsync(string appkey)
{
return CheckTime() && await CheckSignAsync(appkey);
}
}
}