private static IEnumerable <KeyValuePair <string, string> > GetClaims(Microsoft.AspNetCore.Http.HttpRequest httpRequest) { if (httpRequest == null) { throw new ArgumentNullException(nameof(httpRequest)); } IIdentity identity = httpRequest.HttpContext?.User?.Identity; if (identity == null) { return(new List <KeyValuePair <string, string> >()); } if (identity is ClaimsIdentity == false) { return(new List <KeyValuePair <string, string> >()); } ClaimsIdentity claimsIdentity = identity as ClaimsIdentity; List <KeyValuePair <string, string> > claims = InternalHelpers.ToKeyValuePair(claimsIdentity); return(claims); }
public static KissLog.Http.HttpRequest Create(Microsoft.AspNetCore.Http.HttpRequest httpRequest) { if (httpRequest == null) { throw new ArgumentNullException(nameof(httpRequest)); } Session session = KissLog.InternalHelpers.WrapInTryCatch(() => GetSession(httpRequest)); session = session ?? new Session(); bool isAuthenticated = httpRequest.HttpContext?.User?.Identity?.IsAuthenticated ?? false; KissLog.Http.HttpRequest result = new KissLog.Http.HttpRequest(new KissLog.Http.HttpRequest.CreateOptions { Url = new Uri(httpRequest.GetDisplayUrl()), HttpMethod = httpRequest.Method, UserAgent = GetUserAgent(httpRequest.Headers), HttpReferer = GetHttpReferrer(httpRequest.Headers), RemoteAddress = httpRequest.HttpContext?.Connection?.RemoteIpAddress?.ToString(), MachineName = InternalHelpers.GetMachineName(), IsNewSession = session.IsNewSession, SessionId = session.SessionId, IsAuthenticated = isAuthenticated }); RequestProperties.CreateOptions propertiesOptions = new RequestProperties.CreateOptions(); propertiesOptions.Cookies = InternalHelpers.ToKeyValuePair(httpRequest.Cookies); propertiesOptions.Headers = InternalHelpers.ToKeyValuePair(httpRequest.Headers); propertiesOptions.QueryString = InternalHelpers.ToKeyValuePair(httpRequest.Query); propertiesOptions.Claims = GetClaims(httpRequest); if (httpRequest.HasFormContentType) { if (KissLogConfiguration.Options.Handlers.ShouldLogFormData.Invoke(result) == true) { propertiesOptions.FormData = InternalHelpers.ToKeyValuePair(httpRequest.Form); } } if (KissLog.InternalHelpers.CanReadRequestInputStream(propertiesOptions.Headers)) { if (KissLogConfiguration.Options.Handlers.ShouldLogInputStream.Invoke(result) == true) { propertiesOptions.InputStream = KissLog.InternalHelpers.WrapInTryCatch(() => { return(ModuleInitializer.ReadInputStreamProvider.ReadInputStream(httpRequest)); }); } } result.SetProperties(new RequestProperties(propertiesOptions)); return(result); }
public static HttpResponse Create(Microsoft.AspNetCore.Http.HttpResponse httpResponse, long contentLength) { if (httpResponse == null) { throw new ArgumentNullException(nameof(httpResponse)); } if (contentLength < 0) { throw new ArgumentException(nameof(contentLength)); } var options = new HttpResponse.CreateOptions(); options.StatusCode = httpResponse.StatusCode; options.Properties = new ResponseProperties(new ResponseProperties.CreateOptions { Headers = InternalHelpers.ToKeyValuePair(httpResponse.Headers), ContentLength = contentLength }); return(new HttpResponse(options)); }