无论我尝试什么,在新的 .net 5 API 项目中我都无法连接到 RavenDB。它给我一个错误:
This server requires client certificate for authentication, but none was provided by the client.
我连接数据库的方式:
byte[] certificateBytes = _certificateProvider.GetCertificate("CERT-NAME").Result;
string passphrase = _secretProvider.GetSecret("CERT-PASSPHRASE").Result;
X509Certificate2 certificate = new X509Certificate2(certificateBytes, passphrase);
var documentStore = new DocumentStore()
{
Urls = new[] { databaseOptions.DbUrl },
Conventions =
{
UseOptimisticConcurrency = true,
FindCollectionName = findCollectionName
},
Database = "DBNAME",
Certificate = certificate,
}.Initialize();
证书提供者和 secret 提供者从 Azure Key Vault 中获取数据。我验证了 X509Certificate,它与我在 RavenDB 的管理面板中获得的指纹相同。这样就正确加载了。此外,证书对所请求的数据库具有读/写权限
但是当我执行以下操作时:
using (IAsyncDocumentSession session = documentStore.OpenAsyncSession())
{
var entity = await session.Query<EntityDTO>()
.SingleOrDefaultAsync();
}
然后我得到以下错误:
This server requires client certificate for authentication, but none was provided by the client.
这是在初始化文档存储时给出的证书。任何人都知道如何继续这个,因为 RavenCloud 没有提供比这更多的信息吗?
在另一个项目(.net core 3.1)中,具有相同证书的相同代码有效。但在 .net 5 的发行说明中找不到任何可能导致此问题的内容。
最佳答案
您需要加载带有 MachineKeySet 标志的证书。
X509Certificate2 certificate = new X509Certificate2(certificateBytes, passphrase, X509KeyStorageFlags.MachineKeySet);
这将解决 Azure 中的问题,但您的本地开发机器在此更改后可能会出现问题。您需要允许运行 RavenDB 的用户完全访问本地计算机上的 MachineKeySet 文件夹。
“C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys”
另请注意,如果您之后不处理证书,私钥会堆积在该文件夹中(每次 X509Certificate2 构造函数打开证书时都会将一个 key 写入该文件夹)。
关于C# 连接到 Ravendb 一直失败并出现错误 : This server requires client certificate for authentication, 但客户端未提供任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65517894/