首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >证书链是由不受信任的颁发机构颁发的

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

Stack Overflow用户
提问于 2018-06-09 12:15:55
回答 2查看 15.4K关注 0票数 6

我研究过the help here on upgrading to aspnetcore 2.1.0

我的数据库是SQLExpress 2016SP1

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

代码语言:javascript
复制
update-database

在Package Manager控制台上,我收到一个错误

成功建立了与服务器的连接,但随后在登录过程中发生错误。

(提供程序: SSL提供程序,错误:0-证书链由不受信任的颁发机构颁发。)

连接字符串的格式为

代码语言:javascript
复制
   Server="Server=myserver;Initial Catalog=mydatabase;Trusted_Connection=True;MultipleActiveResultSets=true

DbContext

代码语言:javascript
复制
public class ApiDbContext : IdentityDbContext<ApplicationUser>
{
    public ApiDbContext(DbContextOptions<ApiDbContext> options)
        : base(options)
    {
    }
}

上下文工厂是

代码语言:javascript
复制
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的实现中硬编码连接字符串,那么我就可以运行迁移

代码语言:javascript
复制
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,以匹配不需要实现的新生成的程序。

代码语言:javascript
复制
 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);}

代码语言:javascript
复制
    // 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();
    }
}

更新

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

代码语言:javascript
复制
The configuration file 'appsettings.json' was not found and is not optional

当我将这个文件标记为总是复制时,add-migration和update-database都工作了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-12 00:35:55

尝试按照This StackOverflow post向连接字符串添加TrustServerCertificate=True

票数 8
EN

Stack Overflow用户

发布于 2019-02-27 04:12:16

设置"TrustServerCertificate=True“并使用Windows Authentication为我解决了这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50770862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档