【问题标题】:Microsoft Azure Batch Service Account Create from .NET从 .NET 创建 Microsoft Azure Batch 服务帐户
【发布时间】:2017-12-29 21:50:07
【问题描述】:
我正在考虑从 .NET SDK 创建一个 Microsoft Azure Batch 帐户。我成功通过了身份验证,但我刚刚遇到了这个错误:
Microsoft.Rest.Azure.CloudException:客户端
带有对象 ID 的“xxxxxxxxxxxxxxxxxxxxxxxxxxx”
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 无权执行
在范围内操作“Microsoft.Batch/batchAccounts/write”
'/subscriptions/344cb101-b565-453f-83f3-87e9a13c4ddb/resourceGroups/bswbatch5RG/providers/Microsoft.Batch/batchAccounts/bbbbbbbbbtest'
【问题讨论】:
标签:
.net
azure
permissions
azure-batch
【解决方案1】:
根据您提到的异常,我假设您没有为应用程序分配正确的权限,更多细节我们可以参考Assign application to role。
我还创建了一个演示,它在我这边可以正常工作。以下是我的演示代码。
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>