java后端判断用户名,如何使用Java脚本后端在AAD Single Sign On中使用访问令牌获取登录的用户详细信息(用户电子邮件ID和用户名)?...

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值