简体   繁体   English

Microsoft Graph API 通过email地址获取多个B2C用户

[英]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.

相关问题 如何在没有Graph API的C#Azure B2C身份验证中获取当前登录用户的用户名和电子邮件地址? - How to get current login user's username & email address in C# Azure B2C authentication without Graph API? 在 C# 中通过 Microsoft Graph API 更新用户在 Email 地址中的登录 - Updating User's sign in Email Address via Microsoft Graph API in C# B2C Graph API 更新用户、UserPrincipalName 作为电子邮件:URL 不支持加号 (+) 登录电子邮件? - B2C Graph API Update User, UserPrincipalName as an email: URL does not support plus (+) sign in email? 如何从 Microsoft Graph API C# 中的 email 地址获取用户 ID - How to get user id from email address in Microsoft Graph API C# 如何使用 Azure Active Directory Graph Client 在 Azure B2C 中查找具有相同用户名\电子邮件地址的所有用户? - How to find all users with the same User name\email address in Azure B2C using Azure Active Directory Graph Client? 使用 B2C 图形 API 获取用户的 Azure 目录角色 - Get Azure directory role of the user using B2C graph API Azure AD B2C - 通过 REST 获取登录用户的缩略图 API - Azure AD B2C - Get thumbnail image for logged in user via REST API Microsoft graph 更改 azure ad b2c 用户密码不起作用 - Microsoft graph change azure ad b2c user password not working 使用 Microsoft Graph + Http 客户端更改 Azure AD B2C 上的用户密码 - Change user password on Azure AD B2C using Microsoft Graph + Http Client 如何在不使用Microsoft页面的情况下在Azure AD B2C中使用graph api进行社交登录? - How to use graph api for social login in Azure AD B2C without using Microsoft page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM