簡體   English   中英

從.NET創建Microsoft Azure批服務帳戶

[英]Microsoft Azure Batch Service Account Create from .NET

我正在考慮從.NET SDK創建一個Microsoft Azure批處理帳戶。 我認證成功,但是遇到了這個錯誤:

Microsoft.Rest.Azure.CloudException:對象ID為'xxxxxxxxxxxxxxxxxxxxxxxxxx'的客戶端'xxxxxxxxxxxxxxxxxxxxxxxxxx'無權對范圍'/ subscriptions / 344cb101-b565-453f-83f3-87e9a13c4ddb執行操作'Microsoft.Batch / batchAccounts / write' /resourceGroups/bswbatch5RG/providers/Microsoft.Batch/batchAccounts/bbbbbbbbbtest”

根據您提到的例外情況,我假設您沒有為應用程序分配正確的權限,有關更多詳細信息,請參閱將應用程序分配給角色

在此處輸入圖片說明

我還創建了一個演示,它可以正常運行。 以下是我的演示代碼。

    static string appId = "application name";
    static string secretKey = "scretkey";
    static string tenantId = "tenant id";
    private static readonly string _subscriptionId = "subscription Id";

    static void Main(string[] args)
    {
        var resourceGroupName = "resource Group name";
        var accountName = "batch account name";
        var location = "eastus2";// location
        var accessToken = GetAccessToken(tenantId, appId, secretKey).Result;
        BatchManagementClient batchManagementClient =
            new BatchManagementClient(new TokenCredentials(accessToken)) {SubscriptionId = _subscriptionId};
        var batchAccount = batchManagementClient.BatchAccount.Create(resourceGroupName, accountName, new BatchAccountCreateParameters() { Location = location });


    }
    public static async Task<string> GetAccessToken(string azureTenantId, string azureAppId, string azureSecretKey)
    {

        var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
        ClientCredential clientCredential = new ClientCredential(appId, secretKey);
        var tokenResponse = await context.AcquireTokenAsync("https://management.azure.com/", clientCredential);
        var accessToken = tokenResponse.AccessToken;
        return accessToken;
    }

在此處輸入圖片說明

Packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Azure.Management.Batch" version="3.0.0" targetFramework="net461" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net461" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.8" targetFramework="net461" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.8" targetFramework="net461" />
  <package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.3.0" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
</packages>

暫無
暫無

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

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