[英]Microsoft Graph API get multiple B2C user via email address
I have the use case to query multiple b2c user from the microsoft graph api to get the display name and get the information about the last login.我有一个用例,从 Microsoft Graph api 查询多个 b2c 用户以获取显示名称并获取有关上次登录的信息。 I am aware that the last login is just available via the beta route.
我知道最后一次登录只能通过测试路线获得。
I am using the microsoft graph api beta client and try to get a user via an email address.我正在使用 microsoft graph api beta 客户端并尝试通过 email 地址获取用户。
My b2c users do not have any mail or otherMail values, only information about the email is placed in the identities list.我的 b2c 用户没有任何邮件或其他邮件值,只有有关 email 的信息放在身份列表中。
var result = await client.Users
.Request()
.Select(e => new
{
e.DisplayName,
e.UserType,
e.OtherMails,
e.UserPrincipalName,
e.Mail,
e.Identities,
e.SignInActivity
}).GetAsync();
This Call returns all user, so I would have to filter in memory which would be bad.此调用返回所有用户,因此我必须在 memory 中进行过滤,这很糟糕。
.Filter("identities/any(id:id/issuer eq 'xxx.onmicrosoft.com' and id/issuerAssignedId eq 'superUser@mail.com')")
This filter function returns exactly one specific user, but I wasn't able to query multiple users via a single request.此过滤器 function 仅返回一个特定用户,但我无法通过单个请求查询多个用户。 Something like
.Filter("identities/any(id:id/issuer eq 'xxx.onmicrosoft.com' and id/issuerAssignedId eq 'superUser@mail.com') or identities/any(id:id/issuer eq 'xxx.onmicrosoft.com' and id/issuerAssignedId eq 'superUser2@mail.com')")
Return query is to complex an replace the 'eq' with an 'in' returns not supported query, because looks like lambda operators do not support 'in'.像
.Filter("identities/any(id:id/issuer eq 'xxx.onmicrosoft.com' and id/issuerAssignedId eq 'superUser@mail.com') or identities/any(id:id/issuer eq 'xxx.onmicrosoft.com' and id/issuerAssignedId eq 'superUser2@mail.com')")
返回查询是复杂的,将 'eq' 替换为 'in' 返回不支持的查询,因为看起来 lambda 运算符不支持 'in '.
Has someone an idea how to query for eg 2 emails addresses with an single request?有人知道如何通过单个请求查询例如 2 个电子邮件地址吗?
I also can provide an rosly pad script were you just have to set your specific values like client id, secret and so on.我还可以提供一个 rosly pad 脚本,您只需设置您的特定值,如客户端 ID、密码等。
#r "nuget:Microsoft.Graph.Auth/1.0.0-preview.7"
#r "nuget:Microsoft.Graph.Beta/4.28.0-preview"
#r "nuget:RestSharp/107.1.1"
#r "nuget:RestRequest/1.2.0"
#r "nuget:Microsoft.Azure.Services.AppAuthentication/1.6.2"
#r "nuget:Azure.Core/1.22.0"
#r "nuget:Azure.Identity/1.5.0"
using Microsoft.Graph;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Net.Http.Headers;
using Azure.Identity;
using System.Linq;
using Azure.Core;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System.Net.Http;
var client = await GetGraphApiClient();
var emails = new []{ "email1@example.de","email2@example.de","email3@example.de","email4@example.de","email5@example.de"};
// Build the batch
var batchRequestContent = new BatchRequestContent();
// Using AddBatchRequestStep adds each request as a step
foreach (var element in emails)
{
var userRequest2 = client.Users
.Request()
.Select(e => new
{
e.DisplayName,
e.UserType,
e.OtherMails,
e.UserPrincipalName,
e.Mail,
e.Identities,
e.SignInActivity // just provided in the Microsoft.Graph.Beta package
})
.Filter($"identities/any(id:id/issuer eq ' ' and id/issuerAssignedId eq '{element}')");
batchRequestContent.AddBatchRequestStep(userRequest2);
}
var returnedResponse = await client.Batch.Request().PostAsync(batchRequestContent);
try
{
var user = await returnedResponse
.GetResponsesAsync();
user.Dump();
}
catch (ServiceException ex)
{
Console.WriteLine($"Failed to get user: {ex.Error.Message}");
}
private static async Task<GraphServiceClient> GetGraphApiClient()
{
var clientId = "<Client-Id-Of-your-app-with-graph-access>";
var secret = "<Client-Secret-Of-your-app-with-graph-access>";
var tenant = "<tenant-id>";
string[] scopes = new string[] { "AuditLog.Read.All", "User.Read.All" };
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenant)
.WithClientSecret(secret)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
var serviceToken = await authProvider.ClientApplication.AcquireTokenForClient(new string[] { "https://graph.microsoft.com/.default" }).ExecuteAsync();
return new GraphServiceClient(authProvider);
}
Thanks Danstan,谢谢丹斯坦,
with the a batch request it works to get with single request up to 20 accounts at once.通过批量请求,它可以一次获得最多 20 个帐户的单个请求。 https://learn.microsoft.com/en-us/graph/sdks/batch-requests?tabs=csharp#simple-batching-example The API still limited it to 20 request in a single batch request.
https://learn.microsoft.com/en-us/graph/sdks/batch-requests?tabs=csharp#simple-batching-example API 仍然将其限制为单个批处理请求中的 20 个请求。 'Code: MaximumValueExceeded Message: Number of batch request steps exceeds the maximum value of 20.'
“代码:MaximumValueExceeded 消息:批处理请求步骤数超过最大值 20。”
This makes it possible to query all the data by view requests.这使得通过视图请求查询所有数据成为可能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.