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

2guxujil  于 2022-12-03  发布在  其他
关注(0)|答案(2)|浏览(487)

我研究了the help here on upgrading to aspnetcore 2.1.0
我的数据库是SQLExpress 2016 SP1
我可以添加迁移,但当我发出

update-database

在软件包管理器控制台中,我收到一个错误
已成功建立与服务器的连接,但在登录过程中发生错误。
(提供程序:SSL提供程序,错误:0 -证书链由不受信任的颁发机构颁发。)
连接字符串的格式为

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

DbContext

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

上下文工厂是

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);
    }
}

【更新】
如果我在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);
    }
}

硬编码连接字符串是不可取的,所以我想一个更好的答案。
我不清楚为什么需要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>();

().设置兼容版本(兼容版本.版本_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();
    }
}

【更新】
我更新到VS 2017版本15.7.3。当我重复该问题并创建第一个迁移时,PM显示消息

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

当我标记这个文件副本总是然后添加迁移和更新数据库工作。

wh6knrhe

wh6knrhe1#

尝试按照This StackOverflow postTrustServerCertificate=True添加到连接字符串

2022更新:

链接的帖子已经更新,提供了更多最新的建议。这里发布的答案是权宜之计,不建议用于生产工作负载

ruarlubt

ruarlubt2#

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

相关问题