簡體   English   中英

如何通過 SDK 對 Azure 進行交互式身份驗證?

[英]How to interactively authenticate to Azure via SDK?

我發現了這個要點: azure-service-bus-in-fsharp-with-fluent-api.fs最初看起來很有用,但我相信它已經過時了,現在應該使用DefaultAzureCredential

我使用 Visual Studio Code,但希望以交互方式進行身份驗證。

我創建了一個服務主體,得到了它的 client_id、client_secret、tenant_id 以及 subscription_id。

到目前為止,我的代碼是:

#r "nuget: Azure.Identity"
open Azure.Identity

let spDefaultAzCredentialOptions = 
    DefaultAzureCredentialOptions (
        ManagedIdentityClientId = Guid.NewGuid ()
)
let servicePrincipal = DefaultAzureCredential spDefaultAzCredentialOptions

在這個特定的例子中,我得到了錯誤:

Binding session to 'C:/Users/{user}/.nuget/packages/azure.identity/1.6.1/lib/netstandard2.0/Azure.Identity.dll'...
Binding session to 'C:/Users/{user}/.nuget/packages/azure.core/1.23.0/lib/net5.0/Azure.Core.dll'...
System.MissingMethodException: Method not found: 'Void Azure.Core.ClientOptions..ctor(Azure.Core.DiagnosticsOptions)'.
   at Azure.Identity.TokenCredentialOptions..ctor()
   at Azure.Identity.DefaultAzureCredentialOptions..ctor()
   at <StartupCode$FSI_0033>.$FSI_0033.main@() in C:\Users\{user \ path}\PowerShell\stdin:line 91
Stopped due to error

但是,在 Microsoft 的文檔DefaultAzureCredentialOptions Class 中,我可以找到該屬性——但也可以看到構造函數為空。

通常,如何使用 F# 驗證與 Azure 的交互?

盡管DefaultAzureCredential有大量文檔,但我還是花了一點時間才明白如何在我的案例中使用它。 它的第一種身份驗證方法是使用環境變量。 因此,我首先創建了一個記錄——不是必需的,但為了我的清楚起見——然后創建了新變量。

type ServicePrincipalCredentials = {
    ClientId: Guid
    ClientSecret: string
    TenantId: Guid 
}

let credentials = {
    ClientId = Guid.Parse "appId"
    ClientSecret = "Service Principal Password"
    TenantId = Guid.Parse "tenant" 
}

System.Environment.SetEnvironmentVariable ("AZURE_CLIENT_ID", credentials.ClientId.ToString())
System.Environment.SetEnvironmentVariable ("AZURE_CLIENT_SECRET", credentials.ClientSecret)
System.Environment.SetEnvironmentVariable ("AZURE_TENANT_ID", credentials.TenantId.ToString())

之后, DefaultAzureCredential應該可以工作。 我成功地將它與資源管理器一起使用來創建一個ArmClient

#r "nuget: Azure.ResourceManager.Resources, 1.3.0"
open Azure.ResourceManager
open Azure.ResourceManager.Resources

let client = Azure.ResourceManager.ArmClient(DefaultAzureCredential())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM