【问题标题】:Getting Azure Active directory token获取 Azure 活动目录令牌
【发布时间】:2018-10-20 10:01:59
【问题描述】:

我有一个 Azure 帐户,现在我正在尝试在控制台应用程序中获取令牌以管理资源(即创建资源组等):

string userName = "xyz@gmail.com";
string password = "XXXXXXXXX";
string directoryName = "xyzgmail.onmicrosoft.com";
string clientId = "guid-of-registered-application-xxx";
var credentials = new UserPasswordCredential(userName, password);
var authenticationContext = new AuthenticationContext("https://login.windows.net/" + directoryName);
var result = await authenticationContext.AcquireTokenAsync("https://management.core.windows.net/", clientId, credentials);

AcquireTokenAsync 通话中我有

Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException: 'accessing_ws_metadata_exchange_failed: 访问 WS 元数据交换 失败'

有人可以帮忙吗?

更新:我如何尝试在新创建的用户下创建资源组

var jwtToken = result.AccessToken;
string subscriptionId = "XX-XX-XX-YY-YY-YY";
var tokenCredentials = new TokenCredentials(jwtToken);
var client = new ResourceManagementClient(tokenCredentials);
client.SubscriptionId = subscriptionId;
var rgResponse =  await client.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync("myresgroup77777",
                new ResourceGroup("East US"));

这里我遇到了另一个异常

'带有对象 ID 的客户端 'newaduser@xyzgmail.onmicrosoft.com' 'aaa-aaa-aaa-aaa' 无权执行操作 'Microsoft.Resources/subscriptions/resourcegroups/write' 超出范围 '/subscriptions/XX-XX-XX-YY-YY-YY/resourcegroups/myresgroup77777'。'

【问题讨论】:

  • 你有链接到你看到它以这种方式使用的资源吗? I don't even see an overload that matches what you are doing
  • 您是否为用户分配了角色?执行Windows Azure Service Management API 的权限被授予假定登录用户身份的应用程序。但是,该用户必须是有权在订阅中创建资源组的角色。尝试将订阅级别的内置Contributor 角色分配给该用户。
  • @GauravMantri 太棒了!有用!只是一个小问题,我可以通过初始用户(即所有者)登录而不是火化新用户吗?
  • 你应该可以这样做。我试图查找您遇到的第一个错误,但找不到任何关于此的结论。让我发表我的评论作为答案。

标签: c# azure azure-active-directory


【解决方案1】:

不知道为什么会出现第一个错误,但第二个错误是因为登录用户没有执行操作的权限(如错误消息中所述)。

当您分配执行Windows Azure Service Management API 的权限时,它实际上是分配给假定登录用户身份的应用程序。

为了在 Azure 订阅中执行 Create Resource Group 操作,该用户必须具有允许执行此操作的角色。您可以尝试将 Azure 订阅级别的内置 Contributor 角色分配给此用户。

另外,关于使用login.windows.net v/s login.microsoftonline.com,建议使用后者。当您使用login.windows.net 时,它会自动重定向到login.microsoftonline.com。使用login.microsoftonline.com 将为您节省一次重定向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多