首页 > 解决方案 > 如何根据自定义用户属性过滤 azure b2c 中的用户?

问题描述

customerId在 Azure B2C 用户属性中创建了自定义用户属性,以区分特定客户的用户。我可以使用Graph API和 .net B2C sdk 创建用户,并使用这个 git hub示例设置 customerId

我遇到的问题是我无法按 customerId 过滤客户。我的代码如下所示

    public static async Task<List<User>> GetAllB2CUsersByCustomerId(GraphServiceClient graphClient, int customerId)
    {

        try
        {
            // Get users by customerId
            var result = await graphClient.Users
                .Request()
                .Filter($"additionalData/any(c:c/key eq 'extension_b2cApplicationIdWithoutDashes_customerId' and c/value eq '{customerId}')")
                .Select(e => new
                {
                    e.DisplayName,
                    e.Id,
                    e.Identities
                })
                .GetAsync();

            if (result != null)
            {
                return result.ToList();
            }
        }
        catch (Exception ex)
        {
            // catch exception

        }
        return null;
    }

当代码运行时出现以下异常

Code: BadRequest
Message: Filter not supported.
Inner error:
    AdditionalData:
    request-id: 21d0c9d3-7d6a-4c97-9066-c99f678aec54
    date: 2020-06-10T15:59:37
ClientRequestId: 21d0c9d3-7d6a-4c97-9066-c99f678aec54

标签: azure-ad-b2cazure-ad-graph-api

解决方案


只是如果它对某人有帮助,以下语法过滤器上的自定义属性

public static async Task<List<User>> GetAllB2CUsersByCustomerId(GraphServiceClient graphClient, int customerId)
{

    try
    {
        // Get users by customerId
        var result = await graphClient.Users
            .Request()
            .Filter($"extension_b2cApplicationIdWithoutDashes_customerId eq {customerId}")
            .Select(e => new
            {
                e.DisplayName,
                e.Id,
                e.Identities
            })
            .GetAsync();

        if (result != null)
        {
            return result.ToList();
        }
    }
    catch (Exception ex)
    {
        // catch exception

    }
    return null;
}

推荐阅读