简体   繁体   English

如何从IIS上托管的.net代码访问密钥库机密

[英]How to access key vault secret from .net code hosted on IIS

I have a Scenario: 我有一个场景:

  1. Create Key vault with secret in Azure. 在Azure中使用秘密创建密钥保管库。
  2. Access this secret in Code. 在代码中访问此机密。

    1. code is working in Local(tested using Azure CLI) 代码在本地运行(使用Azure CLI测试)
    2. Application hosted in Azure App service(MSI enable) working fine. 托管在Azure App服务(启用MSI)中的应用程序运行正常。
    3. We need to Host same application on Azure VM(MSI enable) IIS server-Not working 我们需要在Azure VM(启用MSI)IIS服务器上托管同一应用程序-无法正常工作

I want the solution and suggestions for above point(Last point) 我想要以上几点的解决方案和建议(最后一点)

Code to Access Key vault Secret value 访问密钥库机密值的代码

     var azureServiceTokenProvider = new AzureServiceTokenProvider();
            var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
            ConfigurationApp.ClientId = keyVaultClient.GetSecretAsync("https://test.vault.azure.net/", "testid").Result.Value;

Follow this Article - https://kasunkodagoda.com/2018/04/28/allow-application-running-on-an-azure-virtual-machine-to-access-azure-key-vault-using-managed-service-identity/ 遵循本文-https: //kasunkodagoda.com/2018/04/28/allow-application-running-on-an-azure-virtual-machine-to-access-azure-key-vault-using-managed-service-身份/

https://azure.microsoft.com/en-us/resources/samples/app-service-msi-keyvault-dotnet/ https://azure.microsoft.com/en-us/resources/samples/app-service-msi-keyvault-dotnet/

I have fixed my issue:Access key vault secret from .net code hosted on Azure VM IIS 我已修复我的问题:从Azure VM IIS上托管的.net代码访问密钥库机密

  public async Task getAppconfiguration2()
    {
        string URI = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net";
        Uri uri = new Uri(String.Format(URI));
        HttpClient _client = new HttpClient();
        _client.DefaultRequestHeaders.Add("Metadata", "true");
        HttpRequestMessage request = new HttpRequestMessage
        {
            // Content = new StringContent(body, Encoding.UTF8, "application/json"),
            Method = HttpMethod.Get,
            RequestUri = new Uri(URI)
        };


        var res = await _client.SendAsync(request);
        var content = res.Content.ReadAsStringAsync();
        JObject token = JsonConvert.DeserializeObject<JObject>(content.Result.ToString());
        string token1 = token["access_token"].ToString();
        ConfigurationApp.Encyptionkey = token1.ToString();

        HttpClient _client1 = new HttpClient();
        _client1.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token1);
        HttpRequestMessage request1 = new HttpRequestMessage
        {

            Method = HttpMethod.Get,
            RequestUri = new Uri("https://test.vault.azure.net/secrets/clientid?api-version=2016-10-01")
        };


        var rs = _client1.SendAsync(request1);
        var rk = rs.Result.Content.ReadAsStringAsync();
        JObject clientjson = JsonConvert.DeserializeObject<JObject>(rk.Result.ToString());
        ConfigurationApp.ClientId = clientjson["value"].ToString();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 DefaultAzureCredential 从 Azure Key Vault 检索 Secret - How to retrieve Secret from Azure Key Vault using DefaultAzureCredential 使用应用程序客户端密码访问 azure 密钥保管库密码 - Access azure key vault secret with application client secret 从 azure 密钥保管库中检索秘密 - retrieve secret from azure key vault 如何检查机密是否在 Azure Key Vault 中 - How to check if secret is in Azure Key Vault 如何在.net源代码中对我的访问密钥和秘密访问密钥进行硬编码? - How do I hardcode my accesskey and secret access key in my .net source code? 使用未部署在 Azure 中的应用程序访问 Azure Key Vault 存储的机密 - Access Azure Key Vault stored secret using application not deployed in Azure Azure Key Vault 秘密访问间歇性地无法连接,出现套接字异常 - Azure Key Vault secret access intermittently failing to connect with socket exception 如何从服务“内部”的代码访问 web.config 以获取托管在 IIS 中的 WCF 服务 - How to access the web.config for a WCF service hosted in IIS from the code “inside” the service 从 AWS Secret Manager 访问密钥 - Access secret key from AWS Secret Manager 尝试从 Azure Key Vault 获取机密时变得未经授权 - Getting Unathorized when trying to get a secret from Azure key Vault
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM