配置 .pfx 证书

问题描述

我正在使用 .Net 5.0,当我将它扔到托管服务器时出现这些错误。过了一会儿,由于这些错误,我的网站给出了 HTTP 错误 500。我使用 OpenSSL 创建了证书,并且用户配置文件为 true,但是当我尝试添加证书时,我收到了这些错误

我该怎么办?

  warn: Microsoft.AspNetCore.DataProtection.Repositories.EphemeralXmlRepository[50]
          Using an in-memory repository. Keys will not be persisted to storage.
    warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[59]
          Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits.
    warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
          No XML encryptor configured. Key {f071590d-e902-4b6f-bbbe-d27d7415d96b} may be persisted to storage in unencrypted form.
    crit: Microsoft.AspNetCore.Hosting.Diagnostics[6]
          Application startup exception
          Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: The system cannot find the file specified.
        

我的创业是这样的:

services.AddDataProtection()
            .SetApplicationName("MyProjectName")
            .ProtectKeysWithCertificate(new X509Certificate2(certificate,"password",X509KeyStorageFlags.MachineKeySet
                             | X509KeyStorageFlags.PersistKeySet
                             | X509KeyStorageFlags.Exportable))  //My bad line of code
                   .UseCryptographicAlgorithms(
                    new AuthenticatedEncryptorConfiguration()
                    {
                        EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
                    }
             )
             .PersistKeysToFileSystem(new DirectoryInfo(keysFolder)) //shared network folder for key location
             .SetDefaultKeyLifetime(TimeSpan.FromDays(600));

解决方法

问题是服务器端过时了。提供者更新服务器,问题解决