在Azure AD中为用户创建分配自定义角色

时间:2016-05-12 13:33:18

标签: azure

Azure允许您通过修改清单来定义AD租户的自定义角色:

 "appRoles": [
{
  "allowedMemberTypes": [
    "User"
  ],
  "description": "Admins can manage roles and perform all actions.",
  "displayName": "Global Admin",
  "id": "some GUID",
  "isEnabled": true,
  "value": "Admin"
}, ... ],

并且,根据msdn azure graph api reference,您可以使用图形api以编程方式将用户分配给自定义角色。但是我在创建新用户并尝试使用AppRoleAssignment分配角色时无法正常工作。我认为问题在于我需要定义用户的ID,但是一旦创建了用户,就会由Azure定义,这也意味着在创建时将用户分配给角色是不可能的,而且只能完成一旦用户被添加到目录中?下面是我用来分配角色的代码片段。

var appRoleAssignment = new AppRoleAssignment();
            appRoleAssignment.Id = Guid.Parse("some-guid-here");
            appRoleAssignment.ResourceId = Guid.Parse(clientId);
            appRoleAssignment.PrincipalId = ??? // what should go here for a user that hasn't been created yet ?

3 个答案:

答案 0 :(得分:1)

清除appRoleAssignments,appRoleAssignedTo将让我们了解用户角色,应用角色,用户,应用程序的关系。

1)可以将应用程序分配给用户应用程序角色。打开 名为" ADlinglitest"的应用程序的用户选项: enter image description here

致电:https://graph.windows.net/Tenantid/users/lingliad01@lingliad.onmicrosoft.com/appRoleAssignments?api-version=1.5获取结果: enter image description here

致电:https://graph.windows.net/tenantid/servicePrincipals/servicePrincipalsobjectid/appRoleAssignedTo?api-version=1.5,您将看到3个已分配的用户。

enter image description here

2)可以将用户分配给应用程序作为用户角色,组,应用程序也可以作为角色分配给应用程序。

"appRoles": [
{
"allowedMemberTypes": [
    "User"
  ],
  "description": "Admins can manage roles and perform all actions.",
  "displayName": "Global Admin",
  "id": "7c93906c-71fa-4e5b-b1da-21b073d298fd",
  "isEnabled": true,
  "value": "Admin"
}
]

致电:https://graph.windows.net/tanantid/servicePrincipals/serviceprincipalobjectid/appRoles?api-version=1.5获取结果: enter image description here

如果您不了解objectid,请致电https://graph.windows.net/tanantid/servicePrincipals?api-version=1.5以获取您的申请的SP目标。

3)如您所说"用户角色分配",实际上缺少方法userRoleAssignedTo,如果用户被分配给应用程序作为用户角色,我们可以通过{查询应用程序infor {1}}。 这确实很容易让人感到困惑。

答案 1 :(得分:0)

如果没有标识符,则无法分配角色。创建用户时会创建对象和标识符。

答案 2 :(得分:0)

我找不到关于在Azure官方文档中将用户分配给应用程序角色的C#代码。我只能看到Powershell和Rest api方法。

Powershell的:

New-AzureRmRoleAssignment -ObjectId <application id> -RoleDefinitionName <role name in quotes> -Scope <subscription id>

REST API:

PATCH - https://graph.windows.net/tenantid/application/clientid?api-version=1.5

BODY应该喜欢这个,id是用户的目标:

{
  “appRoles”: [

    {     

  “allowedMemberTypes”: [       
  “User”, “Application”     
  ],     
  “description”: “Dentists may create, update and delete patient’s health records”,   
  “displayName”: “Dentist”,     
  “id”: “31952ade-a03c-414c-8340-2f11e9099106″,     
  “isEnabled”: true,     
  “value”: “dentist”   

     }
]

在C#示例中使用api:

    AuthenticationContext ac =
                    new AuthenticationContext("https://login.microsoftonline.com/tenantid");
                AuthenticationResult ar =
                    ac.AcquireToken("https://graph.windows.net",
                                    new ClientCredential(Constants.ClientId, Constants.ClientSecret));

                string result = string.Empty;
                HttpClient httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", ar.AccessToken);
                HttpResponseMessage response =
                    httpClient.PostAsync("https://graph.windows.net/tenantid/application/clientid?api-version=1.5","json/body").Result;
if (response.IsSuccessStatusCode)
                {
                    result = response.Content.ReadAsStringAsync().Result;
                }
                Console.WriteLine(result);
                Console.ReadLine();
相关问题