繁体   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