public static string GetRequestID(HttpContext context) { if (context.Items.ContainsKey(RequestIDKey)) { return(context.Items[RequestIDKey].ToString()); } WebParams webParams = new WebParams(context.Request); if (webParams.ContainsKey(RequestIDKey)) { return(webParams[RequestIDKey]); } if (context.Request.Headers.ContainsKey(RequestIDKey)) { return(context.Request.Headers[RequestIDKey].ToString()); } string requestID = Guid.NewGuid().ToString("N"); context.Items.Add(RequestIDKey, requestID); return(requestID); }
public Task CreateValueProviderAsync(ValueProviderFactoryContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var request = context.ActionContext.HttpContext.Request; WebParams webParams = new WebParams(request); string encryptKey = "__encryptdata"; string encryptData = request.Query.ContainsKey(encryptKey) ? request.Query[encryptKey].ToString() : string.Empty; if (encryptData.IsNullOrEmpty() && request.HasFormContentType) { encryptData = request.Form[encryptKey]; } bool isEncryptDatas = webParams.ContainsKey(encryptKey); bool isDecryptSucceed = false; Exception decryptException = null; IDictionary <string, string> dicDecryptDatas = new Dictionary <string, string>(); if (encryptData.IsNotNullOrEmpty()) { try { //生成密钥 //string rsaKey = RSAEncrypt.GenerateKey(); string rsaKey = IFConfigReader.RSAPrivateKey; //通过密钥创建对象 RSAEncrypt privateRSA = new RSAEncrypt(rsaKey); //解密 string decryptData = privateRSA.Decrypt(encryptData); //导出公钥 //string publicKey = privateRSA.ExportParameters(false); //通过公钥加密 //RSAEncrypt publicRSA = new RSAEncrypt(publicKey); foreach (var item in decryptData.Split('&')) { string[] values = item.Split('='); dicDecryptDatas.Add(values[0], values[1]); } isDecryptSucceed = true; } catch (Exception ex) { decryptException = ex; isDecryptSucceed = false; } if (dicDecryptDatas.Count() > 0) { AddResultsToHttpContext(context, isEncryptDatas, isDecryptSucceed, decryptException, dicDecryptDatas); return(AddValueProviderAsync(context, dicDecryptDatas)); } } AddResultsToHttpContext(context, isEncryptDatas, isDecryptSucceed, decryptException, dicDecryptDatas); return(TaskCache.CompletedTask); }
public override void OnActionExecuting(ActionExecutingContext context) { ip = context.HttpContext.Request.Host.Host; //将当前日志组件传递到其他中间件 this.HttpContext.Items.Add("__requestLogger", Logger); HttpRequest request = context.HttpContext.Request; WebParams webParams = new WebParams(request); string path = request.Path.Value.ToLower(); if (!path.Contains("/test/") && !WhitelistActions.Contains(path)) { #region 签名验证 if (context.Result == null) { string signKey = IFConfigReader.SignKey; NameValueCollection param = new NameValueCollection(); foreach (var webParam in webParams) { param.Add(webParam.Key, webParam.Value.ToString()); } string uriString = $"{request.Scheme}://{request.Host}{request.Path}"; Uri uri = new Uri(uriString); if (!UrlValidator.ValidatorSign(uri, param, signKey)) { APIReturn apiResult = new APIReturn(-93, "签名验证失败"); context.Result = this.FuncResult(apiResult); } } #endregion #region 时间戳验证 string stampKey = "timestamp"; if (context.Result == null && webParams.ContainsKey(stampKey)) { long timeStamp = webParams[stampKey].ToLong(); DateTime urlTime = DateTimeHelper.StampToDateTime(timeStamp); if (urlTime.AddMinutes(3) < DateTime.Now) { APIReturn apiResult = new APIReturn(-92, "URL已失效"); context.Result = this.FuncResult(apiResult); } } #endregion #region 加密验证 /* 暂不启用加密验证 * * if (context.Result == null) * { * if (!this.ReqData.IsEncryptDatas || !this.ReqData.IsDecryptSucceed) * { * APIReturn apiResult = new APIReturn(-91, "解密参数失败" + (this.ReqData.DecryptException != null ? $"({this.ReqData.DecryptException.Message})" : string.Empty)); * context.Result = this.FuncResult(apiResult); * } * } * */ #endregion } }