Microsoft Azure Batch 服务帐户从 .NET 创建
Microsoft Azure Batch Service Account Create from .NET
我正在考虑从 .NET SDK 创建 Microsoft Azure Batch 帐户。我已成功进行身份验证,但我只是遇到了这个错误:
Microsoft.Rest.Azure.CloudException: The client
'xxxxxxxxxxxxxxxxxxxxxxxxxx' with object id
'xxxxxxxxxxxxxxxxxxxxxxxxxx' does not have authorization to perform
action 'Microsoft.Batch/batchAccounts/write' over scope
'/subscriptions/344cb101-b565-453f-83f3-87e9a13c4ddb/resourceGroups/bswbatch5RG/providers/Microsoft.Batch/batchAccounts/bbbbbbbbbtest'
根据你提到的异常,我假设你没有为应用程序分配正确的权限,更多细节我们可以参考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>
我正在考虑从 .NET SDK 创建 Microsoft Azure Batch 帐户。我已成功进行身份验证,但我只是遇到了这个错误:
Microsoft.Rest.Azure.CloudException: The client 'xxxxxxxxxxxxxxxxxxxxxxxxxx' with object id 'xxxxxxxxxxxxxxxxxxxxxxxxxx' does not have authorization to perform action 'Microsoft.Batch/batchAccounts/write' over scope '/subscriptions/344cb101-b565-453f-83f3-87e9a13c4ddb/resourceGroups/bswbatch5RG/providers/Microsoft.Batch/batchAccounts/bbbbbbbbbtest'
根据你提到的异常,我假设你没有为应用程序分配正确的权限,更多细节我们可以参考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>