diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs index ff077296c..b3c260335 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs @@ -28,4 +28,4 @@ public DbConnectionSetting() { Slavers = new string[0]; } -} +} \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 506477760..8cc127bdf 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -1,3 +1,5 @@ +using System.Web; + namespace BotSharp.Plugin.MongoStorage; public class MongoDbContext @@ -6,6 +8,8 @@ public class MongoDbContext private readonly string _mongoDbDatabaseName; private readonly string _collectionPrefix; + private const string DB_NAME_INDEX = "authSource"; + public MongoDbContext(BotSharpDatabaseSettings dbSettings) { var mongoDbConnectionString = dbSettings.BotSharpMongoDb; @@ -16,14 +20,27 @@ public MongoDbContext(BotSharpDatabaseSettings dbSettings) private string GetDatabaseName(string mongoDbConnectionString) { - var databaseName = mongoDbConnectionString.Substring(mongoDbConnectionString.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1); - if (databaseName.Contains("?")) + var dbName = string.Empty; + if (!Uri.TryCreate(mongoDbConnectionString, UriKind.Absolute, out var conn)) { - databaseName = databaseName.Substring(0, databaseName.IndexOf("?", StringComparison.InvariantCultureIgnoreCase)); + return dbName; } - return databaseName; + + var query = HttpUtility.ParseQueryString(conn.Query); + var keys = query.AllKeys ?? []; + + if (keys.Contains(DB_NAME_INDEX)) + { + dbName = query[DB_NAME_INDEX]; + } + else + { + dbName = conn.Segments?.FirstOrDefault(x => x != "/") ?? string.Empty; + } + return dbName; } + private IMongoDatabase Database { get { return _mongoClient.GetDatabase(_mongoDbDatabaseName); } } #region Indexes