繁体   English   中英

如果没有订阅可用,是否可以将创建资源组的权限授予服务主体?

[英]Is it possible to grant permission for create resource group to service principal if no subscriptions are available?

我尝试在我的 UWP C# 项目中向服务主体授予创建资源组的权限。 我使用 Azure SDK 为 .NET 创建机密应用程序注册并获取访问令牌。 它与我的扩展方法一起使用,以防止平台检查。 但是,当我尝试获取服务主体的角色分配时,我收到 Azure.RequestFailedException 0x80131500,“找不到订阅 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'”。

ResourceIdentifier scope = new ResourceIdentifier($"/subscriptions/{MyTenantId}");
ConfidentialClientCredential credential = new ConfidentialClientCredential(MyConfidentialClientApplication);
ArmClient armClient = new ArmClient(credential);
RoleAssignmentCollection roleAssignmentCollection = AuthorizationExtensions.GetRoleAssignments(armClient, scope);
IEnumerable<RoleAssignmentResource> roleAssignments = roleAssignmentCollection?.GetAll($"$filter=atScope()+and+assignedTo('{strServicePrincipalId}')");
if(roleAssignments != null)
{
    AuthorizationRoleDefinitionResource roleDefinition = null;
    foreach(RoleAssignmentResource curRoleAssignment in roleAssignments)    // <-- exception throws there
    {
        if(curRoleAssignment?.HasData == true && !string.IsNullOrWhiteSpace(curRoleAssignment.Data.RoleDefinitionId?.Name))
        {
            roleDefinition = await AuthorizationExtensions.GetAuthorizationRoleDefinitionAsync(armClient, scope, curRoleAssignment.Data.RoleDefinitionId);
        }
    }
}

我在这里使用我的租户 ID 作为我的订阅 ID,因为它是由 Azure CLI 命令返回的:

az account show --query "id"

我猜AAD RBAC可以用于此目的,但我还没有找到合适的AD权限。
在我的案例中,有什么方法可以授予创建资源组的权限吗?

请注意,订阅 ID租户 ID不同。 如果您没有订阅,您既不能创建资源组,也不能将 RBAC 角色分配给服务主体。

如果没有订阅,您将在运行az account show --query "id"命令时获得租户 ID作为响应。

我试图通过 CLI 在我的环境中重现相同的内容并得到以下结果:

当我运行以下命令时,我在响应中得到了租户 ID ,因为我没有如下所示的订阅:

az login --allow-no-subscriptions --only-show-errors
az account show --query "id"

回复:

在此处输入图像描述

我的租户中有一位服务负责人,如下所示:

在此处输入图像描述

当我尝试通过将tenantID作为 subscriptionID 使用以下 CLI 命令将角色分配给上述服务主体时,我遇到了与您相同的错误

az role assignment create --assignee-object-id <sp_objectID> --assignee-principal-type ServicePrincipal --role Contributor --scope /subscriptions/<tenant ID>

回复:

在此处输入图像描述

没有订阅,您只能执行目录级操作,例如管理Azure AD用户、组、应用程序注册等...

您肯定需要订阅才能管理Azure 资源,例如资源组、虚拟机、存储帐户、角色分配等...

解决该错误,请确保至少有一个订阅链接到您的租户并再次登录,如下所示:

az login --allow-no-subscriptions --only-show-errors
az account show --query "id"

在此处输入图像描述

当我通过包含上面的subscriptionID再次运行下面的CLI 命令时,角色分配成功创建,如下所示:

 az role assignment create --assignee-object-id <sp_objectID> --assignee-principal-type ServicePrincipal --role Contributor --scope /subscriptions/<subscription ID>

回复:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM