//获取js_api_ticket public static string GetJsApiTicket() { string ticket; ICacheManager cache = new MemoryCacheManager(); var contains = cache.Contains("js_api_ticket"); if (contains) { ticket = cache.GetCache <string>("js_api_ticket"); } else { var httpGet = HttpHelper.HttpGet(GetTicketUrl(), ""); var jsApiTicketModel = JsonConvert.DeserializeObject <JsApiTicketModel>(httpGet); if (jsApiTicketModel.errcode == 0) { ticket = jsApiTicketModel.ticket; long expiresIn = jsApiTicketModel.expires_in; cache.SetCache("js_api_ticket", ticket, TimeSpan.FromSeconds(expiresIn - 60)); } else { ticket = null; } } // var httpGet = HttpHelper.HttpGet(GetTicketUrl(), ""); // var jsApiTicketModel = JsonConvert.DeserializeObject<JsApiTicketModel>(httpGet); // ticket = jsApiTicketModel.errcode == 0 ? jsApiTicketModel.ticket : null; return(ticket); }
public async Task Invoke(HttpContext context) { RequestResponseLog _logInfo = new RequestResponseLog(); var token = context.Request.Headers["Authorization"].ToString().Replace("Bearer ", ""); var users = MemoryCacheManager.GetCache <UserDto>(token); HttpRequest request = context.Request; _logInfo.Url = request.Path.ToString(); IDictionary <string, string> Headers = request.Headers.ToDictionary(k => k.Key, v => string.Join(";", v.Value.ToList())); _logInfo.Headers = $"[" + string.Join(",", Headers.Select(i => "{" + $"\"{i.Key}\":\"{i.Value}\"" + "}")) + "]"; _logInfo.Method = request.Method; _logInfo.ExcuteStartTime = DateTime.Now; _logInfo.UserName = users?.UserName; _logInfo.IPAddress = request.HttpContext.Connection.RemoteIpAddress.ToString(); _logInfo.Port = request.HttpContext.Connection.RemotePort; //获取request.Body内容 if (request.Method.ToLower().Equals("post")) { request.EnableRewind(); //启用倒带功能,就可以让 Request.Body 可以再次读取 Stream stream = request.Body; byte[] buffer = new byte[request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); _logInfo.RequestBody = Encoding.UTF8.GetString(buffer); request.Body.Position = 0; } else if (request.Method.ToLower().Equals("get")) { _logInfo.RequestBody = request.QueryString.Value; } using (var responseBody = new MemoryStream()) { //获取Response.Body内容 var originalBodyStream = context.Response.Body; context.Response.Body = responseBody; await _next(context); _logInfo.ResponseBody = await FormatResponse(context.Response); //Log4Net.LogInfo($"VisitLog: {_logInfo.ToString()}"); await responseBody.CopyToAsync(originalBodyStream); } context.Response.OnCompleted(async o => { _logInfo.ExcuteEndTime = DateTime.Now; _appSystemServices.Create <RequestResponseLog>(_logInfo); }, context); }
private UserDto GetUsers() { var token = HttpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", ""); var users = MemoryCacheManager.GetCache <UserDto>(token); return(users); }
public void Intercept(IInvocation invocation) { if (AllowAnonymous(invocation.MethodInvocationTarget, invocation.TargetType)) { var clientInfo = ClientInfoProvider.GetClientInfo(); var key = invocation.Method.Name + invocation.Method.ReflectedType.Name + clientInfo.ClientIpAddress + clientInfo.ComputerName; var keycache = _cacheManager.GetCache(key); if (keycache == null) { //3秒内不能重复提交 _cacheManager.SetCache(key, DateTime.Now, 3); invocation.Proceed(); if (invocation.Method.IsAsync()) { if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = AsyncHelper.AwaitTaskWithFinally( (Task)invocation.ReturnValue, exception => _cacheManager.Remove(key) ); } else { invocation.ReturnValue = AsyncHelper.CallAwaitTaskWithFinallyAndGetResult( invocation.Method.ReturnType.GenericTypeArguments[0], invocation.ReturnValue, exception => _cacheManager.Remove(key) ); } } else { _cacheManager.Remove(key); } } else { throw new Exception("请不要重复提交!"); } } else { invocation.Proceed(); } }
//获取accessToken public static string GetAccessToken() { string accessToken; ICacheManager cache = new MemoryCacheManager(); var contains = cache.Contains("access_token"); if (contains) { accessToken = cache.GetCache <string>("access_token"); } else { var result = HttpHelper.HttpGet(GetAccessTokenUrl(), ""); var accessTokenModel = JsonConvert.DeserializeObject <AccessTokenModel>(result); accessToken = accessTokenModel.access_token; long expiresIn = accessTokenModel.expires_in; cache.SetCache("access_token", accessToken, TimeSpan.FromSeconds(expiresIn - 60)); } return(accessToken); }