4

尝试使用 Microsoft Graph API 来处理在我们的 MS 云中查找数据 - 我被困住了。有这么多的文档 - 但从来没有一个正确的....

我正在尝试使用 C# Graphi 客户端 SDK,而我正在尝试做的是读取给定用户的详细信息,包括其 AD 组成员身份。

我已经在 Azure AD 中注册了我的应用程序,并且我也能够IPublicClientApplication启动并运行和身份验证工作:

IPublicClientApplication app = PublicClientApplicationBuilder
                                       .Create(clientId)
                                       .WithTenantId(tenantId)
                                       .Build();

UsernamePasswordProvider authProvider = new UsernamePasswordProvider(app, scopes);

// creating Graph SDK client 
GraphServiceClient graphClient = new GraphServiceClient(authProvider);

string userName = "......";
var securePassword = new SecureString();

// filling secure password here.....

var users = graphClient.Users
                       .Request()
                       .WithUsernamePassword(userName, securePassword)
                       .Filter("userPrincipalName eq 'someone@myorg.com'")
                       .GetAsync().Result;

这行得通 - 我确实取回了有关过滤器中指定的用户的基本用户详细信息。

两件事情:

  1. 我不喜欢我必须在.WithUsernamePassword每次呼叫客户时添加看似的事实 - 有没有办法graphClient 一次性包含该信息并完成它,直到我注销?

  2. 我正在尝试获得组成员身份。我可以通过/MemberOf在我的查询字符串中添加一个来在 Graph Explorer 中执行此操作 - 但我无法让它在 Graph SDK 客户端场景中工作。

我看到很多博客展示了如何使用当前登录的用户

graphClient.Me.MemberOf.Request().GetAsync();

但我不想要我的组成员身份 - 我想要我在搜索过滤器中指定的用户,如上所示。

尝试简单地添加.Expand("memberOf")似乎没有帮助 - 返回的用户对象在其MemberOf属性中仍然没有值。

我错过了什么?我不敢相信这会如此棘手和困难?还是我真的需要求助于针对 REST API 发出 HTTP GET 请求?如果 MS 提供 SDK 和客户端代码,这似乎很奇怪......坦率地说,我更喜欢使用它。

4

1 回答 1

0

要获得特定用户的会员资格,您可以拨打这样的电话

await client.Users["username@domain.com"].MemberOf.Request().GetAsync();

您可以遍历用户列表并将username@domain.com上面代码段中的 替换为user.UserPrincipalName属性。

您还可以使用不同的提供程序,以便您只提供一次凭据,并将其用于应用程序的整个生命周期。例如,下面的代码使用InteractiveAuthenticationProvider将创建一个浏览器弹出窗口,您将登录一次,您的凭据将用于应用程序中的其余请求。

IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
    .Create(clientId)
    .WithRedirectUri("http://localhost:1256")
    .Build();

InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(publicClientApplication, scopes);

GraphServiceClient client = new GraphServiceClient(authProvider);

var users = await client.Users.Request().GetAsync();

foreach (var user in users)
{
    var result = await client.Users[user.UserPrincipalName].MemberOf.Request().GetAsync();
}

您可以根据您的场景将此页面用作一堆身份验证提供程序的参考。 https://docs.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=CS

于 2021-03-23T09:33:37.680 回答