Vb.net 授权代码验证(OpenID Connect 1.0)

Vb.net 授权代码验证(OpenID Connect 1.0),vb.net,identityserver4,openid-connect,Vb.net,Identityserver4,Openid Connect,我尝试按照以下步骤验证授权代码: 验证从授权颁发的授权代码的步骤 对于具有ID令牌的端点,客户端应执行以下操作: 1-使用哈希值对代码的ASCII表示形式的八位字节进行哈希 在JWA[JWA]中为 ID令牌的何塞头球。例如,如果alg是RS256,则哈希 使用的算法是SHA-256 2-取散列的最左边的一半,然后 base64url对其进行编码 3-ID令牌中的c_哈希值必须匹配 如果ID中存在c_散列,则在上一步中生成的值 代币 当我第一次请求授权端点尝试从WebForms客户端对用户进行身份

我尝试按照以下步骤验证授权代码:

验证从授权颁发的授权代码的步骤 对于具有ID令牌的端点,客户端应执行以下操作:

1-使用哈希值对代码的ASCII表示形式的八位字节进行哈希 在JWA[JWA]中为 ID令牌的何塞头球。例如,如果alg是RS256,则哈希 使用的算法是SHA-256

2-取散列的最左边的一半,然后 base64url对其进行编码

3-ID令牌中的c_哈希值必须匹配 如果ID中存在c_散列,则在上一步中生成的值 代币

当我第一次请求授权端点尝试从WebForms客户端对用户进行身份验证时,我有以下代码:

code=0655d48df75629d9fdbd5a060141bf66ca04418a0e762a6a5e6382c2748753af
我也有这个C_散列,我可以从id_令牌获得:

"c_hash": "QadHSCSim4aHM8q1F1F6Bg"
我正在尝试验证代码,执行以下操作:

Private Shared Function IsValidAuthorizationCode(authorizationCode As String, stringIdTokenPayload As String) As Boolean
    Dim serializer As New JavaScriptSerializer()
    Dim BytesPayload As Byte() = Decode(stringIdTokenPayload)
    Dim stringPayload As String = System.Text.ASCIIEncoding.ASCII.GetString(BytesPayload)
    Dim deserialized_payload = serializer.Deserialize(Of Dictionary(Of String, Object))(stringPayload)
    Dim c_hash = deserialized_payload.Item("c_hash").ToString()

    Dim mySHA256 = SHA256Managed.Create()
    Dim authorizationCodeOCTETS = Decode(authorizationCode)
    Dim elemntsToIterate = mySHA256.ComputeHash(authorizationCodeOCTETS)
    Dim length = elemntsToIterate.Length
    Dim hashedCode(length/2 - 1) As Byte

    Dim count = -1
    For Each element As Byte in elemntsToIterate
        count += 1
        If count > 15 Then
            hashedCode(count - 16) = element
        End If
    Next

    Dim hashedCodeLikeString = Convert.ToBase64String(hashedCode)
    If hashedCodeLikeString.Length <> hashedCode.Length
        Return False
    Dim result As Boolean = True
    For value As Integer = 0 To hashedCodeLikeString.Length
        If (hashedCodeLikeString(value) <> hashedCode(value)) Then
            result = False
            Exit For
        End If
    Next
    Return result
End Function
私有共享函数IsValidAuthorizationCode(authorizationCode为字符串,stringIdTokenPayload为字符串)为布尔值
Dim序列化程序作为新的JavaScriptSerializer()
Dim BytesPayload As Byte()=解码(stringIdTokenPayload)
Dim stringPayload As String=System.Text.AscienceODing.ASCII.GetString(BytesPayload)
Dim deserialized_payload=序列化程序。反序列化(字典的(字符串的,对象的))(stringPayload)
Dim c_hash=反序列化的_有效负载.Item(“c_hash”).ToString()
Dim mySHA256=SHA256Managed.Create()
Dim authorizationCodeOCTETS=解码(authorizationCode)
Dim elemntsToIterate=mySHA256.ComputeHash(authorizationCodeOCTETS)
尺寸长度=元素长度
Dim哈希代码(长度/2-1)作为字节
Dim计数=-1
对于每个元素,将其作为elemntsToIterate中的字节
计数+=1
如果计数>15,则
hashedCode(计数-16)=元素
如果结束
下一个
Dim hashedCodeLikeString=Convert.ToBase64String(hashedCode)
如果hashedCodeLikeString.Length hashedCode.Length
返回错误
将结果设置为布尔值=真
对于值为整数=0的hashedCodeLikeString.Length
如果(hashedCodeLikeString(value)hashedCode(value)),则
结果=错误
退出
如果结束
下一个
返回结果
端函数
但是我没有得到预期的结果。我需要得到一个真值,但我得到了一个假值。我认为我做错了什么,但我不知道是什么。需要帮忙吗


非常感谢您。

我不知道您的这种编程语言,但这是来自OIDClient的代码

    public bool ValidateHash(string data, string hashedData, string signatureAlgorithm)
    {
        var hashAlgorithm = GetMatchingHashAlgorithm(signatureAlgorithm);

        using (hashAlgorithm)
        {
            var hash = hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(data));

            byte[] leftPart = new byte[hashAlgorithm.HashSize / 16];
            Array.Copy(hash, leftPart, hashAlgorithm.HashSize / 16);

            var leftPartB64 = Base64Url.Encode(leftPart);
            var match = leftPartB64.Equals(hashedData);

            if (!match)
            {
                _logger.LogError($"data ({leftPartB64}) does not match hash from token ({hashedData})");
            }

            return match;
        }
    }

    public HashAlgorithm GetMatchingHashAlgorithm(string signatureAlgorithm)
    {
        var signingAlgorithmBits = int.Parse(signatureAlgorithm.Substring(signatureAlgorithm.Length - 3));

        switch (signingAlgorithmBits)
        {
            case 256:
                _logger.LogDebug("SHA256");
                return SHA256.Create();
            case 384:
                _logger.LogDebug("SHA384");
                return SHA384.Create();
            case 512:
                _logger.LogDebug("SHA512");
                return SHA512.Create();
            default:
                return null;
        }
    }