Azure Active Directory - 从 JWT token 挖掘 oAuth2Permission、appRole 和组声明

标签 azure azure-active-directory

场景:我在不同的 Azure 租户中有 2 个 AAD 应用程序 - 让我们调用租户 A 和 B。租户 A 中的应用程序定义了租户 B 中的应用程序已同意的自定义 appRole 和 oAuth2Permissions。租户 B 中的应用程序定义了KeyCredential 使用自己的证书。

我能够使用传入的 ADAL v3 成功获取 JWT token
ClientAssertionCertificate(资源是租户 A 中的应用程序,客户端是租户 B 中的应用程序),如下所示:

        AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);
        X509Certificate2 cert = AuthHelper.FindCertificateByThumbprint("<Thumbprint of the Tenant B app's KeyCredential cert>");
        ClientAssertionCertificate assertionCert = new ClientAssertionCertificate(clientId, cert);
        AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(resource, assertionCert).Result;
        string accessToken = authenticationResult.AccessToken; // Valid.
        string idToken = authenticationResult.IdToken; // Null.

AcquireTokenAsync 的 AuthenticationResult 包含我也可以成功验证的 AccessToken...

        string issuer = string.Format(ConfigurationManager.AppSettings["issuer"], ConfigurationManager.AppSettings["tenantid"]);
        string audience = ConfigurationManager.AppSettings["audience"];
        string certThumbprint = ConfigurationManager.AppSettings["certThumbprint"];
        var stsDiscoveryEndpoint = string.Concat(issuer, URI_DELIMITER, STS_DISCOVERY_ENDPOINT_SUFFIX);

        ConfigurationManager<OpenIdConnectConfiguration> configManager = new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint);
        var config = configManager.GetConfigurationAsync().Result;

        // extract issuer and token for validation
        var authority = config.Issuer;
        var signingTokens = config.SigningTokens.ToList();

        sys.TokenValidationParameters validationParameters = new sys.TokenValidationParameters
        {
            ValidIssuer = issuer,
            ValidAudience = audience,
            ValidateLifetime = true,
            IssuerSigningTokens = signingTokens,
            CertificateValidator = X509CertificateValidator.None,                
        };

        sys.JwtSecurityTokenHandler tokendHandler = new sys.JwtSecurityTokenHandler();
        sys.SecurityToken jwt;
        var result = tokendHandler.ValidateToken(token, validationParameters, out jwt);
  1. 但是 IdToken 是空的。这是因为我没有将 UserAssertion 传递到 AcquireTokenAsync 调用吗?

  2. 为了能够进一步授权请求,我正在寻找 - appRole、oAuth2Permissions 和 groupMembership 声明。然而,这些声明并不在 AccessToken 中。如果这是预期的,那么我什么时候可以在 token 中找到所述声明。如果这不是预期的情况,那么我在 token 生成或 AAD 应用程序设置中缺少什么会导致这种情况?

注意:我的场景不涉及 Web API(但它受 WAAD 限制),因此我必须手动进行 token 验证和授权。

最佳答案

  1. 仅当您遵循涉及用户的身份验证流程时,才会生成 ID token 。 在身份验证过程中,您不会登录用户,因此我无法理解您希望获得哪种 ID token ...

    您需要按照授权代码授予流程来登录用户并获取委托(delegate) token 。

  2. 我有 a blog post关于如何让 groupMembership 声明出现在两个应用程序之间。

    访问 token 中不存在“oAuth2Permission”声明之类的想法。您可以找到 AAD here 支持的所有不同 token 和声明.

    最终,您应该依靠 AAD Graph API 来获取这些详细信息,而不是 token ,因为 token 具有固有的大小限制,这可能会遗漏有关现有的某些角色或组的数据。

关于Azure Active Directory - 从 JWT token 挖掘 oAuth2Permission、appRole 和组声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46717128/

相关文章:

sql - Azure Function 到 Azure SQL - 性能和扩展

java - Azure 从 Azure Java SDK 检索虚拟机的 PublicIPAddress

node.js - 如何将多个 Azure Active Directory 帐户连接到您的应用程序?

azure - Open Id Connect 的 token 存储在哪里?

azure - 如何访问部署在Azure上的VS代码

c# - 适用于 Windows Phone 8.1 的 ADAL 问题

azure - 如何使用 PowerShell 添加应用程序注册 key 而不破坏 Azure 门户?

azure - 如何使用应用程序注册和 azure cli 命令获取所有 azure 应用程序注册并查看 secret 的到期日期?

angularjs - 如何使用 Azure AD 授权 Node.js API?

Azure AD B2C 不显示自定义应用程序的登录信息