【问题标题】:Using GraphServiceClient to retrieve group info of Azure Ad members使用 GraphServiceClient 检索 Azure 广告成员的组信息
【发布时间】:2020-06-02 00:17:11
【问题描述】:

我想在我的后端应用程序中检索 Azure 广告组的所有成员。我遵循了herehere 的步骤,这是我的代码:

但是我在使用这个方法时总是会出现这个错误: Microsoft.Graph.ServiceException:代码:generalException 消息:一个 发送请求时出错。

---> Microsoft.Graph.Auth.AuthenticationException:代码: authenticationChallengeRequired 消息:身份验证挑战是 必填。

有人可以帮我吗?我没有发现这个特定的错误。

【问题讨论】:

    标签: asp.net authentication graph azure-active-directory microsoft-graph-api


    【解决方案1】:

    显然这些参数(clientId/tenantId/clientSecret/groupId)需要替换为特定的字符串。

    您可以通过应用注册->概览找到clientIdtenantId

    clientSecret 通过 应用注册-> 证书和机密:

    groupId 通过 Azure Active Directory -> 组:


    您还可以将特定字符串存储在配置文件中并读取文件中的字符串。 而sample 将帮助您理解它。

    AccountController.cs:

    IConfidentialClientApplication daemonClient;
                    daemonClient = ConfidentialClientApplicationBuilder.Create(Startup.clientId)
                        .WithAuthority(string.Format(AuthorityFormat, tenantId))
                        .WithRedirectUri(Startup.redirectUri)
                        .WithClientSecret(Startup.clientSecret)
                        .Build();
    

    Web.config:

      <add key="ida:ClientId" value="[Enter your client ID]" />
      <add key="ida:ClientSecret" value="[Enter your client secret]" />
    

    Startup.Auth.cs:

    public static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
    public static string clientSecret = ConfigurationManager.AppSettings["ida:ClientSecret"];
    public static string redirectUri = "https://localhost:44316/";
    

    【讨论】:

      【解决方案2】:

      您是否在 azure ad 中注册了应用程序,创建了秘密,设置了正确的 api 权限? 应用程序注册门户中甚至还有快速入门,可帮助您配置代码并为您提供可以试验的预设置项目。

      此外,您的代码似乎缺少范围,您需要为图形 api 令牌请求适当的范围以访问组。

      我认为这里的文档更好:https://github.com/microsoftgraph/msgraph-sdk-dotnet-auth

      如果您在此处查看单元测试以了解授权代码流:https://github.com/microsoftgraph/msgraph-sdk-dotnet-auth/blob/dev/tests/Microsoft.Graph.Auth.Test/ConfidentialClient/AuthorizationCodeProviderTests.cs

      为您提供了如何使其发挥作用的一个很好的例子。

      【讨论】:

      • 感谢您的建议!问题是我使用了 AuthorizationCodeProvider,它以用户的名义获取令牌,但在我的情况下,我需要使用 ClientCredentialProvider 在没有用户的情况下访问图表。然后,当没有用户通过身份验证并且它在秘密上工作时,我也需要读取组和成员的权限。
      猜你喜欢
      • 2015-01-06
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多