Find a User by Email Address
Asked Answered
R

4

8

I'm trying find out if an email address is already taken in my Azure AD B2C directory.

var token = await this.GetTokenAsync();

var client = new HttpClient();

var id = HttpUtility.UrlEncode("adrian_mydomain.com#EXT#@xxxxxxxxx.onmicrosoft.com");
////var id = HttpUtility.UrlEncode("[email protected]"); // 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)?

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.

Rib answered 19/5, 2016 at 15:29 Comment(0)
T
5

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/

You will notice that the user is referenced by GUID or by UPN, not by email! Emails are in the collection 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

Start with the GetUsers(to get all users), then update password and last the filter.

Tang answered 26/5, 2016 at 8:59 Comment(4)
Thanks, I've not included it on my question, but I've tried using the user object and get no joy either. I can however successfully retrieve a list of all users.Rib
Can you add users? Most likely a problem with the json you sent, what is it like?Tang
Yes I can add users fine. I send exactly the content required and the user is created fine. I can also do a GET request for all users. But still cannot retrieve the details of a single user.Rib
Filters turned out to work fine, although some other parts of the API reference seem broken. Thanks for the help.Rib
K
7

Since it is a odata, you can query using odata syntax. Odata syntax here

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

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

$filter did the trick

queryString["$filter"] = "signInNames/any(x:x/value eq '[email protected]')";

Knighton answered 3/7, 2017 at 12:3 Comment(0)
T
5

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/

You will notice that the user is referenced by GUID or by UPN, not by email! Emails are in the collection 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

Start with the GetUsers(to get all users), then update password and last the filter.

Tang answered 26/5, 2016 at 8:59 Comment(4)
Thanks, I've not included it on my question, but I've tried using the user object and get no joy either. I can however successfully retrieve a list of all users.Rib
Can you add users? Most likely a problem with the json you sent, what is it like?Tang
Yes I can add users fine. I send exactly the content required and the user is created fine. I can also do a GET request for all users. But still cannot retrieve the details of a single user.Rib
Filters turned out to work fine, although some other parts of the API reference seem broken. Thanks for the help.Rib
M
5

signInNames isn't the only place that emails are stored. It could also be userPrincipalName or 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}')

Munshi answered 15/5, 2019 at 22:56 Comment(0)
J
0

I'm also trying to find a user by their login/email address.

Here's my (obfuscated XXXX) query string:

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

It doesn't error, but doesn't find the user (whom I know to exist, because GetAllUsers finds it).

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?

Jonellejones answered 30/8, 2018 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.