using Microsoft.AspNetCore.Mvc.Filters; using ServiceShared; namespace ServiceInside.Filter { public class ServiceOutsideRequest : Attribute, IAsyncResultFilter, IAsyncAuthorizationFilter { /// /// IP of ServiceOutside /// private static string ServiceOutsideIP = ""; /// /// If log type is debug, request headers will be printed out in the console /// private static Log.Types _LogType = Log.Types.INFO; /// /// Sets the log type /// /// Log.Types public static void SetLog(Log.Types logType) { _LogType = logType; } /// /// Sets the IP of ServiceOutside /// /// IP of ServiceOutside public static void SetServiceInsideIP(string serviceOutsideIP) { ServiceOutsideIP = serviceOutsideIP; } public virtual async Task OnAuthorizationAsync(AuthorizationFilterContext context) { if (context == null || context.HttpContext == null || context.HttpContext.Connection == null || context.HttpContext.Connection.RemoteIpAddress == null || context.HttpContext.Connection.RemoteIpAddress.ToString() != ServiceOutsideIP) { return; } } /// /// Logs request in debug mode /// /// /// /// public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { if (_LogType == Log.Types.DEBUG) { string headers = ""; foreach (var header in context.HttpContext.Request.Headers) { headers += header.Key + "=" + header.Value + "\r\n"; } string message = ""; if (context != null && context.HttpContext != null && context.HttpContext.Connection != null && context.HttpContext.Connection.RemoteIpAddress != null) { message = context.HttpContext.Connection.RemoteIpAddress.ToString() + " " + context.ActionDescriptor.DisplayName + "\r\nHeaders:" + headers; } Log.Debug(message); } await next(); } } }