Azure AD应用程序使用客户端凭据将用户添加到Active Directory

时间:2018-12-06 10:41:50

标签: .net-core azure-active-directory microsoft-graph

我试图将用户添加到应用程序Active Directory中,但收效甚微。我正在将GraphServiceClient与.Net Core一起使用

这里的文档说我需要这些权限

Azure AD Graph Client Beta Docs

  

应用程序目录.ReadWrite.All

但是我找不到在Azure门户中可以分配此权限的地方。

enter image description here

上面的代码是GraphServiceClient目前处于beta版本,而它还不是API的一部分,所以我要手动调用该请求。

下面是我的身份验证代码,我正在使用针对AD中的应用程序设置的应用程序客户端密码。我可以很好地读取目录数据。

public async Task AuthenticateRequestAsync(HttpRequestMessage request)
        {
            try
            {
                if (null == _configuration)
                {
                    throw new InvalidOperationException("Azure AD Configuration is not set");
                }

                var authContext = new AuthenticationContext(
                    $"{_configuration.Instance}/{_configuration.Domain}", false);

                var credentials = new ClientCredential(_configuration.ClientId, _configuration.ClientSecret);

                var authResult =
                    await authContext.AcquireTokenAsync("https://graph.microsoft.com/", credentials);

                request.Headers.Add("Authorization", "Bearer " + authResult.AccessToken);
            }
            catch (Exception ex)
            {
                _logger.Error("Authentication Provider, unable to get token", ex);
            }
        }

更新-查看Rohit的建议后,您可以看到我已设置权限。但是请注意,它们旁边都是蓝色的刻度线!我已经更改并保存了,可以看到保存按钮被禁用了。我单击了授予权限。这相关吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

  

但是我找不到在Azure门户中可以分配的位置   许可。

在Azure门户中,导航到Azure Active Directory>应用程序注册>您的特定应用程序>设置>所需权限

enter image description here

点击添加并选择Microsoft图形

enter image description here

现在,在“应用程序权限”部分中,选中“读取和写入目录数据”

enter image description here

完成后,请获得管理员许可的“授予权限”,因为此权限需要它。

相关问题