update:请求加密&防篡改
This commit is contained in:
43
QRCodeService/Models/CheckSignModel.cs
Normal file
43
QRCodeService/Models/CheckSignModel.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user