简体   繁体   English

如何使用C#中的Azure Active Directory Graph客户端创建角色分配?

[英]How to create role assignments using Azure Active Directory Graph client in C#?

I'm using this library : Microsoft.Azure.ActiveDirectory.GraphClient class: ActiveDirectoryClient . 我正在使用此库: Microsoft.Azure.ActiveDirectory.GraphClient类: ActiveDirectoryClient

I'd like to give an Application (I have the appID) "Owner" access to some subscription. 我想给一个应用程序(我有appID)“所有者”访问某些订阅。 How would I go about doing that? 我将如何去做? Thanks 谢谢

The whole premise of this question is incorrect. 这个问题的全部前提是不正确的。 The GraphClient is not the right client to manage such authorizations. GraphClient不是管理此类授权的正确客户端。 The proper API library for that is Microsoft.Azure.Management.Authorization and the class AuthorizationManagementClient . 正确的API库是Microsoft.Azure.Management.Authorization和类AuthorizationManagementClient

I will post additional notes on the actual sequence of calls. 我将在实际的通话顺序中添加其他注释。

*** Update *********** ***更新************

As promised here's the sample code: 如所承诺的,这里是示例代码:

    public static async Task<IServicePrincipal> GetServicePrincipalAsync(string accessToken, string tenantId, string clientId)
    {
        var graphClient = NewActiveDirectoryClient(accessToken, tenantId);
        var matches = await graphClient.ServicePrincipals.Where(sp => sp.AppId == clientId).ExecuteAsync();
        return matches.CurrentPage.ToList().FirstOrDefault();
    }
    private static ActiveDirectoryClient NewActiveDirectoryClient(string accessToken, string tenantId)
    {
        TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
        tcs.SetResult(accessToken);
        return new ActiveDirectoryClient(
            new Uri($"{GraphApiBaseUrl}{tenantId}"),
            async () => { return await tcs.Task; });
    }

First you need to get the ObjectId of the principal you want to add. 首先,您需要获取要添加的主体的ObjectId。 In the case of ServicePricipal I have a function that gets it from the directory like so: 对于ServicePricipal,我有一个从目录中获取它的函数,如下所示:

Then using that and a scope ("/subscriptions/{my_subscription_id}", for the entire subscription) you can create a RoleAssignment: 然后,使用它和一个范围(对于整个订阅,为“ / subscriptions / {my_subscription_id}”),您可以创建RoleAssignment:

    public static async Task AssignRoleToPrincipalAsync(
        string accessToken, 
        string subscriptionId, 
        string scope, 
        string roleName,
        string principalObjectId)
    {
        using (var client = NewAuthorizationManagementClient(accessToken, subscriptionId))
        {
            RoleDefinition roleDef = (await FindRoleDefinitionAsync(accessToken, subscriptionId, scope, roleName)).FirstOrDefault();
            if (roleDef == null)
                throw new Exception($"Role was not found: {roleName}");
            var props = new RoleAssignmentProperties()
            {
                PrincipalId = principalObjectId,
                RoleDefinitionId = roleDef.Id
            };
            await client.RoleAssignments.CreateAsync(scope, Guid.NewGuid().ToString("N"), props);
        }
    }

    private static AuthorizationManagementClient NewAuthorizationManagementClient(string accessToken, string subscriptionId)
    {
        return new AuthorizationManagementClient(new TokenCredentials(accessToken)) { SubscriptionId = subscriptionId};
    }

暂无
暂无

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

相关问题 如何使用图谱API在Azure活动目录中创建自定义角色? - How to create custom role in azure active directory using graph api? C#控制台应用程序,用于使用Microsoft Graph在Azure Active Directory中创建用户 - C# Console App to Create User in Azure Active Directory using Microsoft Graph Active Directory图客户端:使用C#API删除应用程序 - Active Directory Graph Client: Delete an Application using the C# API 如何使用 Azure Active Directory Graph Client 在 Azure B2C 中查找具有相同用户名\电子邮件地址的所有用户? - How to find all users with the same User name\email address in Azure B2C using Azure Active Directory Graph Client? 使用Azure AD Graph Client获取Active Directory管理员 - Get Active Directory Administrators using Azure AD Graph Client 如何使用图形服务客户端根据 azure 活动目录中的组 ID 获取组名 - How to get group names based on group id in azure active directory using graph service client 使用http post请求使用Graph API在Azure Active Directory(B2C)中创建新用户 - Create a new user in Azure Active Directory (B2C) with Graph API, using http post request 使用图API API在Azure Active Directory中创建应用程序失败 - Create application in Azure Active Directory using graph API fails Azure Active Directory C# - Azure Active Directory C# 如何使用图形API在Azure活动目录中添加外部用户 - How to add the external user in azure active directory using graph api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM