简体   繁体   English

如何从访问令牌中获取用户详细信息

[英]How to get user details from access token

I downloaded this sample: https://github.com/Microsoft/PowerBI-Developer-Samples/tree/master/User%20Owns%20Data/integrate-report-web-app/PBIWebApp我下载了这个示例: https : //github.com/Microsoft/PowerBI-Developer-Samples/tree/master/User%20Owns%20Data/integrate-report-web-app/PBIWebApp

and I am able to get access token.我能够获得访问令牌。 Next, I saved this access token in my database for future use.接下来,我将此访问令牌保存在我的数据库中以备将来使用。 Using this token stored in database I want to get user's profile but not sure how to do it.使用存储在数据库中的这个令牌,我想获取用户的个人资料,但不知道如何去做。

public string GetAccessToken(string authorizationCode, string applicationID, string applicationSecret, string redirectUri)
        {
            //Redirect uri must match the redirect_uri used when requesting Authorization code.
            //Note: If you use a redirect back to Default, as in this sample, you need to add a forward slash
            //such as http://localhost:13526/

            // Get auth token from auth code       
            TokenCache TC = new TokenCache();

            //Values are hard-coded for sample purposes
            string authority = Settings.Default.AADAuthorityUri;
            AuthenticationContext AC = new AuthenticationContext(authority, TC);
            ClientCredential cc = new ClientCredential(applicationID, applicationSecret);

            //Set token from authentication result
            return AC.AcquireTokenByAuthorizationCode(
                authorizationCode,
                new Uri(redirectUri), cc).AccessToken;
        }

I checked methods of AuthenticationContext class but it doesn't have any method like 'GetMe' or something.我检查了 AuthenticationContext 类的方法,但它没有像“GetMe”之类的任何方法。

Any help is appreciated.任何帮助表示赞赏。

ADAL is only for authentication, it is not for calling APIs like the Microsoft Graph API. ADAL 仅用于身份验证,而不用于调用 Microsoft Graph API 等 API。

You'll need to acquire a token for the https://graph.microsoft.com resource, and then call the endpoint for user information: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_get .您需要获取https://graph.microsoft.com资源的令牌,然后调用端点获取用户信息: https : //developer.microsoft.com/en-us/graph/docs/api-参考/v1.0/api/user_get

There is also an SDK which can make the calls a bit easier: https://www.nuget.org/packages/Microsoft.Graph/还有一个 SDK 可以使调用更容易一些: https : //www.nuget.org/packages/Microsoft.Graph/

You can make use of JWTSecurityToken in C#.您可以在 C# 中使用JWTSecurityToken Also, you can visit https://jwt.io/ to see its working.此外,您可以访问https://jwt.io/以查看其工作情况。

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

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