![](/img/trans.png)
[英]Using Azure managed identity for App Service not authorising for new SDK
[英]When using the Azure Fluent SDK in an Azure Function how can I create an azure object using a Managed Service Identity?
我正在編寫一個Azure功能,它將更新Azure DNS區域。 該功能附加了托管服務標識(MSI)。
我能夠使用非流暢的SDK來讀取DNS區域中的當前記錄。 但是當我嘗試使用流暢的庫做同樣的事情時,我得到以下錯誤:
[07/11/2018 14:36:37]執行'Function1'(失敗,Id = 8d34472e-956a-4ff3-a1b1-16ea6186934a)
[07/11/2018 14:36:37] System.Private.CoreLib:執行函數時出現異常:Function1.Microsoft.Azure.Management.ResourceManager.Fluent:Value不能為null。
[07/11/2018 14:36:37]參數名稱:MSI_ENDPOINT。
這樣我就可以輕松測試兩個庫之間的區別,我已經把測試函數放在一起了。
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Rest;
using Microsoft.Azure.Management.Dns;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
namespace UpdateDNS
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "{subscription}/{rg_name}/{zone_name}/{lib}")] HttpRequest req,
string subscription,
string rg_name,
string zone_name,
string lib,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
int count = 0;
dynamic records;
// determine the lib to use to get the dns data
switch (lib)
{
case "fluent":
AzureCredentialsFactory factory = new AzureCredentialsFactory();
MSILoginInformation msi = new MSILoginInformation(MSIResourceType.AppService);
AzureCredentials msiCred = factory.FromMSI(msi, AzureEnvironment.AzureGlobalCloud);
var azureAuth = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders).Authenticate(msiCred);
// set the subscription to work with
var azure = azureAuth.WithSubscription(subscription);
var dnszone = azure.DnsZones.GetByResourceGroup(rg_name, zone_name);
records = dnszone.ListRecordSets();
break;
default:
// get the token from the managed service identity
AzureServiceTokenProvider token_provider = new AzureServiceTokenProvider();
string token = await token_provider.GetAccessTokenAsync("https://management.azure.com");
TokenCredentials token_creds = new TokenCredentials(token);
// create the dns client
DnsManagementClient client = new DnsManagementClient(token_creds);
client.SubscriptionId = subscription;
records = client.RecordSets.ListAllByDnsZone(rg_name, zone_name);
break;
}
foreach (var record in records)
{
Console.WriteLine(record.Name);
count++;
}
return new OkObjectResult($"Records: {count}");
}
}
}
這是一個HTTP觸發的Azure功能,允許將訂閱,資源組和DNS區域作為參數以及要使用的庫傳遞。
因此,為了測試非流暢的庫,我可以調用以下內容:
HTTP://本地主機:7071 / API / ee65837a-8b52-4fed-9820-f2eb0bb11baf / my_rg / my_zone /穩定
這將返回如下內容:
Records: 3
但是,如果我嘗試運行相同的查詢但使用流暢的庫,我會收到如上所示的錯誤:
HTTP://本地主機:7071 / API / ee65837a-8b52-4fed-9820-f2eb0bb11baf / my_rg / my_zone /流利
我錯過了需要傳入的參數嗎? 我不確定'MSI_ENDPOINT'的設置位置以及應該設置的位置。 我的感覺是應該為我做這件事。
正在使用的庫的版本是:
Microsoft.Azure.Management.DNS 3.0.1
Microsoft.Azure.Management.Fluent 1.17.0
Microsoft.Azure.Services.AppAuthentication 1.0.3
我在Visual Studio中本地運行它,該帳戶登錄到具有對Azure的適當訪問權限的帳戶。
我在Visual Studio中本地運行它,該帳戶登錄到具有對Azure的適當訪問權限的帳戶。
您在本地計算機上沒有“管理服務標識”,因此無法使用本地第一種方法。 正如junnas所說,您可以將Azure Services Authentication Extension
與AzureServiceTokenProvider
一起AzureServiceTokenProvider
,后者會檢索您的帳戶以訪問Azure。
有關更多詳細信息,請參閱此文章 。
所以,首先你需要做的是找你yourappname.scm.azurewebsites.net
並選擇Environment
來檢查其中是否有MSI_ENDPOINT
變量。 這意味着您已成功設置MSI。
其次,將功能發布到Azure,它將正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.