Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The certificate chain was issued by an authority that is not trusted

I studied the help here on upgrading to aspnetcore 2.1.0

My database is SQLExpress 2016SP1

I am able to add a migration but when I issue

update-database

at the Package Manager Console, I get an error

A connection was successfully established with the server, but then an error occurred during the login process.
(provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)

The connection string is of the form

   Server="Server=myserver;Initial Catalog=mydatabase;Trusted_Connection=True;MultipleActiveResultSets=true

The DbContext is

public class ApiDbContext : IdentityDbContext<ApplicationUser>
{
    public ApiDbContext(DbContextOptions<ApiDbContext> options)
        : base(options)
    {
    }
}

The Context Factory is

public class MyContextFactory : IDesignTimeDbContextFactory<ApiDbContext>
{
    public ApiDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ApiDbContext>();
        var builder = new ConfigurationBuilder();
        builder.AddJsonFile("appsettings.json");
        var config = builder.Build();
        var connectionString = config.GetConnectionString("MyDatabase");
        optionsBuilder.UseSqlServer(connectionString);
        return new ApiDbContext(optionsBuilder.Options);
    }
}

[Update]

If I hard code the connection string inside the implementation of IDesignTimeDbContextFactory then I can run the migrations

public class MyContextFactory : IDesignTimeDbContextFactory<ApiDbContext>
{
    public ApiDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ApiDbContext>();
        var connectionString =    "Server=myserver;Initial Catalog=mydatabase;Trusted_Connection=True;MultipleActiveResultSets=true";
        optionsBuilder.UseSqlServer(connectionString);
        return new ApiDbContext(optionsBuilder.Options);
    }
}

Hard coding the connection string is not desirable so I would like a better answer.

I am unclear as to why the implementation of IDesignTimeDbContextFactory is needed. (my migration does fail without it )

I have updated my Startup.cs and Progam.cs to match those of a newly generated program where the implementation is not needed.

 public class Program
 {
   public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApiDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("MyDatabase")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApiDbContext>();

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();

        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseAuthentication();
        app.UseMvc();
    }
}

[Update]

I updated to VS2017 version 15.7.3. When I went to repeat the problem and create the first migration, PM displayed the message

The configuration file 'appsettings.json' was not found and is not optional

When I marked this file copy always then both the add-migration and the update-database worked.

like image 893
Kirsten Avatar asked Jun 09 '18 04:06

Kirsten


People also ask

How are certificate chains verified?

The certificate signature is verified using the public key in the issuer's certificate. The validity period for the certificate is verified against the current time provided by the verifier's system clock. If the issuer's certificate is trusted by the verifier, verification is successful and stops here.

How do I disable Force Encryption in SQL Server?

From SQL Server Configuration Manager, under SQL Server Network Configuration, right-click Protocols for <server instance>, and then select Properties. I remove the certificate and on Flags tab, in the ForceEncryption box, I select NO, and then OK to close the dialog box.

What is trust server certificate in SQL Server?

A trusted SSL certificate validates the SQL Server instance when the client application requests encrypted connection (or vice versa), while the SQL Server must be configured to follow the certificate authority (CA). This means that a certificate must be “signed” by a trusted source.


1 Answers

Try adding TrustServerCertificate=True to the connection string, as per This StackOverflow post

like image 172
Will Jones Avatar answered Sep 30 '22 01:09

Will Jones