Esempio n. 1
0
        public DefaultUserInformationProvider(IRequestInformationProvider requestInformationProvider)
        {
            if (requestInformationProvider == null)
            {
                throw new ArgumentNullException(nameof(requestInformationProvider));
            }

            _requestInformationProvider = requestInformationProvider;
        }
        public Task Invoke(HttpContext context)
        {
            ILogger logger = context.RequestServices.GetRequiredService <ILogger>();

            IRequestInformationProvider requestInformationProvider = context.RequestServices.GetRequiredService <IRequestInformationProvider>();

            LogRequestInformationMiddleware.LogRequest(logger, requestInformationProvider);

            return(_next.Invoke(context));
        }
        public async Task Invoke(HttpContext context)
        {
            ILogger logger = context.RequestServices.GetService <ILogger>();

            IRequestInformationProvider requestInformationProvider = context.RequestServices.GetService <IRequestInformationProvider>();

            LogRequestInformationMiddleware.LogRequest(logger, requestInformationProvider);

            await Next.Invoke(context);
        }
        public DefaultTimeZoneManager(IRequestInformationProvider requestInformationProvider)
        {
            if (requestInformationProvider == null)
            {
                throw new ArgumentNullException(nameof(requestInformationProvider));
            }

            _currentTimeZoneName = requestInformationProvider.CurrentTimeZone;

            _desiredTimeZoneName = requestInformationProvider.DesiredTimeZone;
        }
Esempio n. 5
0
        public override async Task Invoke(IOwinContext context)
        {
            IDependencyResolver dependencyResolver = context.GetDependencyResolver();

            ILogger logger = dependencyResolver.Resolve <ILogger>();

            IRequestInformationProvider requestInformationProvider =
                dependencyResolver.Resolve <IRequestInformationProvider>();

            LogRequest(logger, requestInformationProvider);

            await Next.Invoke(context);
        }
Esempio n. 6
0
        public virtual void Configure(IAppBuilder owinApp)
        {
            if (owinApp == null)
            {
                throw new ArgumentNullException(nameof(owinApp));
            }

            owinApp.Map("/core", coreApp =>
            {
                LogProvider.SetCurrentLogProvider(DependencyManager.Resolve <ILogProvider>());

                IdentityServerServiceFactory factory = new IdentityServerServiceFactory()
                                                       .UseInMemoryClients(DependencyManager.Resolve <IOAuthClientsProvider>().GetClients().ToArray())
                                                       .UseInMemoryScopes(ScopesProvider.GetScopes());

                IUserService ResolveUserService(IdentityServer3.Core.Services.IDependencyResolver resolver)
                {
                    OwinEnvironmentService owinEnv = resolver.Resolve <OwinEnvironmentService>();
                    IOwinContext owinContext       = new OwinContext(owinEnv.Environment);
                    IUserService userService       = owinContext.GetDependencyResolver().Resolve <IUserService>();
                    return(userService);
                }

                factory.UserService = new Registration <IUserService>(ResolveUserService);

                IEventService ResolveEventService(IdentityServer3.Core.Services.IDependencyResolver resolver)
                {
                    OwinEnvironmentService owinEnv = resolver.Resolve <OwinEnvironmentService>();
                    IOwinContext owinContext       = new OwinContext(owinEnv.Environment);
                    if (owinContext.TryGetDependencyResolver(out Core.Contracts.IDependencyResolver? dependencyResolver))
                    {
                        IRequestInformationProvider requestInformationProvider = dependencyResolver.Resolve <IRequestInformationProvider>();

                        if (IPAddress.TryParse(requestInformationProvider.ClientIp, out IPAddress _))
                        {
                            owinContext.Request.RemoteIpAddress = requestInformationProvider.ClientIp;
                        }
                        else
                        {
                            owinContext.Request.RemoteIpAddress = "::1";
                        }

                        return(dependencyResolver.Resolve <IEventService>());
                    }
                    else
                    {
                        return(new FakeEventService {
                        });
                    }
                }
Esempio n. 7
0
        public Task Invoke(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            ILogger logger = context.RequestServices.GetRequiredService <ILogger>();

            IRequestInformationProvider requestInformationProvider = context.RequestServices.GetRequiredService <IRequestInformationProvider>();

            LogRequestInformationMiddleware.LogRequest(logger, requestInformationProvider);

            return(_next.Invoke(context));
        }
Esempio n. 8
0
        public override Task Invoke(IOwinContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            IDependencyResolver dependencyResolver = context.GetDependencyResolver();

            ILogger logger = dependencyResolver.Resolve <ILogger>();

            IRequestInformationProvider requestInformationProvider =
                dependencyResolver.Resolve <IRequestInformationProvider>();

            LogRequest(logger, requestInformationProvider);

            return(Next.Invoke(context));
        }
        public virtual void Configure(IAppBuilder owinApp)
        {
            if (owinApp == null)
            {
                throw new ArgumentNullException(nameof(owinApp));
            }

            owinApp.Map("/core", coreApp =>
            {
                LogProvider.SetCurrentLogProvider(DependencyManager.Resolve <ILogProvider>());

                IdentityServerServiceFactory factory = new IdentityServerServiceFactory()
                                                       .UseInMemoryClients(DependencyManager.Resolve <IOAuthClientsProvider>().GetClients().ToArray())
                                                       .UseInMemoryScopes(ScopesProvider.GetScopes());

                IUserService ResolveUserService(IdentityServer3.Core.Services.IDependencyResolver resolver)
                {
                    OwinEnvironmentService owinEnv = resolver.Resolve <OwinEnvironmentService>();
                    IOwinContext owinContext       = new OwinContext(owinEnv.Environment);
                    IUserService userService       = owinContext.GetDependencyResolver().Resolve <IUserService>();
                    return(userService);
                }

                factory.UserService = new Registration <IUserService>(ResolveUserService);

                IEventService ResolveEventService(IdentityServer3.Core.Services.IDependencyResolver resolver)
                {
                    OwinEnvironmentService owinEnv = resolver.Resolve <OwinEnvironmentService>();
                    IOwinContext owinContext       = new OwinContext(owinEnv.Environment);
                    if (owinContext.TryGetDependencyResolver(out Core.Contracts.IDependencyResolver dependencyResolver))
                    {
                        IRequestInformationProvider requestInformationProvider = dependencyResolver.Resolve <IRequestInformationProvider>();
                        owinEnv.Environment["server.RemoteIpAddress"]          = requestInformationProvider.ClientIp ?? "::1"; // some test hosts won't provide remote ip address request feature and idSrv requires it in event sevice decorator.
                        return(dependencyResolver.Resolve <IEventService>());
                    }
                    else
                    {
                        return(new FakeEventService {
                        });
                    }
                }
Esempio n. 10
0
        public static void LogRequest(ILogger logger, IRequestInformationProvider requestInformationProvider)
        {
            logger.AddLogData(nameof(IRequestInformationProvider.HttpMethod), requestInformationProvider.HttpMethod);
            logger.AddLogData(nameof(IRequestInformationProvider.RequestUri), requestInformationProvider.RequestUri);

            if (requestInformationProvider.ClientAppVersion != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientAppVersion), requestInformationProvider.ClientAppVersion);
            }

            if (requestInformationProvider.SystemLanguage != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.SystemLanguage), requestInformationProvider.SystemLanguage);
            }

            if (requestInformationProvider.ClientCulture != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientCulture), requestInformationProvider.ClientCulture);
            }

            if (requestInformationProvider.ClientType != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientType), requestInformationProvider.ClientType);
            }

            if (requestInformationProvider.ClientDateTime != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientDateTime), requestInformationProvider.ClientDateTime);
            }

            if (requestInformationProvider.ClientDebugMode != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientDebugMode), requestInformationProvider.ClientDebugMode);
            }

            if (requestInformationProvider.ClientScreenSize != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientScreenSize), requestInformationProvider.ClientScreenSize);
            }

            if (requestInformationProvider.ContentType != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ContentType), requestInformationProvider.ContentType);
            }

            if (requestInformationProvider.Origin != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.Origin), requestInformationProvider.Origin);
            }

            if (requestInformationProvider.Referer != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.Referer), requestInformationProvider.Referer);
            }

            logger.AddLogData(nameof(IRequestInformationProvider.ClientIp), requestInformationProvider.ClientIp);

            if (requestInformationProvider.ClientPlatform != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientPlatform), requestInformationProvider.ClientPlatform);
            }

            if (requestInformationProvider.ClientRoute != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientRoute), requestInformationProvider.ClientRoute);
            }

            if (requestInformationProvider.ClientSysLanguage != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientSysLanguage), requestInformationProvider.ClientSysLanguage);
            }

            if (requestInformationProvider.ClientTheme != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientTheme), requestInformationProvider.ClientTheme);
            }

            if (requestInformationProvider.UserAgent != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.UserAgent), requestInformationProvider.UserAgent);
            }

            if (requestInformationProvider.CorrelationId != null)
            {
                logger.AddLogData("X-CorrelationId", requestInformationProvider.CorrelationId);
            }
        }
 public XCorrelationIdHttpMessageHandler(IRequestInformationProvider requestInformationProvider, HttpMessageHandler innerHandler)
     : base(innerHandler)
 {
     _requestInformationProvider = requestInformationProvider;
 }
Esempio n. 12
0
        public virtual void Initialize(ITelemetry telemetry)
        {
            if (telemetry == null)
            {
                throw new ArgumentNullException(nameof(telemetry));
            }

            LogEntryAppLevelConstantInfo logEntryAppLevelConstantInfo = LogEntryAppLevelConstantInfo.GetAppConstantInfo();

            if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.ApplicationName)))
            {
                telemetry.Context.GlobalProperties.Add(nameof(LogEntry.ApplicationName), logEntryAppLevelConstantInfo.ApplicationName);
            }
            if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppVersion)))
            {
                telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppVersion), logEntryAppLevelConstantInfo.AppVersion);
            }
            if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppEnvironmentName)))
            {
                telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppEnvironmentName), logEntryAppLevelConstantInfo.AppEnvironmentName);
            }
            if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppWasInDebugMode)))
            {
                telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppWasInDebugMode), logEntryAppLevelConstantInfo.AppWasInDebugMode.ToString());
            }
            if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerName)))
            {
                telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerName), logEntryAppLevelConstantInfo.AppServerName);
            }
            if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerOSVersion)))
            {
                telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerOSVersion), logEntryAppLevelConstantInfo.AppServerOSVersion);
            }
            if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerAppDomainName)))
            {
                telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerAppDomainName), logEntryAppLevelConstantInfo.AppServerAppDomainName);
            }
            if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerProcessId)))
            {
                telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerProcessId), logEntryAppLevelConstantInfo.AppServerProcessId.ToString());
            }
            if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerUserAccountName)))
            {
                telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerUserAccountName), logEntryAppLevelConstantInfo.AppServerUserAccountName);
            }
            if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerWas64Bit)))
            {
                telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerWas64Bit), logEntryAppLevelConstantInfo.AppServerWas64Bit.ToString(CultureInfo.InvariantCulture));
            }
            if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppWas64Bit)))
            {
                telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppWas64Bit), logEntryAppLevelConstantInfo.AppWas64Bit.ToString(CultureInfo.InvariantCulture));
            }

            if (telemetry is RequestTelemetry requestTelemetry && HttpContextAccessor.HttpContext != null)
            {
                List <AppInsightsLogKeyVal> logKeyValues;

                if (HttpContextAccessor.HttpContext.Items.TryGetValue("LogKeyValues", out object?logKeyValuesAsObj))
                {
                    logKeyValues = (List <AppInsightsLogKeyVal>)logKeyValuesAsObj;
                }
                else // in ok responses, we've no LogKeyValues because ApplicationInsightsLogStore won't gets called.
                {
                    using IDependencyResolver childResolver = DependencyManager.CreateChildDependencyResolver(); // HttpContextAccessor.HttpContext.RequestServices is null because scope is gets disposed at this time.

                    IRequestInformationProvider requestInformationProvider = childResolver.Resolve <IRequestInformationProvider>();
                    IUserInformationProvider    userInformationProvider    = childResolver.Resolve <IUserInformationProvider>();

                    logKeyValues = new List <AppInsightsLogKeyVal> {
                    };

                    if (userInformationProvider.IsAuthenticated())
                    {
                        logKeyValues.Add(new AppInsightsLogKeyVal {
                            Key = "UserId", Value = userInformationProvider.GetCurrentUserId()
                        });
                    }

                    logKeyValues.Add(new AppInsightsLogKeyVal {
                        Key = nameof(IRequestInformationProvider.UserAgent), Value = requestInformationProvider.UserAgent
                    });

                    logKeyValues.Add(new AppInsightsLogKeyVal {
                        Key = "X-Correlation-ID", Value = requestInformationProvider.XCorrelationId
                    });
                }

                AppInsightsLogKeyVal userAgent = logKeyValues.FirstOrDefault(ld => ld.Key == nameof(IRequestInformationProvider.UserAgent));

                if (userAgent != null)
                {
                    requestTelemetry.Context.User.UserAgent = userAgent.Value;
                }

                AppInsightsLogKeyVal userId = logKeyValues.FirstOrDefault(ld => ld.Key == "UserId");

                if (userId != null)
                {
                    requestTelemetry.Context.User.AccountId = requestTelemetry.Context.User.Id = requestTelemetry.Context.User.AuthenticatedUserId = requestTelemetry.Context.User.AuthenticatedUserId = userId.Value;
                }

                foreach (AppInsightsLogKeyVal keyVal in logKeyValues.OrderBy(kv => kv.Key))
                {
                    if (keyVal.Key == nameof(IRequestInformationProvider.UserAgent) || keyVal.Key == "UserId")
                    {
                        continue;
                    }
                    if (!requestTelemetry.Properties.ContainsKey(keyVal.Key))
                    {
                        requestTelemetry.Properties.Add(keyVal.Key, keyVal.Value);
                    }
                }

                AppInsightsLogKeyVal xCorrelationId = logKeyValues.FirstOrDefault(ld => ld.Key == "X-Correlation-ID");

                if (xCorrelationId != null)
                {
                    requestTelemetry.Id = xCorrelationId.Value;
                }
            }
        }
Esempio n. 13
0
        public static void LogRequest(ILogger logger, IRequestInformationProvider requestInformationProvider)
        {
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (requestInformationProvider == null)
            {
                throw new ArgumentNullException(nameof(requestInformationProvider));
            }

            logger.AddLogData(nameof(IRequestInformationProvider.HttpMethod), requestInformationProvider.HttpMethod);
            logger.AddLogData(nameof(IRequestInformationProvider.DisplayUrl), requestInformationProvider.DisplayUrl);

            if (requestInformationProvider.ClientAppVersion != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientAppVersion), requestInformationProvider.ClientAppVersion);
            }

            if (requestInformationProvider.SystemLanguage != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.SystemLanguage), requestInformationProvider.SystemLanguage);
            }

            if (requestInformationProvider.ClientCulture != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientCulture), requestInformationProvider.ClientCulture);
            }

            if (requestInformationProvider.ClientType != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientType), requestInformationProvider.ClientType);
            }

            if (requestInformationProvider.BitClientType != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.BitClientType), requestInformationProvider.BitClientType);
            }

            if (requestInformationProvider.ClientDateTime != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientDateTime), requestInformationProvider.ClientDateTime);
            }

            if (requestInformationProvider.ClientDebugMode != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientDebugMode), requestInformationProvider.ClientDebugMode);
            }

            if (requestInformationProvider.ClientScreenSize != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientScreenSize), requestInformationProvider.ClientScreenSize);
            }

            if (requestInformationProvider.ContentType != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ContentType), requestInformationProvider.ContentType);
            }

            if (requestInformationProvider.Origin != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.Origin), requestInformationProvider.Origin);
            }

            if (requestInformationProvider.Referer != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.Referer), requestInformationProvider.Referer);
            }

            logger.AddLogData(nameof(IRequestInformationProvider.ClientIp), requestInformationProvider.ClientIp);

            if (requestInformationProvider.ClientPlatform != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientPlatform), requestInformationProvider.ClientPlatform);
            }

            if (requestInformationProvider.ClientRoute != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientRoute), requestInformationProvider.ClientRoute);
            }

            if (requestInformationProvider.ClientSysLanguage != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientSysLanguage), requestInformationProvider.ClientSysLanguage);
            }

            if (requestInformationProvider.ClientTheme != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.ClientTheme), requestInformationProvider.ClientTheme);
            }

            if (requestInformationProvider.UserAgent != null)
            {
                logger.AddLogData(nameof(IRequestInformationProvider.UserAgent), requestInformationProvider.UserAgent);
            }

            logger.AddLogData("X-Correlation-ID", requestInformationProvider.XCorrelationId);
        }
Esempio n. 14
0
        public override async Task Invoke(IOwinContext context)
        {
            IDependencyResolver dependencyResolver = context.GetDependencyResolver();

            ILogger logger = dependencyResolver.Resolve<ILogger>();

            IRequestInformationProvider requestInformationProvider =
                dependencyResolver.Resolve<IRequestInformationProvider>();

            logger.AddLogData(nameof(IRequestInformationProvider.HttpMethod), requestInformationProvider.HttpMethod);
            logger.AddLogData(nameof(IRequestInformationProvider.RequestUri), requestInformationProvider.RequestUri);

            if (requestInformationProvider.ClientAppVersion != null)
                logger.AddLogData(nameof(IRequestInformationProvider.ClientAppVersion), requestInformationProvider.ClientAppVersion);

            if (requestInformationProvider.SystemLanguage != null)
                logger.AddLogData(nameof(IRequestInformationProvider.SystemLanguage), requestInformationProvider.SystemLanguage);

            if (requestInformationProvider.ClientCulture != null)
                logger.AddLogData(nameof(IRequestInformationProvider.ClientCulture), requestInformationProvider.ClientCulture);

            if (requestInformationProvider.ClientType != null)
                logger.AddLogData(nameof(IRequestInformationProvider.ClientType), requestInformationProvider.ClientType);

            if (requestInformationProvider.ClientDateTime != null)
                logger.AddLogData(nameof(IRequestInformationProvider.ClientDateTime), requestInformationProvider.ClientDateTime);

            if (requestInformationProvider.ClientDebugMode != null)
                logger.AddLogData(nameof(IRequestInformationProvider.ClientDebugMode), requestInformationProvider.ClientDebugMode);

            if (requestInformationProvider.ClientScreenSize != null)
                logger.AddLogData(nameof(IRequestInformationProvider.ClientScreenSize), requestInformationProvider.ClientScreenSize);

            if (requestInformationProvider.ContentType != null)
                logger.AddLogData(nameof(IRequestInformationProvider.ContentType), requestInformationProvider.ContentType);

            if (requestInformationProvider.Origin != null)
                logger.AddLogData(nameof(IRequestInformationProvider.Origin), requestInformationProvider.Origin);

            if (requestInformationProvider.Referer != null)
                logger.AddLogData(nameof(IRequestInformationProvider.Referer), requestInformationProvider.Referer);

            logger.AddLogData(nameof(IRequestInformationProvider.ClientIp), requestInformationProvider.ClientIp);

            if (requestInformationProvider.ClientPlatform != null)
                logger.AddLogData(nameof(IRequestInformationProvider.ClientPlatform), requestInformationProvider.ClientPlatform);

            if (requestInformationProvider.ClientRoute != null)
                logger.AddLogData(nameof(IRequestInformationProvider.ClientRoute), requestInformationProvider.ClientRoute);

            if (requestInformationProvider.ClientSysLanguage != null)
                logger.AddLogData(nameof(IRequestInformationProvider.ClientSysLanguage), requestInformationProvider.ClientSysLanguage);

            if (requestInformationProvider.ClientTheme != null)
                logger.AddLogData(nameof(IRequestInformationProvider.ClientTheme), requestInformationProvider.ClientTheme);

            if (requestInformationProvider.UserAgent != null)
                logger.AddLogData(nameof(IRequestInformationProvider.UserAgent), requestInformationProvider.UserAgent);

            if (requestInformationProvider.CorrelationId != null)
                logger.AddLogData(nameof(IRequestInformationProvider.CorrelationId), requestInformationProvider.CorrelationId);

            await Next.Invoke(context);
        }