c# - IIS 中的 RSA 容器返回 "Object already exists"

标签 c# asp.net-core iis rsa

当我将我的工作 ASP.NET CORE 2.2 应用程序部署到我的本地 IIS 10 时,它给了我异常

Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Object already exists
   at Internal.NativeCrypto.CapiHelper.CreateCSP(CspParameters parameters, Boolean randomKeyContainer, SafeProvHandle& safeProvHandle)
   at Internal.NativeCrypto.CapiHelper.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
   at System.Security.Cryptography.RSACryptoServiceProvider.get_SafeProvHandle()
   at System.Security.Cryptography.RSACryptoServiceProvider.get_SafeKeyHandle()
   at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 keySize, CspParameters parameters, Boolean useDefaultKeySize)
   at System.Security.Cryptography.RSACryptoServiceProvider..ctor(CspParameters parameters)
   at SamoletBot.Utilities.RSAHelper.GetRSAFromString(String pemstr) in D:\Projects\SamoletBot22\SamoletBot\SamoletBot.Utilities\RSAHelper.cs:line 23

这是相关的代码:
   CspParameters cspParameters = new CspParameters();
   cspParameters.KeyContainerName = "TheContainer";
   cspParameters.Flags = CspProviderFlags.UseMachineKeyStore;

   RSACryptoServiceProvider rsaKey;
   rsaKey = new RSACryptoServiceProvider(cspParameters);

最后一行抛出异常

阅读后我得出结论,这是由于 RSA 容器权限而发生的,我看到了几个使用它来授予每个用户权限的答案。
CryptoKeyAccessRule rule = new CryptoKeyAccessRule("everyone", CryptoKeyRights.FullControl, AccessControlType.Allow);

问题是CryptoKeyAccessRule找不到。我已导入 using System.Security.AccessControl .看着 Microsoft docs我看到这仅适用于 .NET Framework。

在 .NET Core 2.2 中创建“共享”RSA 容器的替代方法是什么?

最佳答案

我不确定这会满足您的要求。但以上库不包含在 .net 标准/核心 api 中。
您可能需要切换到下面并执行解决方法
https://www.nuget.org/packages/System.IO.FileSystem.AccessControl/

System.IO.FileSystem.AccessControl

关于c# - IIS 中的 RSA 容器返回 "Object already exists",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57607272/

相关文章:

c# - 获取插入行的ID cassandra .Net

c# - 响应消息的内容类型 application/xml;charset=utf-8 与绑定(bind)的内容类型不匹配 (text/xml; charset=utf-8)

asp.net-core - 将 MVC 5 迁移到 MVC 6

iis - 检查是否安装了IIS?

c# - 使用 DateTime 结构解析 ISO8601 日期/时间

c# - KeyDown 事件的延迟计时器

c# - 如何在 ASP.NET Core 中结合基于模板和属性的路由?

c# - ASPNET 5 MVC 6 中的远程验证

angular - 浏览器在使用 azure 应用服务托管的根 index.html 上保留令人困惑的 Angular 应用的缓存版本

windows - 我可以使用 System.Data.SqlClient 提供程序与 Azure SQL 托管实例建立连接吗?其他 SQL 数据库怎么样?