创建签名验证自定义中间件 ,优化代码结构
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user