Пример #1
0
        /// <summary>
        /// UseManagedIdentity for this SqlConnection specifiying an optional tenantID taken from https://github.com/juunas11/Joonasw.ManagedIdentityDemos
        /// </summary>
        /// <param name="optionsBuilder"></param>
        /// <param name="tenantID"></param>
        /// <returns></returns>
        public static DbContextOptionsBuilder UseManagedIdentity([NotNull] this DbContextOptionsBuilder optionsBuilder, string?tenantID = null)
        {
            var sqlServerOptionsExtension = optionsBuilder.Options.FindExtension <SqlServerOptionsExtension>();

            if (sqlServerOptionsExtension == null)
            {
                throw new NotImplementedException("UseManagedIdentity is only implemented for SQL Server");
            }
            var connectionString = sqlServerOptionsExtension.ConnectionString;

            if (connectionString.Contains("user id=", StringComparison.OrdinalIgnoreCase) || connectionString.Contains("Integrated Security=", StringComparison.OrdinalIgnoreCase) || connectionString.Contains("user id =", StringComparison.OrdinalIgnoreCase) || connectionString.Contains("Integrated Security =", StringComparison.OrdinalIgnoreCase))
            {
                return(optionsBuilder);                                                                                                                                                                                                                                                                                                                                            //no-op if connectionString contains user id or integrated security
            }
            optionsBuilder.AddInterceptors(new ManagedIdentityConnectionInterceptor(tenantID));
            return(optionsBuilder);
        }
 /// <summary>
 /// Add <see cref="EFInterceptor"/> to <see cref="DbContextOptionsBuilder"/>.
 /// </summary>
 /// <param name="dbContextOptionsBuilder">Instance of <see cref="DbContextOptionsBuilder"/>.</param>
 /// <param name="collectSqlQueries">Set this parameter to true to capture sql query text. The value set here overrides the value of CollectSqlQueries in appsettings.json if present. The default value of this parameter is null.</param>
 /// <returns>Instance of <see cref="DbContextOptionsBuilder"/>.</returns>
 public static DbContextOptionsBuilder AddXRayInterceptor(this DbContextOptionsBuilder dbContextOptionsBuilder, bool?collectSqlQueries = null)
 {
     return(dbContextOptionsBuilder.AddInterceptors(new EFInterceptor(collectSqlQueries)));
 }