{"action":"created","issue":{"url":"https://api.github.com/repos/Azure/azure-sdk-for-net/issues/5275","repository_url":"https://api.github.com/repos/Azure/azure-sdk-for-net","labels_url":"https://api.github.com/repos/Azure/azure-sdk-for-net/issues/5275/labels{/name}","comments_url":"https://api.github.com/repos/Azure/azure-sdk-for-net/issues/5275/comments","events_url":"https://api.github.com/repos/Azure/azure-sdk-for-net/issues/5275/events","html_url":"https://github.com/Azure/azure-sdk-for-net/issues/5275","id":413200621,"node_id":"MDU6SXNzdWU0MTMyMDA2MjE=","number":5275,"title":"VM: Unable to Add Azure Key Vault Through AzureServiceTokenProvider: No Connection string","user":{"login":"sirlukecoop","id":10985741,"node_id":"MDQ6VXNlcjEwOTg1NzQx","avatar_url":"https://avatars1.githubusercontent.com/u/10985741?v=4","gravatar_id":"","url":"https://api.github.com/users/sirlukecoop","html_url":"https://github.com/sirlukecoop","followers_url":"https://api.github.com/users/sirlukecoop/followers","following_url":"https://api.github.com/users/sirlukecoop/following{/other_user}","gists_url":"https://api.github.com/users/sirlukecoop/gists{/gist_id}","starred_url":"https://api.github.com/users/sirlukecoop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sirlukecoop/subscriptions","organizations_url":"https://api.github.com/users/sirlukecoop/orgs","repos_url":"https://api.github.com/users/sirlukecoop/repos","events_url":"https://api.github.com/users/sirlukecoop/events{/privacy}","received_events_url":"https://api.github.com/users/sirlukecoop/received_events","type":"User","site_admin":false},"labels":[{"id":1178448111,"node_id":"MDU6TGFiZWwxMTc4NDQ4MTEx","url":"https://api.github.com/repos/Azure/azure-sdk-for-net/labels/Management","name":"Management","color":"ffeb77","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2019-02-22T01:29:19Z","updated_at":"2019-03-11T18:22:33Z","closed_at":null,"author_association":"NONE","body":"I tried to connect to an Azure Key Vault via a Virtual Machine with the AzureServiceTokenProvider pattern, but failed (see 1 below). The VM was setup for MSI. As a I stepped through the code, I noticed that it checks the environment variables for some MSI related configuration but my VM had neither of the variables. Since I added the Identity after I created the VM, I thought maybe if I setup a new VM with Identity set from the get-go everything would be good. I was wrong. \r\nError: Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException: Parameters: Connectionstring: [No connection string specified]\r\n\r\nSo, I followed the steps in link 2 (which I should have used in the first place). Essentially, these instructions make calls through the REST API. It mostly works, though it is not as syntactically sugary.\r\n\r\n1) https://docs.microsoft.com/en-us/azure/key-vault/tutorial-net-create-vault-azure-web-app#open-and-edit-the-solution\r\n2) https://docs.microsoft.com/en-us/azure/key-vault/tutorial-net-windows-virtual-machine\r\n\r\nWhat's strange is that the backup behavior of attempt 1 is to contact the IDMS instance (169.254.169.254/metadata/identity/oauth2/token) that attempt 2 explicitly calls. I'm out of time to understand what the missing link is. \r\n\r\nHowever, if anyone has the same problem, I'd be glad to submit a PR with my solution. All I did was mix 1 and 2. Or if someone could show me how I'm over complicating things, that'd be even better.\r\n\r\nSolution:\r\n```\r\nvar keyVaultEndpoint = Environment.GetEnvironmentVariable(\"KEYVAULT_ENDPOINT\");\r\nif (!string.IsNullOrEmpty(keyVaultEndpoint))\r\n{\r\n    var keyVaultClient = new KeyVaultClient(async (a, r, s) => \r\n    {\r\n        var uri = \"http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net\";\r\n        var request = WebRequest.Create(uri);\r\n        request.Headers.Add(\"Metadata\", \"true\");\r\n        var response = await request.GetResponseAsync();\r\n        var accessToken = ParseWebResponse(response, \"access_token\");\r\n        return accessToken;\r\n     });\r\n\r\n     builder.AddAzureKeyVault(keyVaultEndpoint, keyVaultClient, new DefaultKeyVaultSecretManager());\r\n}\r\n\r\nprivate static string ParseWebResponse(WebResponse response, string tokenName)\r\n{\r\n    string token = string.Empty;\r\n    using (var stream = response.GetResponseStream())\r\n    {\r\n        var reader = new StreamReader(stream, Encoding.UTF8);\r\n        var responseString = reader.ReadToEnd();\r\n        var joResponse = JObject.Parse(responseString);\r\n        var ojObject = (JValue)joResponse[tokenName];\r\n        token = ojObject.Value.ToString();\r\n    }\r\n    return token;\r\n}\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/Azure/azure-sdk-for-net/issues/comments/471661864","html_url":"https://github.com/Azure/azure-sdk-for-net/issues/5275#issuecomment-471661864","issue_url":"https://api.github.com/repos/Azure/azure-sdk-for-net/issues/5275","id":471661864,"node_id":"MDEyOklzc3VlQ29tbWVudDQ3MTY2MTg2NA==","user":{"login":"kurtzeborn","id":22382549,"node_id":"MDQ6VXNlcjIyMzgyNTQ5","avatar_url":"https://avatars0.githubusercontent.com/u/22382549?v=4","gravatar_id":"","url":"https://api.github.com/users/kurtzeborn","html_url":"https://github.com/kurtzeborn","followers_url":"https://api.github.com/users/kurtzeborn/followers","following_url":"https://api.github.com/users/kurtzeborn/following{/other_user}","gists_url":"https://api.github.com/users/kurtzeborn/gists{/gist_id}","starred_url":"https://api.github.com/users/kurtzeborn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kurtzeborn/subscriptions","organizations_url":"https://api.github.com/users/kurtzeborn/orgs","repos_url":"https://api.github.com/users/kurtzeborn/repos","events_url":"https://api.github.com/users/kurtzeborn/events{/privacy}","received_events_url":"https://api.github.com/users/kurtzeborn/received_events","type":"User","site_admin":false},"created_at":"2019-03-11T18:22:33Z","updated_at":"2019-03-11T18:22:33Z","author_association":"MEMBER","body":"Thank you for opening this issue!  We are routing it to the appropriate team for follow up."}}
{"actor":{"display_login":"kurtzeborn"}}