证书链由不受信任的机构颁发

Kir*_*eed 5 sql-server asp.net-identity entity-framework-core asp.net-core

我研究了这里关于升级到 aspnetcore 2.1.0 的帮助

我的数据库是 SQLExpress 2016SP1

我可以添加迁移,但是当我发出

update-database
Run Code Online (Sandbox Code Playgroud)

在包管理器控制台,我收到一个错误

已成功与服务器建立连接,但随后在登录过程中出现错误。
(提供者:SSL 提供者,错误:0 - 证书链由不受信任的机构颁发。)

连接字符串的格式为

   Server="Server=myserver;Initial Catalog=mydatabase;Trusted_Connection=True;MultipleActiveResultSets=true
Run Code Online (Sandbox Code Playgroud)

DbContext

public class ApiDbContext : IdentityDbContext<ApplicationUser>
{
    public ApiDbContext(DbContextOptions<ApiDbContext> options)
        : base(options)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

上下文工厂是

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

[更新]

如果我在 IDesignTimeDbContextFactory 的实现中对连接字符串进行硬编码,那么我可以运行迁移

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

对连接字符串进行硬编码是不可取的,所以我想要一个更好的答案。

我不清楚为什么需要 IDesignTimeDbContextFactory 的实现。(没有它,我的迁移确实会失败)

我已经更新了我的 Startup.cs 和 Progam.cs 以匹配那些不需要实现的新生成的程序。

 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>();
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

[更新]

我更新到 VS2017 版本 15.7.3。当我去重复这个问题并创建第一个迁移时,PM 显示了消息

The configuration file 'appsettings.json' was not found and is not optional
Run Code Online (Sandbox Code Playgroud)

当我始终标记此文件副本时,add-migration 和 update-database 都可以工作。

Dan*_*111 8

设置“TrustServerCertificate=True”并使用 Windows 身份验证为我解决了这个问题。


Wil*_*nes 6

尝试TrustServerCertificate=True根据此 StackOverflow 帖子添加到连接字符串

  • 还添加 Encrypt=False; 为我工作 (2认同)