Microsoft Azure批处理服务帐户从.NET创建

时间:2017-07-24 10:38:09

标签: .net azure permissions azure-batch

我正在考虑从.NET SDK创建Microsoft Azure批量帐户。我在身份验证方面取得了成功,但我发现了这个错误:

  

Microsoft.Rest.Azure.CloudException:客户端   ' xxxxxxxxxxxxxxxxxxxxxxxxxx'对象id   ' xxxxxxxxxxxxxxxxxxxxxxxxxx'没有授权执行   动作' Microsoft.Batch / batchAccounts / write'超范围   ' /subscriptions/344cb101-b565-453f-83f3-87e9a13c4ddb/resourceGroups/bswbatch5RG/providers/Microsoft.Batch/batchAccounts/bbbbbbbbbtest'

1 个答案:

答案 0 :(得分:0)

根据您提到的例外情况,我假设您没有为应用程序分配正确的权限,我们可以参考Assign application to role了解更多详情。

enter image description here

我也创建了一个演示,它在我身边正常工作。以下是我的演示代码。

    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;
    }

enter image description here

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>