update:请求加密&防篡改

This commit is contained in:
2021-02-25 10:39:30 +08:00
parent 9834c7a988
commit 7fe75e8c7c
11 changed files with 145 additions and 27 deletions

View File

@@ -0,0 +1,43 @@
using QRCodeService.Application.Queries;
using QRCodeService.Extensions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QRCodeService.Models
{
public abstract class CheckSignModel
{
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);
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using QRCodeService.Application.Queries;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -6,9 +7,8 @@ using System.Threading.Tasks;
namespace QRCodeService.Models
{
public class CreateLinkModel
public class CreateLinkModel: CheckSignModel
{
public string SuffixUrl { get; set; }
public string Time { get; set; }
}
}

View File

@@ -1,16 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QRCodeService.Models
{
public class GenInputModel
{
public int AppId { get; set; }
public string Time { get; set; }
public string SuffixUrl { get; set; }
public string Sign { get; set; }
}
}