Azure 上的 .Net CryptographicException "Object already exists"

标签 .net azure cryptography azure-web-app-service cryptographicexception

我们在 Azure 上的两个 Web 应用程序上部署了一个网站:生产版本和预生产版本。

该网站在某个时间使用以下代码创建一个容器来托管 RSA key :

// -----------------------------
// Part 1 : Initialize csp params
// -----------------------------
const int PROVIDER_RSA_FULL = 1;
const string CONTAINER_NAME = "OurKeyContainer";
CspParameters cspParams;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";

// --------------------------------------------------
// Part 2 : A try to set folder access rights to "everyone"
// --------------------------------------------------
// http://whowish-programming.blogspot.fr/2010/10/systemsecuritycryptographycryptographic.html
// http://stackoverflow.com/questions/5013881/c-sharp-how-do-i-get-the-everybody-user
var sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var rule = new CryptoKeyAccessRule(sid, CryptoKeyRights.FullControl, AccessControlType.Allow);
cspParams.CryptoKeySecurity = new CryptoKeySecurity();
cspParams.CryptoKeySecurity.SetAccessRule(rule);

return new RSACryptoServiceProvider(cspParams);

问题是此代码仅适用于其中一个网站,即实际首次启动的网站。第二个抛出了 CryptographicException“对象已存在”。

经过谷歌搜索后,该问题似乎是由执行该网站的用户无权访问 key 容器引起的,但不幸的是,建议的修复(在上面的代码的第 2 部分中实现)不起作用..

有什么想法或建议吗?

谢谢

莉安娜

最佳答案

我认为问题在于您基本上是在尝试创建一个具有相同名称的机器范围容器两次。我假设pre-prodprod环境位于同一台计算机上,也许作为应用程序服务中的部署槽?我至少可以通过此设置复制您的问题。我猜还有一些其他设置可能会产生相同的情况,但这是最有可能的。

您可以在此设置中更改三项内容,这将产生不同的结果:

  • 授予访问权限的身份
  • 容器商店
  • 容器名称

对于身份,我们可以使用 Everyone (正如您尝试过的那样)或 Current我们可以从 WindowsIdentity 获取

    private IdentityReference GetWindowsIdentity()
    {
        return System.Security.Principal.WindowsIdentity.GetCurrent().User;
    }

    private IdentityReference GetEveryoneIdentity()
    {
        return new SecurityIdentifier(WellKnownSidType.WorldSid, null);
    }

容器存储可以是 machine wide或仅适用于当前用户

// machine wide store
var cspParams = new CspParameters(PROVIDER_RSA_FULL)
{
    ...
    Flags = CspProviderFlags.UseMachineKeyStore;
    ...
};

// default store
var cspParams = new CspParameters(PROVIDER_RSA_FULL)
{
    ...
    Flags = CspProviderFlags.UseDefaultKeyContainer;
    ...
};

对于商店名称,我们可以选择一些东西,在您的情况下,您选择相同的东西,但我们也可以设置一些对于身份来说唯一的东西

    KeyContainerName = "OurKeyContainer",

    KeyContainerName = $"OurKeyContainer-{identity}",

不同的组合会产生不同的结果:

每个人的身份、机器范围的存储、相同的容器名称在其中一个插槽上失败,并显示 System.Security.Cryptography.CryptographicException: Object already exists.

每个人的身份,机器范围的存储,每个身份的容器 OK

每个人身份、用户存储、相同的容器名称在两个插槽上均失败,并显示 System.Security.Cryptography.CryptographicException: The system cannot find the file specified.

每个身份、用户存储、每个身份的容器 OK

当前身份、机器范围存储、相同容器名称在其中一个插槽上失败,并显示 System.Security.Cryptography.CryptographicException: Object already exists.

当前身份、机器范围存储、每个身份的容器 OK

当前身份、用户存储、相同容器名称在两个插槽上均失败,并显示 System.Security.Cryptography.CryptographicException: The system cannot find the file specified.

当前身份、用户存储、每个身份的容器在两个插槽上均失败,并显示 System.Security.Cryptography.CryptographicException: The system cannot find the file specified.

因此,总而言之,更改容器的名称以包含环境特有的名称将解决问题。它不一定是身份(但您将在计算机上运行的每个应用程序服务获得一个身份,因此它相当安全),如果您将其设置为 ENVIRONMENT VARIABLE,它可能是环境的名称。在您的应用服务中并确保将其设置为 pre-prodprod分别。

Here is the playaround code to test it

关于Azure 上的 .Net CryptographicException "Object already exists",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41615391/

相关文章:

azure - 如何在 Azure Synapse 中克隆 SQL 数据库 - 尽管是 Synapse 管理员,但没有菜单选项

azure - 允许 Azure CDN 访问 Azure KeyVault

azure - 使用 Azure Powershell 脚本出现错误 "Changing property ' osDisk.name' is not allowed."

c - 程序无法使用空格正确加密

c# - 如何在没有独占锁的情况下使用 FileStream 附加到文件?

.net - 仅根据环境拾取服务器上的 Hangfire 任务(交换槽)

.net - x509 C# 示例?

c# - 我可以让 ProcessBytes 返回准确的数量吗?

.net - 在 .net 中重命名命名空间

c# - DllImport user32 与 user32.dll