简体   繁体   English

通过电子邮件地址查找用户

[英]Find a User by Email Address

I'm trying find out if an email address is already taken in my Azure AD B2C directory.我正在尝试确定一个电子邮件地址是否已在我的 Azure AD B2C 目录中使用。

var token = await this.GetTokenAsync();

var client = new HttpClient();

var id = HttpUtility.UrlEncode("adrian_mydomain.com#EXT#@xxxxxxxxx.onmicrosoft.com");
////var id = HttpUtility.UrlEncode("adrian@mydomain.com"); // This also fails.
////var id = HttpUtility.UrlEncode("adrian_mydomain.com#EXT#"); // This also fails.
////var id = "xxxx-xxxx-xxxxxxxx-xxxxxxxxxx"; // This also fails (user object id).

var resource = $"{this.graphConfig.GraphUri}/{this.graphConfig.Tenant}/users/{id}?api-version=1.6";
//// This line below works, it returns all the users, so I do know the token is good and the resource URI is valid, etc.
////var resource = $"{this.graphConfig.GraphUri}/{this.graphConfig.Tenant}/users?api-version=1.6";

var request = new HttpRequestMessage(HttpMethod.Get, resource);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();

I'm encoding my email address in the same way that I see my email address encoded when I get all users.我对我的电子邮件地址进行编码,就像我在获取所有用户时看到我的电子邮件地址编码一样。 I have a feeling I'm close, if it is even possible to query by email address.如果甚至可以通过电子邮件地址查询,我有一种感觉我很接近。

Currently all the things I've tried either return a 400 or a 404. Does anyone know if there is a way to query by email address (sign in name)?目前,我尝试过的所有事情都返回 400 或 404。有谁知道是否可以通过电子邮件地址(登录名)进行查询?

EDIT编辑

On a similar theme, I'm also trying a query to change a user's password to no avail.在类似的主题上,我还尝试查询以更改用户密码,但无济于事。 I figure if I can get the query working for one, I can get it working on the other.我想如果我可以让查询为一个工作,我可以让它在另一个上工作。

Since it is a odata, you can query using odata syntax.由于是 odata,因此可以使用 odata 语法进行查询。 Odata syntax here 此处的Odata 语法

var queryString = HttpUtility.ParseQueryString(string.Empty);
queryString["api-version"] = "1.6";
queryString["$filter"] = "signInNames/any(x:x/value eq 'paule@test.in')";

string url = "https://graph.windows.net/" + tenant + "/users"+ "?" + queryString;

$filter did the trick $filter 做到了

queryString["$filter"] = "signInNames/any(x:x/value eq 'paule@test.co.uk')"; queryString["$filter"] = "signInNames/any(x:x/value eq 'paule@test.co.uk')";

Take a look at the B2C.exe implementation, first get that working: https://azure.microsoft.com/nl-nl/documentation/articles/active-directory-b2c-devquickstarts-graph-dotnet/看看 B2C.exe 实现,首先让它工作: https ://azure.microsoft.com/nl-nl/documentation/articles/active-directory-b2c-devquickstarts-graph-dotnet/

You will notice that the user is referenced by GUID or by UPN , not by email!您会注意到用户是通过GUIDUPN引用的,而不是通过电子邮件! Emails are in the collection signInNames电子邮件位于signInNames集合中

To query on email address, you will need to specify a filter: https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/users-operations#GetUsers要查询电子邮件地址,您需要指定一个过滤器: https : //msdn.microsoft.com/en-us/library/azure/ad/graph/api/users-operations#GetUsers

Start with the GetUsers(to get all users), then update password and last the filter.从 GetUsers(获取所有用户)开始,然后更新密码并最后过滤。

signInNames isn't the only place that emails are stored. signInNames 不是唯一存储电子邮件的地方。 It could also be userPrincipalName or otherMails.它也可以是 userPrincipalName 或 otherMails。 You'll want to use the following query to search all possible fields for an email.您需要使用以下查询来搜索电子邮件的所有可能字段。

/users?api-version=1.6&$filter=otherMails/any(x:x eq '{email}') or userPrincipalName eq '{email}' or signInNames/any(x:x/value eq '{email}')

I'm also trying to find a user by their login/email address.我也在尝试通过用户的登录名/电子邮件地址来查找用户。

Here's my (obfuscated XXXX) query string:这是我的(混淆的 XXXX)查询字符串:

"https://graph.windows.net/XXXX.onmicrosoft.com/users?api-version=1.6&$filter=signInNames/any(x: x/value eq 'dd.rrr@XXXX.com')"

It doesn't error, but doesn't find the user (whom I know to exist, because GetAllUsers finds it).它没有错误,但没有找到用户(我知道他存在,因为 GetAllUsers 找到了它)。

However, looking at the user details, I can see:但是,查看用户详细信息,我可以看到:

"showInAddressList": null,
"signInNames": [],
"sipProxyAddress": null,

Could this be a clue as to why search doesn't work?这可能是为什么搜索不起作用的线索吗?

How can a user NOT have a signInName ?用户怎么可能没有signInName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM