首页 > 解决方案 > 具有来自 AzureKeyvault 的签名凭据的 IdentityServer4 的多个实例

问题描述

我的 identityserver4 部署到 AKS 中,副本数为 1。但是当我将副本数设置为大于 1 时,我开始看到问题。

有时身份验证验证有效,有时无效。根据 IdentityServer4 门户网站的文档,如果我们使用多个正在运行的身份服务器实例,那么我们必须确保在所有实例中使用相同的签名凭据。我已使用 Azure keyvault Keys 来签署本文之后的凭据。但我仍然面临这个问题。我还确保即使根据此链接运行多个实例,发现 url 也是相同的。请指教。

用于从 Azure keyVault 获取密钥的示例启动代码 -

var keyClient = new KeyClient(
        new Uri(""), // e.g. https://scottbrady91-test.vault.azure.net/
        new ClientSecretCredential(
            tenantId: "",
            clientId: "",
            clientSecret: ""));

    Response<KeyVaultKey> response = keyClient.GetKey(""); // e.g. IdentityServerSigningKeyEcc

    AsymmetricSecurityKey key;
    string algorithm;

    if (response.Value.KeyType == KeyType.Ec)
    {
        ECDsa ecDsa = response.Value.Key.ToECDsa();
        key = new ECDsaSecurityKey(ecDsa) {KeyId = response.Value.Properties.Version};
        
        // parse from curve
        if (response.Value.Key.CurveName == KeyCurveName.P256) algorithm = "ES256";
        else if (response.Value.Key.CurveName == KeyCurveName.P384) algorithm = "ES384";
        else if (response.Value.Key.CurveName == KeyCurveName.P521) algorithm = "ES521";
        else  throw new NotSupportedException();
    }
    else if (response.Value.KeyType == KeyType.Rsa)
    {
        RSA rsa = response.Value.Key.ToRSA();
        key = new RsaSecurityKey(rsa) {KeyId = response.Value.Properties.Version};

        // you define
        algorithm = "PS256";
    }
    else
    {
        throw new NotSupportedException();
    }
    
    services.AddIdentityServer()
        .AddTestUsers(TestUsers.Users)
        .AddInMemoryIdentityResources(Config.Ids)
        .AddInMemoryApiResources(Config.Apis)
        .AddInMemoryClients(Config.Clients)
        .AddSigningCredential(key, algorithm);

标签: identityserver4azure-keyvault

解决方案


可能给您带来问题的另一件事是数据保护 API,它负责保护所涉及的会话 cookie。如果您希望会话 cookie 在实例之间有效,那么它们需要共享相同的加密密钥(密钥环)。

请参阅配置 ASP.NET Core 数据保护

我也在这里写过关于这个的博客: Storing the ASP.NET Core Data Protection Key Ring in Azure Key Vault


推荐阅读