/// <summary> /// 验证 /// </summary> /// <param name="filterContext"></param> protected virtual bool Verify(ActionExecutingContext filterContext) { var value = GetValue(filterContext); var sign = filterContext.RequestContext.RouteData.Values["sign"] ?? filterContext.RequestContext.HttpContext.Request["sign"]; var token = filterContext.RequestContext.RouteData.Values["token"] ?? filterContext.RequestContext.HttpContext.Request["token"]; var args = new ApiArgsEntity { Ip = HttpContextHelper.GetClientIp(), Method = Method, Sign = sign.Convert <string>(), Token = token.Convert <string>(), Value = value }; var info = Ioc.Resolve <IApiEngineApplicationService>().Verify(args); filterContext.Controller.ViewBag.Verification = info; filterContext.Controller.ViewBag.Method = Method; if (info != null && info.Error != null) { filterContext.Result = new ContentResult { Content = GetErrorResult(info.Error.Key, info.Error.Message) }; } return(info != null && info.IsPass); }
/// <summary> /// 添加错误信息 /// </summary> public static void AddExceptionLog(this Controller handle, Exception ex, IdentityEntity identity = null) { var info = new ErrorEntity { Address = HttpContext.Current.Request.Url.ToString(), Ip = HttpContextHelper.GetClientIp(), Device = HttpContext.Current.Request.UserAgent, SaveType = SaveType.Add }; if (identity != null) { info.Account = new AccountEntity { Id = identity.Id }; } info.SetEntity(ex); Ioc.Resolve <IApplicationService, ErrorEntity>().Save(info); }
/// <summary> /// 添加登入日志 /// </summary> /// <param name="controller"></param> /// <param name="identity"></param> /// <param name="type"></param> /// <param name="message"></param> public static void AddLoginLog(this Controller controller, IdentityEntity identity, string type, string message) { if (identity == null) { return; } var info = new LoginEntity { Type = string.IsNullOrEmpty(type) ? "Account" : type, Ip = HttpContextHelper.GetClientIp(), Address = controller.Request.Url.ToString(), Device = controller.Request.UserAgent, Account = new AccountEntity { Id = identity.Id }, Message = message, SaveType = SaveType.Add }; Ioc.Resolve <IApplicationService, LoginEntity>().Save(info); }
/// <summary> /// 添加日志 /// </summary> public virtual void AddLog(ResultExecutedContext filterContext) { VerificationEntity verification = filterContext.Controller.ViewBag.Verification; string method = filterContext.Controller.ViewBag.Method; var accountId = verification == null || verification.Voucher == null || verification.Voucher.Account == null ? 0 : verification.Voucher.Account.Id; string key = filterContext.Controller.ViewBag.TraceKey; var info = new ApiTraceEntity { Method = method, Key = key, Request = GetRequest(filterContext), Response = GetResponse(filterContext), Ip = HttpContextHelper.GetClientIp(), Account = new AccountEntity { Id = accountId }, SaveType = SaveType.Add }; Ioc.Resolve <IApplicationService, ApiTraceEntity>().Save(info); }
/// <summary> /// 添加异常 /// </summary> private static void AddExceptionLog() { try { var url = ConfigurationManager.GetSetting <string>("PresentationWebsiteSharedUrl"); var action = CheckSave(); if (string.IsNullOrEmpty(action)) { var info = new ErrorEntity { Address = HttpContext.Current.Request.Url.ToString(), Ip = HttpContextHelper.GetClientIp(), Device = HttpContext.Current.Request.UserAgent, SaveType = SaveType.Add }; info.SetEntity(HttpContext.Current.Server.GetLastError()); info.Account = new AccountEntity { Id = 0 }; Ioc.Resolve <IApplicationService, ErrorEntity>().Save(info); action = "General"; } if (ConfigurationManager.GetSetting <bool>("IsDebug")) { throw HttpContext.Current.Server.GetLastError(); } HttpContext.Current.Response.Redirect(string.Format("{0}/Error/{1}", url, action)); } catch (Exception ex) { if (ex.InnerException != null) { throw ex.InnerException; } throw ex; } }
/// <summary> /// 添加异常 /// </summary> /// <param name="handle"></param> static public void AddApiErrorLog(this HttpApplication handle) { try { var ex = HttpContext.Current.Server.GetLastError(); var action = CheckSave(); if (string.IsNullOrEmpty(action)) { var info = new ErrorEntity { Address = HttpContext.Current.Request.Url.ToString(), Ip = HttpContextHelper.GetClientIp(), Device = HttpContext.Current.Request.UserAgent, SaveType = SaveType.Add }; info.SetEntity(ex); info.Account = new AccountEntity { Id = 0 }; Ioc.Resolve <IApplicationService, ErrorEntity>().Save(info); } if (ConfigurationManager.GetSetting <bool>("IsDebug")) { throw ex; } HttpContext.Current.Response.Redirect("/Shared/Error"); } catch (Exception ex) { if (ex.InnerException != null) { throw ex.InnerException; } throw ex; } }
public virtual void ProcessRequest(HttpContext httpContext, out string action) { action = null; ISerializer serializer = null; Response response = null; var processData = new ProcessRequestData() { HttpContext = httpContext, Url = httpContext.Request.Url.ToString() }; var isDebugRequest = false; try { // получим IP processData.Ip = HttpContextHelper.GetClientIp(httpContext); // получим action action = GetAction(httpContext); processData.Action = action; if (string.IsNullOrEmpty(action)) { response = GetErrorResponse(ResponseCode.UnknownAction, "Не указано действие"); return; } if (string.Equals("Debug", action, StringComparison.InvariantCultureIgnoreCase)) { isDebugRequest = true; string debugInfo; try { debugInfo = GetDebugInfo(); } catch (Exception exception) { debugInfo = exception.ToString(); } httpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); httpContext.Response.Cache.AppendCacheExtension("no-store, must-revalidate"); httpContext.Response.ContentType = "text/plain"; httpContext.Response.Charset = "utf-8"; httpContext.Response.Write(debugInfo); return; } if (string.Equals("Test", action, StringComparison.InvariantCultureIgnoreCase)) { isDebugRequest = true; string testInfo; try { testInfo = GetTestInfo(); } catch (Exception exception) { testInfo = exception.ToString(); } httpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); httpContext.Response.Cache.AppendCacheExtension("no-store, must-revalidate"); httpContext.Response.ContentType = "text/plain"; httpContext.Response.Charset = "utf-8"; httpContext.Response.Write(testInfo); return; } // получим метод var method = GetMethodInfo(action); if (method == null) { response = GetErrorResponse(ResponseCode.UnknownAction, "Неизвестное действие: " + action); action = null; return; } // получим тело var body = GetRequestBody(httpContext); if (body.Length < httpContext.Request.ContentLength) { throw new IncompleteRequestException(body.Length, httpContext.Request.ContentLength); } processData.Body = Encoding.UTF8.GetString(body); // получим serializer serializer = GetSerializer(httpContext); processData.ContentType = httpContext.Request.ContentType; // получим пакет запроса var requestPackage = GetRequestPackage(method, serializer, body); // получим обработчик var handler = GetRealHandler(processData.Ip, httpContext); // получим response var timer = new Stopwatch(); timer.Start(); response = InvokeMethod(handler, method, requestPackage); timer.Stop(); AddInvokeDuration((int)(timer.Elapsed).TotalMilliseconds); } catch (ThreadAbortException) { } catch (Exception exception) { response = HandleException(exception, processData); } finally { if (isDebugRequest == false) { if (response == null) { response = GetErrorResponse(ResponseCode.ServerError, "Неизвестная ошибка сервера"); } if (serializer == null) { serializer = new JsonSerializer(); } httpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); httpContext.Response.Cache.AppendCacheExtension("no-store, must-revalidate"); httpContext.Response.ContentType = "application/" + serializer.Format; httpContext.Response.Charset = "utf-8"; var responseBytes = serializer.GetBytes(response); httpContext.Response.BinaryWrite(responseBytes); } } }