Let's walk through it!
I have a tenant: shawntest.onmicrosoft.com
I created a new Guest User in this tenant using the Azure Portal:
I clicked the "+ New guest user" button, and created the guest Gmail account you can see above. I have additionally added this user to a few groups, also in the portal:
So let's sign this user in.
First, it is important in these situations to specify the Tenant where you want to sign the user in. You should probably NOT use the common
endpoint for these kinds of accounts, since they may be associated with multiple tenants, and then you leave it up to AAD to make the final determination for you. Instead, specify a specific authentication context with a specific tenant id:
https://login.microsoftonline.com/shawntest.onmicrosoft.com
Now get a token using your preferred user login flow. I am using the Native Client flow, which can be easily simulated with just 13 lines of PowerShell.
Once I have my token, I call the /me
endpoint to show evidence that I am working with an external user:
GET https://graph.windows.net/me?api-version=1.6
{
"odata.metadata": "https://graph.windows.net/myorganization/$metadata#directoryObjects/Microsoft.DirectoryServices.User/@Element",
"odata.type": "Microsoft.DirectoryServices.User",
"objectType": "User",
...
"displayName": "Shawn Tabrizi",
...
"userPrincipalName": "xxxxxx_gmail.com#EXT#@shawntest.onmicrosoft.com",
"userType": "Guest"
}
Looks good to me. Now let's get the groups that user is a member of, using the memberOf
query. Note that you can do this query right away, it is NOT dependent on any data from the last query.
GET https://graph.windows.net/me/memberOf?api-version=1.6
{
"odata.metadata": "https://graph.windows.net/myorganization/$metadata#directoryObjects",
"value": [{
"odata.type": "Microsoft.DirectoryServices.Group",
"objectType": "Group",
...
"displayName": "TestGroup",
...
"securityEnabled": true
}, {
"odata.type": "Microsoft.DirectoryServices.Group",
"objectType": "Group",
...
"displayName": "MyTestGroup",
...
"securityEnabled": true
}]
}
And that's all folks!