错误:在配置Azure键Vault时,在ASP.NET核心应用程序中没有任何此类主机

发布于 2025-02-03 16:53:35 字数 712 浏览 5 评论 0 原文

在ASP.NET Core Web API项目中配置Azure密钥库时,我面临问题。

以下是代码片段,也是参考的错误,我试图找到根本原因,但没有运气。

异常详细信息

请帮助我解决此问题。

I am facing an issue while configuring Azure Key vault in Asp.net core Web API project .

Below is the code snippet as well as error for reference and I tried to find the root cause but no luck.

enter image description here

enter image description here

Error while Run() method execution.

enter image description here

Exception details
enter image description here

Please help me out in solving this issue .Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2025-02-10 16:53:36

可能是从.NET结束的问题,主要是.NET Core 3.0.100或3.1 中的。当大部分是在Visual Studio中的调试器下运行时,这似乎会发生这种情况,并且当进行长时间的平行呼叫并且重新拨动可能有助于阻止此播放一段时间并再次尝试。

  1. 因此,请尝试升级/更新.NET Project SDK (如果有任何更新)
    可在Visual Studio中找到。
  2. 尝试使用命令行运行,

在错误的消息中,您可以在4次尝试后看到重试失败。

您可以检查ReloadInterval 在3.0和3.1中使用的属性,

builder.AddAzureKeyVault(
    new Uri(Configuration["KeyVault:URI"]), 
    new DefaultAzureCredential(
        new DefaultAzureCredentialOptions
        {
            ExcludeSharedTokenCacheCredential = true,
            VisualStudioTenantId = Configuration["AzureAd:TenantId"]
        }), 
    new AzureKeyVaultConfigurationOptions() 
    {
        ReloadInterval = TimeSpan.FromMinutes(15)
    }
);

您可以尝试捕获此异常,并为您的代码实现重试机制,如果抛出此异常,以便可以尝试重试时间和下一次尝试所需的延迟。
azure钥匙库节流指导| Microsoft Docs

SecretClientOptions options = new SecretClientOptions()
    {
        Retry =
        {
            Delay= TimeSpan.FromSeconds(2),
            MaxDelay = TimeSpan.FromSeconds(16),
            MaxRetries = 5,
            Mode = RetryMode.Exponential
         }
    };
    var client = new SecretClient(new Uri("https://keyVaultName.vault.azure.net"), new DefaultAzureCredential(),options);
                                 
    //Retrieve Secret
    secret = client.GetSecret(secretName);

另请参见 github讨论

  • 如果仍然发行,则可能是由于几次
    网络问题。因此,请检查网络,防火墙以及该端点的任何DNS问题。
  • 检查URI如果端点不正确或托管身份没有
    数据所有者阅读器角色。请确保您拥有正确
    权限
    可以访问Azure KeyVault并给出适当的 访问
    策略
    至少在需要时列出并创建。
  • 请确保拥有 角色 (rbac)此处提供
    密钥vault。

参考:

  1. asp.net core-如何配置Azure keyVault刷新间隔
    使用azure.security.keyvault库 - 堆栈溢出

It can be issue from .net end mostly ,in .NET Core 3.0.100 or 3.1. This seems to occur when running under the debugger in Visual Studio mostly and when long parallel calls are made and retrypolicy may help stop this from giving exception for sometime and tries again.

  1. So Please try to Upgrade/Update the .NET Project SDK if any updates
    available in Visual Studio.
  2. Try running with command line

Also in the message of error you can see retry faild after 4 tries..

You can check ReloadInterval Property which is used in 3.0 and 3.1

builder.AddAzureKeyVault(
    new Uri(Configuration["KeyVault:URI"]), 
    new DefaultAzureCredential(
        new DefaultAzureCredentialOptions
        {
            ExcludeSharedTokenCacheCredential = true,
            VisualStudioTenantId = Configuration["AzureAd:TenantId"]
        }), 
    new AzureKeyVaultConfigurationOptions() 
    {
        ReloadInterval = TimeSpan.FromMinutes(15)
    }
);

You could try catching this exception and implementing a retry mechanism for your code if this exception is thrown so that it could try with retry time and delay required for next attempt.
Azure Key Vault throttling guidance | Microsoft Docs

SecretClientOptions options = new SecretClientOptions()
    {
        Retry =
        {
            Delay= TimeSpan.FromSeconds(2),
            MaxDelay = TimeSpan.FromSeconds(16),
            MaxRetries = 5,
            Mode = RetryMode.Exponential
         }
    };
    var client = new SecretClient(new Uri("https://keyVaultName.vault.azure.net"), new DefaultAzureCredential(),options);
                                 
    //Retrieve Secret
    secret = client.GetSecret(secretName);

Also see GitHub discussion

  • If still issue remains, it may be calling several times due to
    network issue also.So please check the network , firewall and if there is any DNS issue for that endpoint.
  • Check URI if endpoint is incorrect or Managed Identity does not have
    Data owner or Reader role.Please make sure that you have proper
    permissions
    to access azure keyvault and give proper access
    policies
    atleast get , list and create if needed .
  • Make sure to have one of the roles(RBAC) provided here to access the
    keyvault .

References:

  1. asp.net core - How to configure Azure KeyVault refresh interval
    with the Azure.Security.KeyVault libraries - Stack Overflow
  2. azure sdk .net issues(github)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文