【问题标题】:How to get user details from access token如何从访问令牌中获取用户详细信息
【发布时间】:2020-03-23 15:24:23
【问题描述】:

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

并且我能够获得访问令牌。接下来,我将此访问令牌保存在我的数据库中以备将来使用。使用存储在数据库中的这个令牌,我想获取用户的个人资料,但不知道该怎么做。

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;
        }

我检查了 AuthenticationContext 类的方法,但它没有像 'GetMe' 之类的任何方法。

感谢任何帮助。

【问题讨论】:

    标签: c# asp.net azure azure-active-directory powerbi


    【解决方案1】:

    ADAL 仅用于身份验证,不适用于调用 Microsoft Graph API 等 API。

    您需要获取https://graph.microsoft.com 资源的令牌,然后调用端点以获取用户信息:https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_get

    还有一个 SDK 可以让调用更简单:https://www.nuget.org/packages/Microsoft.Graph/

    【讨论】:

    • 谢谢。有什么方法可以将此访问令牌与 Graph API 一起使用,还是需要其他类型的访问令牌?这很混乱。
    • 另外,请让我知道 ADAL 中是否有任何方法可以知道令牌是否有效(可能已过期或可能有人错误地修改了 DB 中的令牌)?
    • 很难说,您可以检查令牌受众(预期目标),例如jwt.ms(这是 aud 声明)。只要令牌缓存正确实施,ADAL 就可以很好地为您处理令牌更新。验证不适合你,那是 API 的工作。
    • 好的 - juunas。看起来很公平。知道如何将 TokenCache 保存到 SQL Server 数据库吗?我认为默认情况下它会保存在内存中。
    • 是的,默认是内存中的。您可以在此处查看 SQL 令牌缓存的示例:github.com/Azure-Samples/…
    【解决方案2】:

    您可以在 C# 中使用JWTSecurityToken。此外,您可以访问https://jwt.io/ 以查看其工作情况。

    【讨论】:

      猜你喜欢
      • 2016-05-05
      • 1970-01-01
      • 2021-05-30
      • 2018-08-12
      • 2021-08-14
      • 2019-01-03
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多