1

我是 JWS 概念的新手,并被要求在 C# 中为 JSON 签名创建一个片段。我们将有多个签名,因此每次对 JSON 有效负载进行签名时,都会将其添加到签名中。

我检查了 JWS JSON 序列化以及如何在多个签名的情况下使用它。

以下是用于签名和加密的代码:

// Checking if the request contains body, usually will be null wiht HTTP GET and DELETE
if (request.Content != null)
{
    byte[] content = await request.Content.ReadAsByteArrayAsync();
    MD5 md5 = MD5.Create();

    // Hashing the request body, any change in request body will result in different hash, we'll ensure message integrity
    byte[] requestContentHash = md5.ComputeHash(content);
    requestContentBase64String = Convert.ToBase64String(requestContentHash);
}

// Creating the raw signature string
string signatureRawData = String.Format("{0}{1}{2}{3}{4}{5}", APPId, requestHttpMethod, requestUri, requestTimeStamp, nonce, requestContentBase64String);

var secretKeyByteArray = Convert.FromBase64String(APIKey);

byte[] signature = Encoding.UTF8.GetBytes(signatureRawData);

using (HMACSHA256 hmac = new HMACSHA256(secretKeyByteArray))
{
    byte[] signatureBytes = hmac.ComputeHash(signature);
    string requestSignatureBase64String = Convert.ToBase64String(signatureBytes);

    // Setting the values in the Authorization header using custom scheme (amx)
    request.Headers.Authorization = new AuthenticationHeaderValue("amx", string.Format("{0}:{1}:{2}:{3}", APPId, requestSignatureBase64String, nonce, requestTimeStamp));
}

response = await base.SendAsync(request, cancellationToken);

但是我们如何实现 JSON 签名呢?

我需要帮助实现我们如何使用 SignedXML 逻辑,以使用 x509 证书签署 XML 文档。

4

1 回答 1

-5

在此处输入图像描述

我从这里获取图表https://jwt.io/index.html

JSON Web 令牌是一种开放的行业标准 RFC 7519 方法,用于在两方之间安全地表示声明。

JWT.IO 允许您解码、验证和生成 JWT。

试试 Nuget jose-jwt 包。

var payload = new Dictionary<string, object>()
{
    { "sub", "mr.x@contoso.com" },
    { "exp", 1300819380 }
};

string token = Jose.JWT.Encode(payload, null, JwsAlgorithm.none);
于 2019-08-21T05:02:12.757 回答