简体   繁体   English

从 .NET 7 API 连接到 SQL 在 Docker 中运行的服务器时出现不受信任的证书错误 VS2022

[英]Untrusted Certificate Error when connecting to SQL Server running in Docker from .NET 7 API in VS2022

I have an API which I've upgraded from .NET Core 3.1 to .NET 7. It's now throwing an error when trying to connect to the database.我有一个 API,我已经从 .NET Core 3.1 升级到 .NET 7。它现在在尝试连接到数据库时抛出错误。

The error is:错误是:

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. (提供商:SSL 提供商,错误:0 - 证书链是由不受信任的机构颁发的。

I'm using VS2022, SQL Server (mcr.microsoft.com/mssql/server:2022-latest) is running in a Docker container.我正在使用 VS2022,SQL 服务器 (mcr.microsoft.com/mssql/server:2022-latest) 在 Docker 容器中运行。

I'm using Docker Compose:我正在使用 Docker 撰写:

version: '3.9'

services:
  sql-server-db:
    image: "mcr.microsoft.com/mssql/server:2022-latest"
    container_name: sql-server-db
    environment:
      MSSQL_SA_PASSWORD: "P@ssw0rd"
      ACCEPT_EULA: "Y"
    ports:
      - "1433:1433"
    volumes: 
      - sql1:/var/opt/mssql

volumes:
  sql1:
    external: false

The connection string is连接字符串是

server=127.0.0.1,1433;Initial Catalog=xxx;user id=sa;password=P@ssw0rd;Encrypt=False;TrustServerCertificate=True

From the posts I've read, either Encrypt=False or TrustServerCertificate=True should fix this issue, but neither, or indeed, both have helped.从我读过的帖子来看, Encrypt=FalseTrustServerCertificate=True应该可以解决这个问题,但两者都没有帮助,或者实际上两者都没有帮助。

I have confirmed that the SQL Server instance is running ok, I can connect using SSMS, using the username and password from the connection string.我已经确认 SQL 服务器实例运行正常,我可以使用 SSMS 连接,使用连接字符串中的用户名和密码。

Further, I can confirm that it's working with .NET 6, so it is definitely an issue with .NET 7 & EntityFrameworkCore 7.此外,我可以确认它正在与 .NET 6 一起使用,因此这绝对是 .NET 7 和 EntityFrameworkCore 7 的问题。

I only added "Trust Server Certificate=True" to my connection string after I updated my app.我只在更新我的应用程序后将“Trust Server Certificate=True”添加到我的连接字符串中。 Try to remove "Encrypt=False".尝试删除“Encrypt=False”。

As I previously mentioned on your Mastodon post, that's a catch-all error message for several issues.正如我之前在您的 Mastodon 帖子中提到的,这是针对多个问题的包罗万象的错误消息。

I solved it by using the standard volume for persistence, another person mentioned on a GitHub issue that I saw that they solved it by applying the password requirements (I think they are at least 8 characters with at least one lowercase, at least one uppercase, at least one number and at least one symbol).我通过使用持久性标准卷解决了这个问题,另一个人在 GitHub 问题中提到,我看到他们通过应用密码要求解决了这个问题(我认为它们至少有 8 个字符,至少有一个小写字母,至少一个大写字母,至少一个数字和至少一个符号)。

I have managed to fix the issue.我已经设法解决了这个问题。

I changed my repositories to inject a IDbContextFactory<> and create a DbContext, rather than inject a DbContext object directly.我更改了存储库以注入 IDbContextFactory<> 并创建 DbContext,而不是直接注入 DbContext object。

    public BookRepository(IDbContextFactory<SampleAPIContext> dbContextFactory, IRestToLinqParser<Book> parser, ILogger<BookRepository> logger) : base(dbContextFactory, parser, logger)
    {
    }

and changed my Startup.cs to add the IDbContextFactory rather than the DBContext并更改我的 Startup.cs 以添加 IDbContextFactory 而不是 DBContext

        services.AddDbContextFactory<SampleAPIContext>(
            options => SqlServerDbContextOptionsExtensions.UseSqlServer(options, connectionString)
        );

I'm not entirely sure why this made the difference, but it did.我不完全确定为什么这会有所不同,但确实如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net c# 代码(从 VS2022 和 IIS10)在本地机器上工作以连接到 SQL Server DB 它在 Web 服务器上不起作用 - asp.net c# code works (from VS2022 and on IIS10) on local machine to connect to SQL Server DB it does not work on Webserver .NET 6.0 web 应用程序在 VS2022 中调试非常慢 - .NET 6.0 web app very slow debugging in VS2022 VS2022上的水晶报表 - Crystal Report on VS2022 VS2022中如何添加WebForm文件? - How to add a WebForm file in VS2022? VS2022 社区 - Vanilla ASP/C#.Net 在 IISExpress 中使用托管调试助手“FatalExecutionEngineError”立即崩溃 - VS2022 Community - Vanilla ASP/C#.Net crashes immeditely in IISExpress with Managed Debugging Assistant 'FatalExecutionEngineError' SSL_ERROR_SYSCALL 连接到 Docker 容器中的 ASP.NET 6 web 服务器时 - SSL_ERROR_SYSCALL when connecting to ASP.NET 6 web server in Docker container 从Web API连接到SQL Server时出现问题 - Issue connecting to the SQL server from Web API VS2022 VueJs 独立模板 TS 错误 - VS2022 VueJs Standalone Template TS Errors 类型 Universe 无法解析 vs2022 中的程序集打开项目 - type universe cannot resolve assembly opening project in vs2022 将Web API连接到SQL Server - Connecting Web API to SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM