Azure AD 和 Azure AD B2C 令牌的区别

4

我最近一直在研究Azure AD授权代码流程,突然开始将所有内容转移到Azure AD B2C,发现Azure AD和Azure AD B2C之间存在很多差异。以下是我的问题,请有人能回答吗?

  1. Azure AD中注册本地应用程序时,允许http或https作为重定向URL。但Azure AD B2C不支持此功能(由于两者都遵循OAUTH规范,因此两者应该表现类似)。

  2. Azure AD JWT访问令牌具有“x5c”条目,而B2C没有此条目。这是有特别原因的吗?我尝试从Azure AD中复制公钥,并尝试上传相同的签名密钥到B2C,但无法成功。不确定我缺少了什么,但我的问题是为什么这些访问令牌在其签名方面不同。


你不能将Azure AD的签名密钥上传到B2C,因为你所看到的只是公钥。你需要私钥才能使用它们进行签名。如果签名不同,则签名密钥也不同。 - juunas
1个回答

3
对于第一个问题,我建议您从此处提出反馈如果您需要此功能。
至于第二个问题,验证来自Azure AD B2C和普通Azure AD的令牌相同。我们可以使用指数(e)和模数(n)生成公钥。但是密钥终结点不同,我们需要使用以下链接检索Azure AD B2C的密钥:
https://login.microsoftonline.com/{tenant}/discovery/v2.0/keys?p={signInPolicy}

以下是验证Azure AD B2C发行的令牌的代码,供您参考:

static void Main(string[] args)
{          
    var idtoken = "";

    var exponent = "AQAB";
    var modulus = "";
    var result=  VerifyTokenDetails(idtoken, exponent, modulus);
}
private static bool VerifyTokenDetails(string idToken, string exponent, string modulus)
{
    try
    {              
        var parts = idToken.Split('.');
        var header = parts[0];
        var payload = parts[1];
        string signedSignature = parts[2];
        //Extract user info from payload   
        string userInfo = Encoding.UTF8.GetString(Base64UrlDecode(payload));
        //Which will be Verified
        string originalMessage = string.Concat(header, ".", payload);
        byte[] keyBytes = Base64UrlDecode(modulus);
        string keyBase = Convert.ToBase64String(keyBytes);
        string key = @"<RSAKeyValue> <Modulus>" + keyBase + "</Modulus> <Exponent>" + exponent + "</Exponent> </RSAKeyValue>";
        bool result = VerifyData(originalMessage, signedSignature, key);
        if (result)
            return true;
        else
            return false;
    }
    catch (Exception ex) { }
    return false;
}

/// <summary>  
/// Verifies encrypted signed message with public key encrypted original message.  
/// </summary>  
/// <param name="originalMessage">Original message as string. (Encrypted form)</param>  
/// <param name="signedMessage">Signed message as string. (Encrypted form)</param>  
/// <param name="publicKey">Public key as XML string.</param>  
/// <returns>Boolean True if successful otherwise return false.</returns>  
private static bool VerifyData(string originalMessage, string signedMessage, string publicKey)
{
    bool success = false;
    using (var rsa = new RSACryptoServiceProvider())
    {
        var encoder = new UTF8Encoding();
        byte[] bytesToVerify = encoder.GetBytes(originalMessage);
        byte[] signedBytes = Base64UrlDecode(signedMessage);
        try
        {

            rsa.FromXmlString(publicKey);
            SHA256Managed Hash = new SHA256Managed();
            byte[] hashedData = Hash.ComputeHash(signedBytes);
            // Summary:
            //     Verifies that a digital signature is valid by determining the hash value in the
            //     signature using the provided public key and comparing it to the hash value of
            //     the provided data.
            success = rsa.VerifyData(bytesToVerify, CryptoConfig.MapNameToOID("SHA256"), signedBytes);
        }
        catch (CryptographicException e)
        {
            success = false;
        }
        finally
        {
            rsa.PersistKeyInCsp = false;
        }
    }
    return success;
}

private static byte[] Base64UrlDecode(string input)
{
    var output = input;
    output = output.Replace('-', '+'); // 62nd char of encoding  
    output = output.Replace('_', '/'); // 63rd char of encoding  
    switch (output.Length % 4) // Pad with trailing '='s  
    {
        case 0: break; // No pad chars in this case  
        case 2: output += "=="; break; // Two pad chars  
        case 3: output += "="; break; // One pad char  
        default: throw new System.Exception("Illegal base64url string!");
    }
    var converted = Convert.FromBase64String(output); // Standard base64 decoder  
    return converted;
}

2
谢谢您的回复。我也在使用类似的方法生成公钥。https://play.golang.org/p/7wWMBOp10R 。不知道 Azure AD 和 B2C 之间令牌差异的原因是否有特别之处? - Venkatesh Marepalli
3
令牌验证问题是否已解决?据我了解,Azure AD发行的令牌中没有x5c声明(请参阅此处)。 - Fei Xue - MSFT
据我所知,在令牌验证的过程中没有必要使用 x5c。我们只需要使用 x5t 声明来从密钥终结点标识公钥。然后,我们可以使用 en 的值创建密钥以生成公钥以验证令牌。有关从 Azure AD B2C 验证令牌的更多详细信息,请参阅此 链接 - Fei Xue - MSFT
1
我想这会让我的问题更清晰。我应该问你关于jwks端点的事情。为什么B2C和AD的公钥不同。Azure AD密钥有“x5c”条目,而B2C没有。 - Venkatesh Marepalli
谢谢。我已经发布了关于重定向URI的反馈,感谢提供代码。 - Venkatesh Marepalli
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接