Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure AD图形api返回禁止_C#_Azure_Azure Active Directory_Azure Ad Graph Api - Fatal编程技术网

C# Azure AD图形api返回禁止

C# Azure AD图形api返回禁止,c#,azure,azure-active-directory,azure-ad-graph-api,C#,Azure,Azure Active Directory,Azure Ad Graph Api,当我想从应用程序中获取graph api的成员时 graph.windows.net/{aadDirectoryId}/users/{userId.Value}/$links/memberOf?api版本=1.6 我总是很紧张 Response: StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Pragma: no-cach

当我想从应用程序中获取graph api的成员时 graph.windows.net/{aadDirectoryId}/users/{userId.Value}/$links/memberOf?api版本=1.6

我总是很紧张

Response: StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Pragma: no-cache
  ocp-aad-diagnostics-server-name: aVvd1R49Sg=
  request-id: 67105ddc-2b5f-84bf-7ec43a4d3117
  client-request-id: fb1ef66f-451357f08975abd4
  x-ms-dirapi-data-contract-version: 1.6
  ocp-aad-session-key: _XjEM7ooA1Emw_l6FjiyMwKqtoEPSWgxw-04c_nX785foVv6fGM_lBejApG_gJW2fXC_LBNrZRJRryuBIOO7_O1bF2oEEiWMvnW9Ywx71OP0NJ5gRyZDGlLyNsjmsDvu.42WXAH4v8FjbaSNvNtH1Nnkm3z5on0J5ZsptMguA52A
  DataServiceVersion: 3.0;
  Strict-Transport-Security: max-age=31536000; includeSubDomains
  Access-Control-Allow-Origin: *
  Duration: 853533
  Cache-Control: no-cache
  Date: Tue, 05 Mar 2019 14:01:17 GMT
  Server: Microsoft-IIS/10.0
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Length: 219
  Content-Type: application/json; odata=minimalmetadata; streaming=true; charset=utf-8
  Expires: -1
} 
当我打电话给你时,一切都好

在azure AD中,我已为设置权限

电话号码:

private static List<string> GetGroupsFromGraphAPI(ClaimsIdentity claimsIdentity)
        {
            _logger.Info($"Getting claims from Graph API for {claimsIdentity.Name}.");

            List<string> groupObjectIds = new List<string>();

            var aadClientId = ConfigurationManager.AppSettings["ida:ClientId"];
            var aadSecret = ConfigurationManager.AppSettings["ida:ClientSecret"];
            var aadDirectoryId = ConfigurationManager.AppSettings["ida:DirectoryId"];

            ClientCredential credential = new ClientCredential(aadClientId, aadSecret);
            AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/" + aadDirectoryId);
            string accessToken;
            try
            {
                _logger.Info($"Client ID: {aadClientId}");
                _logger.Info($"Secret: {aadSecret}");
                _logger.Info($"Directory id: {aadDirectoryId}");

                var token = authContext.AcquireToken("https://graph.windows.net", credential);
                _logger.Info($"Token: {token.ToString()}");
                accessToken = token.AccessToken;
                _logger.Info($"Get access token {accessToken}");
            }
            catch
            {
                _logger.Error("Cannot aquire token for Graph API.");
                throw;
            }

            var userId = claimsIdentity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier");
            if (userId == null)
            {
                _logger.Warn($"No user ID to get group membership for. ({claimsIdentity.Name})");
                return groupObjectIds;
            }

            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            HttpResponseMessage response;
            try
            {
                var link =
                    $"https://graph.windows.net/{aadDirectoryId}/users/{userId.Value}/$links/memberOf?api-version=1.6";
                _logger.Info($"GetAsync {link}");
                response = client.GetAsync(link).Result;
            }
            catch
            {
                _logger.Error("Failed to load group membership for " + claimsIdentity.Name);
                throw;
            }
}
private static List GetGroupsFromGraphAPI(ClaimsIdentity ClaimsIdentity)
{
_Info($“正在从图形API获取{claimsIdentity.Name}的声明”;
List GroupObjectId=新列表();
var aadClientId=ConfigurationManager.AppSettings[“ida:ClientId”];
var aadSecret=ConfigurationManager.AppSettings[“ida:ClientSecret”];
var aadDirectoryId=ConfigurationManager.AppSettings[“ida:DirectoryId”];
ClientCredential=新的ClientCredential(aadClientId,aadSecret);
AuthenticationContext authContext=新的AuthenticationContext(“https://login.microsoftonline.com/“+aadDirectoryId);
字符串访问令牌;
尝试
{
_Info($“客户端ID:{aadClientId}”);
_logger.Info($“Secret:{aadSecret}”);
_Info($“目录id:{aadDirectoryId}”);
var token=authContext.AcquireToken(“https://graph.windows.net“,凭证);
_logger.Info($“令牌:{Token.ToString()}”);
accessToken=token.accessToken;
_Info($“获取访问令牌{accessToken}”);
}
抓住
{
_logger.Error(“无法获取图形API的标记”);
投掷;
}
var userId=claimsIdentity.FindFirst(“http://schemas.microsoft.com/identity/claims/objectidentifier");
if(userId==null)
{
_logger.Warn($“没有用户ID来获取组成员身份({claimsIdentity.Name})”);
返回groupobjectid;
}
HttpClient=新的HttpClient();
client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“承载者”,accessToken);
HttpResponseMessage响应;
尝试
{
var链路=
$"https://graph.windows.net/{aadDirectoryId}/users/{userId.Value}/$links/memberOf?api version=1.6”;
_Info($“GetAsync{link}”);
response=client.GetAsync(link.Result);
}
抓住
{
_logger.Error(“未能加载“+claimsIdentity.Name”的组成员资格);
投掷;
}
}
  • 关于
    禁止的推理
    错误和需要管理员同意

    查看您为获取令牌而共享的代码,您正在使用应用程序标识,因此应用程序所需的权限将是应用程序权限

        ClientCredential credential = new ClientCredential(aadClientId, aadSecret);
        AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/" + aadDirectoryId);  
        ...
        var token = authContext.AcquireToken("https://graph.windows.net", credential);
    

    在您已附加的
    目录的屏幕截图中,已选择Azure AD Graph的所有
    权限,但它还表示未完成管理员同意。如果你看一下许可,它清楚地说,需要管理员同意是的

    所以,您的解决方案将是授予所需权限的管理员同意。如果您以管理员身份登录,则可以直接从Azure Portal(您已分配权限的同一页面)执行此操作。另一种方法是使用AdminApprove端点

  • https://graphexplorer.azurewebsites.net/

    Azure AD Graph Explorer正在使用
    委托权限
    ,并以登录用户的身份调用API,以便为您工作。当您试图从应用程序中执行相同的操作时,不同之处在于您使用的是尚未获得许可的应用程序标识进行调用

  • 在API权限下,只需要Azure AD Graph API

    在您共享的代码中,您只调用
    https://graph.windows.net
    因此您的应用程序只需要获得Azure AD Graph API
    的权限。您可以安全地删除为
    Microsoft Graph API
    分配的权限(除非您在应用程序的其他地方使用Microsoft Graph API)


  • 包括调用代码..@Patrik您在调用API时使用了哪些代码流?您试图通过使用clientid和clientsecret获取令牌来利用应用程序权限。。我从您所附的屏幕截图中看到,很可能未获得管理员同意<代码>目录。读取。所有
    权限都需要管理员同意。。因此,如果您在页面底部看到您的目录,请点击
    授予管理员许可。。如果您不是管理员,则意味着您不是管理员,需要请求管理员为您授予许可。。通过门户或使用AdminApproveendpoint@RohitSaigal,如果没有获得管理员同意,它在GrapherXPlorer中是如何工作的?@Matt.G和Patrik这是一个好问题。。答案是,本例中的Azure AD Graph Explorer使用的是
    委派权限。。i、 e.用户的权限,因此不同。