How to get the logged in user details(user email id and user name) using Access Token in AAD Single Sign on using java script back-end?

一个人想着一个人 提交于 2019-12-22 12:25:21

问题


Hi am developing a windows store 8.1 app using C# and xaml

For log in, am authenticating the user with Windows Azure Active directory Single Sign-on using JavaScript back-end.

Once the user is logged in and i have the access token, how to get the logged in user's user email id and Username using the access token in the app?

Anybody please provide me a solution to get the user email using the access token?


回答1:


If you have the access token then that should contain the user id value. For retrieving the e-mail address you have to query the Graph API to get user details. The full documentation on that is here but in short you should make a get request like below, placing the AccessToken in the Authorization header, after "Bearer ".

GET https://graph.windows.net/contoso.onmicrosoft.com/users/Alex@contoso.onmicrosoft.com?api-version=2013-04-05 HTTP/1.1
Authorization: Bearer eyJ0eX ... FWSXfwtQ
Content-Type: application/json

You can use either user principal name or objectId in the address. Better yet, you can use the Azure AD Graph Client nuget package and call their API to get user information.

Uri servicePointUri = new Uri("https://graph.windows.net");
Uri serviceRoot = new Uri(servicePointUri, "contoso.onmicrosoft.com");
ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, accessToken);
IUser user = activeDirectoryClient.Users
    .Where(user => user.UserPrincipalName.Equals("alex@contoso.onmicrosoft.com"))
    .ExecuteAsync().Result.CurrentPage.ToList().SingleOrDefault();

See here for the full sample.



来源:https://stackoverflow.com/questions/27226277/how-to-get-the-logged-in-user-detailsuser-email-id-and-user-name-using-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!