/// <summary> /// Gets the mongo connection. /// </summary> /// <param name="connectionString">The connection string.</param> /// <returns></returns> internal static MongoDatabase GetMongoConnection(String connectionString) { UserClassMap.Register(); var mongoUrl = MongoUrl.Create(connectionString); var server = new MongoClient(connectionString).GetServer(); return(server.GetDatabase(mongoUrl.DatabaseName)); }
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); }