public void RemoveKeysFromConnectionString() { DbConnectionStringBuilder dbConnectionStringBuilder = new DbConnectionStringBuilder { ConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;" }; dbConnectionStringBuilder.ConnectionString.Contains("myPassword").Should().BeTrue(); dbConnectionStringBuilder.RemoveKeys("Password"); dbConnectionStringBuilder.ConnectionString.Contains("myPassword").Should().BeFalse(); }
/// <summary> /// The strip password from connection string. /// </summary> /// <param name="connectionString"> /// The connection string. /// </param> /// <returns> /// The connection string with the password stripped out /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="connectionString"/> is <see langword="null"/> /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="connectionString"/> is an empty <see cref="string"/> or whitespace /> /// </exception> public static string StripPasswordFromConnectionString([NotNull] this string connectionString) { if (string.IsNullOrEmpty(connectionString)) { return(connectionString); } var db = new DbConnectionStringBuilder { ConnectionString = connectionString }; db.RemoveKeys("Password", "password", "Pwd", "pwd"); return(db.ToString()); }