/// <summary> /// MongoDb Configuration /// </summary> /// <returns></returns> public static MongoClient MongoClientConfiguration(IEnumerable<Uri> servers, string userName, string password, string credentialDataBase, MongoCredentialType type) { var configuration = new MongoClientSettings { MaxConnectionPoolSize = 10, MinConnectionPoolSize = 1, WaitQueueSize = 10000, Servers = servers.Select(uri => new MongoServerAddress(uri.Host, uri.Port)) }; if (!string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(password)) configuration.Credentials = GetMongoCredential(userName, password, credentialDataBase, type); return new MongoClient(configuration); }
public static IEnumerable<MongoCredential> GetMongoCredential(string userName, string password, string credentialDataBase, MongoCredentialType type) { if (string.IsNullOrEmpty(credentialDataBase)) credentialDataBase = "admin"; return new List<MongoCredential> { MongoCredentialType.Plain == type ? MongoCredential.CreatePlainCredential(credentialDataBase, userName, password) : MongoCredentialType.ScramSha1 == type ? MongoCredential.CreateScramSha1Credential(credentialDataBase, userName, password) : MongoCredential.CreateMongoCRCredential(credentialDataBase, userName, password) }; }