简体   繁体   English

使用C#中的Azure AD Graph API向已创建的应用程序添加角色

[英]Add role to already created application using Azure AD Graph API in C#

How to add roles in application that is already created on azure ad using Azure AD Graph API in c#. 如何使用c#中的Azure AD Graph API在已经在azure广告上创建的应用程序中添加角色。
I create role like this in c#: 我在c#中创建这样的角色:

 Guid _id = new Guid();

 AppRole appRole = new AppRole

    {
      AllowedMemberTypes = _AllowedMemberTypes,
      Description = "Admins can manage roles and perform all actions.",
      DisplayName = "Global Admin",
      Id = _id,
      IsEnabled = true,
      Value = "Admin"
    };  

What call will be used to add this new role in application using Azure AD Graph API. 将使用哪种调用在使用Azure AD Graph API的应用程序中添加此新角色。

Finally i was able to create a new role on azure using Azure Ad Graph API 最终,我能够使用Azure Ad Graph API在Azure上创建新角色

1) Create a Role: 1)创建角色:

Guid _id = Guid.NewGuid();
List<String> _AllowedMemberTypes = new List<string> {
    "User"
};
AppRole appRole = new AppRole
{
    AllowedMemberTypes = _AllowedMemberTypes,
    Description = "Admins can manage roles and perform all actions.",
    DisplayName = "Global Admin",
    Id = _id,
    IsEnabled = true,
    Value = "Admin"

};

2) Get Application in which role needed to be created: 2)获取需要在其中创建角色的应用程序:

IPagedCollection<IApplication> pagedCollection = await activeDirectoryClient.Applications.Where(x => x.AppId == AppclientId).ExecuteAsync();
var appObject = pagedCollection.CurrentPage.ToList().FirstOrDefault();  

3) Add Role to Applicationa and Update Application: 3)向Applicationa添加角色并更新应用程序:

 appObject.AppRoles.Add(appRole as AppRole);
 await appObject.UpdateAsync();

You could refer to the code as below to assign application role. 您可以参考以下代码来分配应用程序角色。

1.get access token 1.获取访问令牌

private static async Task<string> GetAppTokenAsync(string graphResourceId, string tenantId, string clientId, string secretKey)
        {
            string aadInstance = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";
            AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance, false);
            var result = await authenticationContext.AcquireTokenAsync(graphResourceId,
                new ClientCredential(clientId, userId));
            return result.AccessToken;
        }

2.Init the graphclient. 2.初始化graphclient。

var graphResourceId = "https://graph.windows.net";
var tenantId = "tenantId";
var clientId = "client Id";
var secretKey = "secret key";
var servicePointUri = new Uri(graphResourceId); 
var serviceRoot = new Uri(servicePointUri, tenantId);
var activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, async () => await GetAppTokenAsync(graphResourceId, tenantId, clientId, secretKey));

3.create role 3.创建角色

AppRole appRole = new AppRole
{
    Id = Guid.NewGuid(),
    IsEnabled = true,
    Description = "Admins can manage roles and perform all actions.",
    DisplayName = "Global Admin",
    Value = "Admin"
};

4.add role assginments 4.添加角色组合

User user = (User) activeDirectoryClient.Users.GetByObjectId("userobjectId").ExecuteAsync().Result;
AppRoleAssignment appRoleAssignment = new AppRoleAssignment
{
       Id = appRole.Id,
       ResourceId = Guid.Parse(newServicePrincpal.ObjectId),
       PrincipalType = "User",
       PrincipalId = Guid.Parse(user.ObjectId),

  };
user.AppRoleAssignments.Add(appRoleAssignment);
user.UpdateAsync().Wait();

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

相关问题 使用 Azure AD 并在 C# Windows 应用程序中获取应用程序角色 - Using Azure AD and Getting App Role in C# Windows Application Azure AD Graph API - 使用C#将用户分配给应用程序 - Azure AD Graph API - Assign user to applications using C# Azure AD - 在 C# Web API 中使用来自 Angular SPA 的图 API 访问令牌 - Azure AD - Using Graph API access token from Angular SPA in C# Web API azure 广告列表用户进入 combobox 图 api ZD7EFA19FBE7D3972FD4ADB6024223D7 - azure ad list users into combobox graph api C# 使用图形客户端 C# 在 Azure AD 中的另一个安全组中添加一个安全组 - Add a security Group in another Security Group in Azure AD using Graph Client C# C#上下文已经在跟踪实体,蔚蓝的活动目录图api。 添加组成员 - C# context is already tracking the entity, azure active directory graph api. add group member Azure AD应用程序 - 需要角色分配+为应用程序添加角色分配? - Azure AD Application - Require Role Assignment + Add a role assignment for an Application? 使用带有WPF应用程序的图形连接到Azure AD api - Connecting to Azure AD api using graph with an WPF application Azure AD Graph API将成员添加到组 - Azure AD Graph API add member to group 使用C#控制台应用程序查询Azure AD - Querying Azure AD using c# console application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM