[英]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=False
或TrustServerCertificate=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.