Ejemplo n.º 1
0
        /// <summary>
        /// Configures a Microsoft SQL Server Storage implementation of <see cref="IWebHookStore"/>
        /// which provides a persistent store for registered WebHooks used by the custom WebHooks module.
        /// </summary>
        /// <param name="config">The current <see cref="HttpConfiguration"/>config.</param>
        /// <param name="encryptData">Indicates whether the data should be encrypted using <see cref="IDataProtector"/> while persisted.</param>
        public static void InitializeCustomWebHooksSqlStorage(this HttpConfiguration config, bool encryptData)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            WebHooksConfig.Initialize(config);

            ILogger logger = config.DependencyResolver.GetLogger();
            SettingsDictionary settings = config.DependencyResolver.GetSettings();

            // We explicitly set the DB initializer to null to avoid that an existing DB is initialized wrongly.
            Database.SetInitializer<WebHookStoreContext>(null);

            IWebHookStore store;
            if (encryptData)
            {
                IDataProtector protector = DataSecurity.GetDataProtector();
                store = new SqlWebHookStore(settings, protector, logger);
            }
            else
            {
                store = new SqlWebHookStore(settings, logger);
            }
            CustomServices.SetStore(store);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Provides a static method for creating a standalone <see cref="SqlWebHookStore"/> instance.
        /// </summary>
        /// <param name="logger">The <see cref="ILogger"/> instance to use.</param>
        /// <returns>An initialized <see cref="SqlWebHookStore"/> instance.</returns>
        public static IWebHookStore CreateStore(ILogger logger)
        {
            SettingsDictionary settings  = CommonServices.GetSettings();
            IDataProtector     protector = DataSecurity.GetDataProtector();
            IWebHookStore      store     = new SqlWebHookStore(settings, protector, logger);

            return(store);
        }
Ejemplo n.º 3
0
        public void CreateStore_Succeeds()
        {
            // Arrange
            ILogger logger = new Mock <ILogger>().Object;

            // Act
            IWebHookStore actual = SqlWebHookStore.CreateStore(logger, encryptData: false);

            // Assert
            Assert.IsType <SqlWebHookStore>(actual);
        }
Ejemplo n.º 4
0
        public void CreateStore_WithCustomSettings_Succeeds()
        {
            // Arrange
            ILogger logger = new Mock <ILogger>().Object;

            // Act
            IWebHookStore actual = SqlWebHookStore.CreateStore(logger, true, WebHookStoreContext.ConnectionStringName, "WebHooks", "WebHooks");

            // Assert
            Assert.IsType <SqlWebHookStore>(actual);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Provides a static method for creating a standalone <see cref="SqlWebHookStore"/> instance.
        /// </summary>
        /// <param name="logger">The <see cref="ILogger"/> instance to use.</param>
        /// <param name="encryptData">Indicates whether the data should be encrypted using <see cref="IDataProtector"/> while persisted.</param>
        /// <returns>An initialized <see cref="SqlWebHookStore"/> instance.</returns>
        public static IWebHookStore CreateStore(ILogger logger, bool encryptData)
        {
            SettingsDictionary settings = CommonServices.GetSettings();
            IWebHookStore      store;

            if (encryptData)
            {
                IDataProtector protector = DataSecurity.GetDataProtector();
                store = new SqlWebHookStore(settings, protector, logger);
            }
            else
            {
                store = new SqlWebHookStore(settings, logger);
            }
            return(store);
        }
        public IWebHookStore GetWebHookStore()
        {
            if (_webHookStore == null)
            {
                var dataSettings = _configManagerHelper.DataSettings;
                Microsoft.AspNet.WebHooks.Config.SettingsDictionary settings = new Microsoft.AspNet.WebHooks.Config.SettingsDictionary();
                settings.Add("MS_SqlStoreConnectionString", dataSettings.DataConnectionString);
                settings.Connections.Add("MS_SqlStoreConnectionString", new Microsoft.AspNet.WebHooks.Config.ConnectionSettings("MS_SqlStoreConnectionString", dataSettings.DataConnectionString));

                Microsoft.AspNet.WebHooks.IWebHookStore store = new Microsoft.AspNet.WebHooks.SqlWebHookStore(settings, _logger);

                Microsoft.AspNet.WebHooks.Services.CustomServices.SetStore(store);

                _webHookStore = CustomServices.GetStore();
            }

            return(_webHookStore);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Configures a Microsoft SQL Server Storage implementation of <see cref="IWebHookStore"/>
        /// which provides a persistent store for registered WebHooks used by the custom WebHooks module.
        /// </summary>
        /// <param name="config">The current <see cref="HttpConfiguration"/>config.</param>
        public static void InitializeCustomWebHooksSqlStorage(this HttpConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            WebHooksConfig.Initialize(config);

            ILogger logger = config.DependencyResolver.GetLogger();
            SettingsDictionary settings = config.DependencyResolver.GetSettings();

            // We explicitly set the DB initializer to null to avoid that an existing DB is initialized wrongly.
            Database.SetInitializer<WebHookStoreContext>(null);

            IDataProtectionProvider provider = GetDataProtectionProvider();
            IDataProtector protector = provider.CreateProtector(Purpose);

            IWebHookStore store = new SqlWebHookStore(settings, protector, logger);
            CustomServices.SetStore(store);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Provides a static method for creating a standalone <see cref="SqlWebHookStore"/> instance.
        /// </summary>
        /// <param name="logger">The <see cref="ILogger"/> instance to use.</param>
        /// <param name="encryptData">Indicates whether the data should be encrypted using <see cref="IDataProtector"/> while persisted.</param>
        /// <param name="nameOrConnectionString">The custom connection string or name of the connection string application setting. Used to initialize <see cref="WebHookStoreContext"/>.</param>
        /// <param name="schemaName">The custom name of database schema. Used to initialize <see cref="WebHookStoreContext"/>.</param>
        /// <param name="tableName">The custom name of database table. Used to initialize <see cref="WebHookStoreContext"/>.</param>
        /// <returns>An initialized <see cref="SqlWebHookStore"/> instance.</returns>
        public static IWebHookStore CreateStore(
            ILogger logger,
            bool encryptData,
            string nameOrConnectionString,
            string schemaName,
            string tableName)
        {
            var           settings = CommonServices.GetSettings();
            IWebHookStore store;

            if (encryptData)
            {
                var protector = DataSecurity.GetDataProtector();
                store = new SqlWebHookStore(settings, protector, logger, nameOrConnectionString, schemaName, tableName);
            }
            else
            {
                store = new SqlWebHookStore(settings, logger, nameOrConnectionString, schemaName, tableName);
            }
            return(store);
        }
Ejemplo n.º 9
0
        public void CheckSqlStorageConnectionString_Throws_IfNullOrEmptyConnectionString(ConnectionSettings connectionSettings)
        {
            // Arrange
            SettingsDictionary settings = new SettingsDictionary();

            settings.Connections.Add(WebHookStoreContext.ConnectionStringName, connectionSettings);

            // Act
            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => SqlWebHookStore.CheckSqlStorageConnectionString(settings));

            // Assert
            Assert.Equal("Please provide a SQL connection string with name 'MS_SqlStoreConnectionString' in the configuration string section of the 'Web.Config' file.", ex.Message);
        }