示例#1
0
        public string Create(HashAlgorithm algorithm, byte[] privateKey, Dictionary <string, string> payload)
        {
            var header = new Dictionary <string, string>();

            header.Add("alg", algorithm.ToString());
            header.Add("typ", "JWT");
            var encodedHeader = this.ToJsonBase64Url(header);

            var encodedPayload = this.ToJsonBase64Url(payload);

            string signature = string.Empty;

            if (algorithm == HashAlgorithm.RS256)
            {
                signature = Base64Url.Encode(EncodeHS256(privateKey, $"{encodedHeader}.{encodedPayload}"));
            }
            else if (algorithm == HashAlgorithm.RS256)
            {
                // TODO: also need to do RS256
                throw new NotImplementedException();
            }

            return($"{encodedHeader}.{encodedPayload}.{signature}");
        }
示例#2
0
 private string ToJsonBase64Url(Dictionary <string, string> dict)
 {
     return(Base64Url.Encode(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(dict))));
 }