76 lines
2.7 KiB
C#
76 lines
2.7 KiB
C#
using System;
|
|
using System.Collections.Specialized;
|
|
using System.Text;
|
|
using System.Web;
|
|
using Kreta.Web.Logging.Logger;
|
|
|
|
namespace Kreta.User.WebApi.RequestResponseLogger
|
|
{
|
|
class AuthenticateRequestBodyRenderer : LoggingFieldRenderer
|
|
{
|
|
const string SecretLogValue = "***";
|
|
|
|
const string Password = nameof(Password);
|
|
|
|
const string UserName = nameof(UserName);
|
|
|
|
public override void Render(string fieldName, LoggingFieldRendererContext context)
|
|
{
|
|
var contentBodyString = context.GetFieldValue(fieldName) as string;
|
|
|
|
if (!string.IsNullOrWhiteSpace(contentBodyString))
|
|
{
|
|
try
|
|
{
|
|
string userName = null;
|
|
|
|
NameValueCollection contentBodyElements = HttpUtility.ParseQueryString(contentBodyString);
|
|
|
|
foreach (string key in contentBodyElements.AllKeys)
|
|
{
|
|
if (string.Equals(key, Password, StringComparison.InvariantCultureIgnoreCase))
|
|
{
|
|
contentBodyElements[key] = SecretLogValue;
|
|
}
|
|
|
|
if (string.Equals(key, UserName, StringComparison.InvariantCultureIgnoreCase))
|
|
{
|
|
userName = contentBodyElements[key];
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(userName))
|
|
{
|
|
var requestUserIdentifier = new StringBuilder(userName.ToUpper());
|
|
|
|
var requestUri = context.GetFieldValue(RequestResponseLoggingFields.Request.Uri) as string;
|
|
|
|
if (!string.IsNullOrWhiteSpace(requestUri))
|
|
{
|
|
requestUserIdentifier.Append('/');
|
|
|
|
try
|
|
{
|
|
var uri = new Uri(requestUri);
|
|
requestUserIdentifier.Append(uri.Authority.Split('.')[0]);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
requestUserIdentifier.Append($"[{ex.Message}]");
|
|
}
|
|
}
|
|
|
|
context.SetFieldValue(RequestResponseLoggingFields.Request.User, requestUserIdentifier.ToString());
|
|
}
|
|
|
|
context.SetFieldValue(fieldName, contentBodyElements.ToString());
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
context.SetFieldValue(fieldName, ex.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|