忽略dll文件git
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.OpenApi
|
||||
{
|
||||
/// <summary>
|
||||
/// 接入的应用
|
||||
/// </summary>
|
||||
public class Application
|
||||
{
|
||||
/// <summary>
|
||||
/// 应用唯一标识
|
||||
/// </summary>
|
||||
public string AppId { get; set; } = "";
|
||||
|
||||
/// <summary>
|
||||
/// 应用密钥
|
||||
/// </summary>
|
||||
public string AppKey { get; set; } = "";
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
public bool Enable { get; set; } = true;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Hncore.Infrastructure.Common;
|
||||
using Hncore.Infrastructure.Extension;
|
||||
using Hncore.Infrastructure.Serializer;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Hncore.Infrastructure.Core.Web;
|
||||
|
||||
namespace Hncore.Infrastructure.OpenApi
|
||||
{
|
||||
public class OpenApiAuthAttribute : TypeFilterAttribute
|
||||
{
|
||||
public OpenApiAuthAttribute() : base(typeof(OpenApiAuthFilter))
|
||||
{
|
||||
Order = -9997;
|
||||
}
|
||||
}
|
||||
|
||||
public class OpenApiAuthFilter : IAsyncAuthorizationFilter
|
||||
{
|
||||
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
|
||||
{
|
||||
if (context.Filters.Any(item => item is IAllowAnonymousFilter))
|
||||
{
|
||||
context.HttpContext.Items["AllowAnonymous"] = true;
|
||||
return;
|
||||
}
|
||||
|
||||
context.HttpContext.Items["OpenApi"] = true;
|
||||
|
||||
var body = await context.HttpContext.Request.ReadBodyAsStringAsync();
|
||||
|
||||
var requestBase = body.FromJsonTo<OpenApiRequestBase>();
|
||||
|
||||
if (requestBase.Timestamp==null)
|
||||
{
|
||||
OpenApiException.Throw(OpenApiReturnCode.Error,"缺少timestamp参数");
|
||||
}
|
||||
|
||||
if (!requestBase.Sign.Has())
|
||||
{
|
||||
OpenApiException.Throw(OpenApiReturnCode.Error,"缺少sign参数");
|
||||
}
|
||||
|
||||
if (!requestBase.AppId.Has())
|
||||
{
|
||||
OpenApiException.Throw(OpenApiReturnCode.Error,"缺少appid参数");
|
||||
}
|
||||
|
||||
var application = await RedisHelper.HGetAsync<Application>("OpenApi:Application", requestBase.AppId);
|
||||
|
||||
context.HttpContext.Items["OpenApiAppKey"] = application.AppKey;
|
||||
|
||||
if (!application.Enable)
|
||||
{
|
||||
OpenApiException.Throw(OpenApiReturnCode.Unauthorized);
|
||||
}
|
||||
|
||||
if (DateTimeHelper.ToUnixTimestamp(DateTime.Now) - requestBase.Timestamp > 60)
|
||||
{
|
||||
OpenApiException.Throw(OpenApiReturnCode.TimeStampExpired);
|
||||
}
|
||||
|
||||
requestBase.CheckSign(application.AppKey);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Hncore.Infrastructure.OpenApi
|
||||
{
|
||||
public class OpenApiException: Exception
|
||||
{
|
||||
public OpenApiReturnCode Code { get; } = OpenApiReturnCode.InternalError;
|
||||
|
||||
public OpenApiException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public OpenApiException(OpenApiReturnCode code, string message = "") : base(message)
|
||||
{
|
||||
Code = code;
|
||||
}
|
||||
|
||||
public static void Throw(string message = "")
|
||||
{
|
||||
throw new OpenApiException(message);
|
||||
}
|
||||
|
||||
public static void Throw(OpenApiReturnCode code, string message = "")
|
||||
{
|
||||
throw new OpenApiException(code, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Hncore.Infrastructure.Extension;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Hncore.Infrastructure.OpenApi
|
||||
{
|
||||
public class OpenApiRequestBase
|
||||
{
|
||||
[JsonProperty("appid")]
|
||||
public string AppId { get; set; }
|
||||
|
||||
[JsonProperty("timestamp")]
|
||||
public long? Timestamp { get; set; }
|
||||
|
||||
[JsonProperty("sign")]
|
||||
public string Sign { get; set; }
|
||||
|
||||
public void CheckSign(string key)
|
||||
{
|
||||
var sign = OpenApiSignUtil.CreateSign(this.Timestamp.ToLong(), key);
|
||||
|
||||
if (!String.Equals(sign, Sign, StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
OpenApiException.Throw(OpenApiReturnCode.SignError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using Hncore.Infrastructure.Common;
|
||||
using Hncore.Infrastructure.Extension;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Hncore.Infrastructure.OpenApi
|
||||
{
|
||||
public class OpenApiResult<T> where T : class, new()
|
||||
{
|
||||
[JsonProperty("code")] public OpenApiReturnCode Code { get; private set; }
|
||||
|
||||
[JsonProperty("message")] public string Message { get; private set; } = "";
|
||||
|
||||
[JsonProperty("timestamp")] public long Timestamp { get; set; }
|
||||
|
||||
[JsonProperty("sign")] public string Sign { get; set; }
|
||||
|
||||
[JsonProperty("data")] public T Data { get; set; } = new T();
|
||||
|
||||
private static readonly Dictionary<Enum, string> Dic;
|
||||
|
||||
static OpenApiResult()
|
||||
{
|
||||
Dic = ObjectExtension.ToDescriptionDictionary<OpenApiReturnCode>();
|
||||
}
|
||||
|
||||
|
||||
public OpenApiResult(OpenApiReturnCode code = OpenApiReturnCode.Success, string message = "")
|
||||
{
|
||||
Code = code;
|
||||
|
||||
if (string.IsNullOrEmpty(message) && Dic.ContainsKey(Code))
|
||||
{
|
||||
Message = Dic[Code];
|
||||
}
|
||||
else
|
||||
{
|
||||
Message = message;
|
||||
}
|
||||
|
||||
this.Timestamp = DateTimeHelper.ToUnixTimestamp(DateTime.Now);
|
||||
}
|
||||
|
||||
public void CreateSign(string key)
|
||||
{
|
||||
this.Sign = OpenApiSignUtil.CreateSign(this.Timestamp, key);
|
||||
}
|
||||
|
||||
public OpenApiResult<T> CreateSign(HttpContext httpContext)
|
||||
{
|
||||
var key = httpContext.Items["OpenApiAppKey"].ToString();
|
||||
|
||||
this.Sign = OpenApiSignUtil.CreateSign(this.Timestamp, key);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public class OpenApiResult : OpenApiResult<object>
|
||||
{
|
||||
public OpenApiResult(OpenApiReturnCode code = OpenApiReturnCode.Success, string message = "") : base(code,
|
||||
message)
|
||||
{
|
||||
}
|
||||
|
||||
public new OpenApiResult CreateSign(HttpContext httpContext)
|
||||
{
|
||||
var key = httpContext.Items["OpenApiAppKey"].ToString();
|
||||
|
||||
this.Sign = OpenApiSignUtil.CreateSign(this.Timestamp, key);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public enum OpenApiReturnCode
|
||||
{
|
||||
/// <summary>
|
||||
/// 成功
|
||||
/// </summary>
|
||||
[Description("成功")] Success = 10000,
|
||||
|
||||
/// <summary>
|
||||
/// 验签失败
|
||||
/// </summary>
|
||||
[Description("未授权")] Unauthorized = 40001,
|
||||
|
||||
/// <summary>
|
||||
/// 验签失败
|
||||
/// </summary>
|
||||
[Description("验签失败")] SignError = 40002,
|
||||
|
||||
/// <summary>
|
||||
/// 时间戳过期
|
||||
/// </summary>
|
||||
[Description("时间戳过期")] TimeStampExpired = 40003,
|
||||
|
||||
/// <summary>
|
||||
/// 内部错误
|
||||
/// </summary>
|
||||
[Description("内部错误")] InternalError = 50000,
|
||||
|
||||
/// <summary>
|
||||
/// 处理失败
|
||||
/// </summary>
|
||||
[Description("处理失败")] Error = 500001
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using Hncore.Infrastructure.Common;
|
||||
using Hncore.Infrastructure.Data;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Hncore.Infrastructure.OpenApi
|
||||
{
|
||||
public static class OpenApiSignUtil
|
||||
{
|
||||
public static string CreateSign(long timestamp, string key)
|
||||
{
|
||||
return SecurityHelper.GetMd5Hash(timestamp.ToString() + key);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user