授予功能跨租户Azure RM访问权限

时间:2018-10-17 15:59:15

标签: azure azure-devops azure-active-directory azure-functions azure-resource-manager

当前,我可以通过PowerShell(客户端ID为1950a258-227b-4e31-a9cf-717495945fc2)通过Azure资源管理器跨多个Azure AD租户管理资源,同时使用具有在Azure AD和所有AD中均具有较高特权的用户帐户使用委派权限相关订阅。也就是说,使用此用户登录的SubscriptionClient具有方法SubscriptionClient.tenants.List(),如果我可以以该用户身份访问不同租户中的订阅,该方法将列出多个租户。

我正在编写跨租户自动化,而我想做的是在Azure中拥有一个.NET功能来执行上述操作,而不会从某种跨租户God用户帐户(作为旧的-学校服务帐户)。相反,我想给函数本身权限。

  1. 首次尝试-使用MSI:

    我想实际上可以使服务原则存在于 服务原则的非本地租户将使用(可读性换行符):

    https://login.microsoftonline.com/{Target_foreign_tenant}/adminconsent
        ?client_id={id_of_msi}
        &redirect_uri=http://localhost/myapp/permissions
    

    使用管理员帐户并接受权限。这里的问题是 MSI需要Graph API权限才能起作用。所以我尝试了:

    New-AzureADServiceAppRoleAssignment `#`
        -ObjectId {object_id_of_msi} `#`
        -PrincipalId {object_id_of_msi} `#`
        -ResourceId {object_id_of_graph_SP} `#`
        -Id {permissions_id_of_relevant_graph_role}
    

    上面的命令以MSI的服务主体的全局管理员权限运行,给出了如下所示的“权限不足”错误。 (请注意,在应用程序注册的服务主体上,此方法工作正常,没有特权问题。)

    New-AzureADServiceAppRoleAssignment : Error occurred while executing NewServicePrincipalAppRoleAssignment
    Code: Authorization_RequestDenied
    Message: Insufficient privileges to complete the operation.
    HttpStatusCode: Forbidden
    HttpStatusDescription: Forbidden
    HttpResponseStatus: Completed
    At line:1 char:1
    + New-AzureADServiceAppRoleAssignment -Id {where id would be here}
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [New-AzureADServiceAppRoleAssignment], ApiException
        + FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.NewServicePrincipalAppRoleAssignment
    

    我正在关注:MSI Permissions for Graph API

  2. 所以我放弃了MSI,决定使用启用多租户的功能 Web App应用程序注册。我再次使用:

    https://login.microsoftonline.com/{Target_forgien_ADTenant}/adminconsent
        ?client_id={id_of_webappreg}
        &redirect_uri=http://localhost/myapp/permissions
    

    成功!我现在可以在我的外国租户中看到我的应用程序 在“企业应用”下注册,然后在 国外Azure AD租户并查看服务负责人– hurrah!

    然后,我很兴奋地将我的企业应用所有者权限分配给了 订阅我的外国租户,并运行了我列出的功能 顶部使用API​​的租户和订阅。不幸的是 不同于用户,只能在其家庭租户中看到订阅。

有关如何使这两种方法有效的任何帮助?我的功能如下:

public static async Task<SubscriptionClient> GetSubscriptionClient(string clientID, string clientSecret, string tenantName)
{
    var serviceCreds = new TokenCredentials(await GetRMAccessToken(clientID, clientSecret, tenantName).ConfigureAwait(false));

    return new SubscriptionClient(serviceCreds);
}

public static async Task<string> GetRMAccessToken(string clientID, string clientSecret, string tenantName)
{
    var authString = "https://login.microsoftonline.com/" + tenantName;
    var resourceUrl = "https://management.azure.com/";

    var authenticationContext = new AuthenticationContext(authString, false);
    var clientCred = new ClientCredential(clientID, clientSecret);
    var authenticationResult = await authenticationContext.AcquireTokenAsync(resourceUrl, clientCred);
    var token = authenticationResult.AccessToken;

    return token;
}

var subClient = await SubscriptionManagement.GetSubscriptionClient(clientID, clientSecret, tenantName);
var tenants = new List<string>();
foreach(var tenant in subClient.Tenants.List())
{
    tenants.Add(tenant.TenantId + " - " + tenant.TenantId);                
}

1 个答案:

答案 0 :(得分:1)

根据我的经验,似乎我们无法获得具有申请许可的所有租户。

我还使用启用了多租户的Web应用程序App对其进行了测试,与您获得的结果相同。

我用提琴手捕获了其余的API请求。它发送List tanents API。

GET https://management.azure.com/tenants?api-version=2016-06-01

rest api用于获取您的帐户的租户。

Gets the tenants for your account.