81 lines
2.6 KiB
C#
81 lines
2.6 KiB
C#
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|||
|
using ServiceShared;
|
|||
|
|
|||
|
namespace ServiceInside.Filter
|
|||
|
{
|
|||
|
public class ServiceOutsideRequest : Attribute, IAsyncResultFilter, IAsyncAuthorizationFilter
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// IP of ServiceOutside
|
|||
|
/// </summary>
|
|||
|
private static string ServiceOutsideIP = "";
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// If log type is debug, request headers will be printed out in the console
|
|||
|
/// </summary>
|
|||
|
private static Log.Types _LogType = Log.Types.INFO;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Sets the log type
|
|||
|
/// </summary>
|
|||
|
/// <param name="Log.Types">Log.Types</param>
|
|||
|
public static void SetLog(Log.Types logType)
|
|||
|
{
|
|||
|
_LogType = logType;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Sets the IP of ServiceOutside
|
|||
|
/// </summary>
|
|||
|
/// <param name="serviceInsideIP">IP of ServiceOutside</param>
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs request in debug mode
|
|||
|
/// </summary>
|
|||
|
/// <param name="context"></param>
|
|||
|
/// <param name="next"></param>
|
|||
|
/// <returns></returns>
|
|||
|
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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|