public override void Initialize(string name, NameValueCollection config) { if (config == null) { throw new ArgumentNullException("config"); } if (String.IsNullOrEmpty(name)) { name = DEFAULT_NAME; } if (String.IsNullOrEmpty(config["description"])) { config.Remove("description"); config.Add("description", Resources.RoleProvider_description); } base.Initialize(name, config); _applicationName = config["applicationName"] ?? System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath; _invalidUsernameCharacters = Helper.GetConfigValue(config["invalidUsernameCharacters"], DEFAULT_INVALID_CHARACTERS); _invalidRoleCharacters = Helper.GetConfigValue(config["invalidRoleCharacters"], DEFAULT_INVALID_CHARACTERS); RoleCollectionSuffix = Helper.GetConfigValue(config["roleCollectionSuffix"], DEFAULT_ROLE_COLLECTION_SUFFIX); UserCollectionSuffix = Helper.GetConfigValue(config["userCollectionSuffix"], DEFAULT_USER_COLLECTION_SUFFIX); // Initialize Connection String var temp = config["connectionStringName"]; if (String.IsNullOrWhiteSpace(temp)) { throw new ProviderException(Resources.Connection_name_not_specified); } var connectionStringSettings = ConfigurationManager.ConnectionStrings[temp]; if (null == connectionStringSettings || String.IsNullOrWhiteSpace(connectionStringSettings.ConnectionString)) { throw new ProviderException(String.Format(Resources.Connection_string_not_found, temp)); } ConnectionString = connectionStringSettings.ConnectionString; // Check for unrecognized config values config.Remove("applicationName"); config.Remove("connectionStringName"); config.Remove("invalidUsernameCharacters"); config.Remove("invalidRoleCharacters"); config.Remove("roleCollectionSuffix"); config.Remove("userCollectionSuffix"); if (config.Count > 0) { var key = config.GetKey(0); if (!String.IsNullOrEmpty(key)) { throw new ProviderException(String.Format(Resources.Provider_unrecognized_attribute, key)); } } // Initialize MongoDB Server Database = ClientMongo.GetMongoConnection(ConnectionString); _userCollectionName = Helper.GenerateCollectionName(_applicationName, UserCollectionSuffix); _roleCollectionName = Helper.GenerateCollectionName(_applicationName, RoleCollectionSuffix); // store element names var names = new RoleMembershipElements { LowercaseUsername = Helper.GetElementNameFor <User>(p => p.LowercaseUsername), Roles = Helper.GetElementNameFor <User>(p => p.Roles) }; ElementNames = names; // ensure indexes // MongoDB automatically indexes the _id field //this.UserCollection.Indexes.CreateOne(Builders<User>.IndexKeys.Ascending(u=>u.LowercaseUsername)); //this.UserCollection.Indexes.CreateOne(Builders<User>.IndexKeys.Ascending(u=>u.Roles)); }
public override void Initialize(string name, NameValueCollection config) { if (config == null) throw new ArgumentNullException("config"); if (String.IsNullOrEmpty(name)) name = DEFAULT_NAME; if (String.IsNullOrEmpty(config["description"])) { config.Remove("description"); config.Add("description", Resources.RoleProvider_description); } base.Initialize(name, config); _applicationName = config["applicationName"] ?? System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath; _invalidUsernameCharacters = Helper.GetConfigValue(config["invalidUsernameCharacters"], DEFAULT_INVALID_CHARACTERS); _invalidRoleCharacters = Helper.GetConfigValue(config["invalidRoleCharacters"], DEFAULT_INVALID_CHARACTERS); _roleCollectionSuffix = Helper.GetConfigValue(config["roleCollectionSuffix"], DEFAULT_ROLE_COLLECTION_SUFFIX); _userCollectionSuffix = Helper.GetConfigValue(config["userCollectionSuffix"], DEFAULT_USER_COLLECTION_SUFFIX); // Initialize Connection String string temp = config["connectionStringName"]; if (String.IsNullOrWhiteSpace(temp)) throw new ProviderException(Resources.Connection_name_not_specified); ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings[temp]; if (null == ConnectionStringSettings || String.IsNullOrWhiteSpace(ConnectionStringSettings.ConnectionString)) throw new ProviderException(String.Format(Resources.Connection_string_not_found, temp)); _connectionString = ConnectionStringSettings.ConnectionString; // Check for unrecognized config values config.Remove("applicationName"); config.Remove("connectionStringName"); config.Remove("invalidUsernameCharacters"); config.Remove("invalidRoleCharacters"); config.Remove("roleCollectionSuffix"); config.Remove("userCollectionSuffix"); if (config.Count > 0) { string key = config.GetKey(0); if (!String.IsNullOrEmpty(key)) throw new ProviderException(String.Format(Resources.Provider_unrecognized_attribute, key)); } // Initialize MongoDB Server UserClassMap.Register(); _db = MongoDatabase.Create(_connectionString); _userCollectionName = Helper.GenerateCollectionName(_applicationName, _userCollectionSuffix); _roleCollectionName = Helper.GenerateCollectionName(_applicationName, _roleCollectionSuffix); // store element names var names = new RoleMembershipElements(); names.LowercaseUsername = Helper.GetElementNameFor<User>(p => p.LowercaseUsername); names.Roles = Helper.GetElementNameFor<User>(p => p.Roles); ElementNames = names; // ensure indexes // MongoDB automatically indexes the _id field this.UserCollection.EnsureIndex(ElementNames.LowercaseUsername); this.UserCollection.EnsureIndex(ElementNames.Roles); }