我们正在使用B2C并将客户编号存储为用户的扩展字段.单个用户可以拥有一个或多个客户,并且它们存储在逗号分隔的字符串中.
我现在正在做的事情非常低效:1.获取所有用户2.获取每个用户的扩展属性3.检查他们是否具有所需的扩展属性以及是否包含我想要的客户.4.构建我想要的用户列表.
Adclient是IActiveDirectoryClient
var users = (await GetAllElementsInPagedCollection(await AdClient.Users.ExecuteAsync())).ToList();
var customersUsers = users.Where(user => user.AccountEnabled.HasValue && user.AccountEnabled.Value).Where(user =>
{
var extendedProperty = ((User) user).GetExtendedProperties().FirstOrDefault(extProp => extProp.Key == customersExtendedProperty.Name).Value?.ToString();
return extendedProperty != null && extendedProperty.Contains(customerId);
}).ToList();
Run Code Online (Sandbox Code Playgroud)
我希望能够使用AdClient在ActiveDirectory的一个查询中执行此操作.如果我尝试这个,我会得到错误,不支持这些方法,这是有道理的,因为我假设在后台构建查询以查询Active Directory.
编辑 - 其他信息:
我能够像这样查询Graph API:
var authContext = await ActiveDirectoryClientFactory.GetAuthenticationContext(AuthConfiguration.Tenant,
AuthConfiguration.GraphUrl, AuthConfiguration.ClientId, AuthConfiguration.ClientSecret);
var url = $"https://graph.windows.net:443/hansaborgb2c.onmicrosoft.com/users?api-version=1.6&$filter={customersExtendedProperty.Name} eq '{customerId}'";
var users = await _graphApiHttpService.GetAll<User>(url, authContext.AccessToken);
Run Code Online (Sandbox Code Playgroud)
但是,在我的示例中,我需要使用substringof进行过滤,但Azure Graph API不支持此功能.
我没有使用该库,但是我们使用Graph API进行了非常相似的搜索。我构造了一个过滤器,该过滤器将寻找与我要寻找的两个扩展属性值匹配的用户。过滤器如下所示:
var filter = $"$filter={idpExtensionAttribute} eq '{userType.ToString()}' and {emailExtensionAttribute} eq '{emailAddress}'";
Run Code Online (Sandbox Code Playgroud)
我们还使用了通过PowerShell对Graph API的REST调用,这将返回所需的用户。带有关联过滤器的URI如下所示:
https://graph.windows.net/$AzureADDomain/users?`$filter=extension_d2fbadd878984184ad5eab619d33d016_idp eq '$idp' and extension_d2fbadd878984184ad5eab619d33d016_email eq '$email'&api-version=1.6
Run Code Online (Sandbox Code Playgroud)
这两个选项都将返回符合过滤条件的所有用户。