コード例 #1
0
        /// <summary>
        /// 签名方式1
        /// </summary>
        /// <param name="data">签名内容的json串格式</param>
        /// <param name="pemFile">私钥文件物理路径</param>
        /// <returns></returns>
        public string CreateSignedToken(string data, string pemFile)
        {
            var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            //5分钟内有效
            var exp = ts.TotalMilliseconds + 30000;

            var header = new Dictionary <string, object>(new List <KeyValuePair <string, object> >()
            {
                new KeyValuePair <string, object>("alg", "PS256"),
                new KeyValuePair <string, object>("typ", "JWT")
            });

            var payload = new Dictionary <string, object>
            {
                { "sub", "tester" },
                { "exp", exp },
                { "datas", GetMd5(data) }
            };
            //string loadedRSA = File.ReadAllText("keys/private.rsa.pem");
            //string loadedRSA = File.ReadAllText(pemFile);
            //RSACryptoServiceProvider privateRSAkey = Crypto.DecodeRsaPrivateKey(loadedRSA);

            //var  rsa =  RSA_PEM.FromPEM(loadedRSA);
            //RSACryptoServiceProvider privateRSAkey= rsa.
            //var pem = System.IO.File.ReadAllText(pemFile);
            //byte[] certBuffer = GetBytesFromPEM(pem, "RSA PRIVATE KEY");

            //var privateKey = new X509Certificate2("my-key.p12", "password").GetRSAPrivateKey();
            //var privateKey = new X509Certificate2(loadedRSA).GetRSAPrivateKey();


            //var token = CreateToken(payload, loadedRSA);
            //var token = JWT.Encode(payload, rsa, JwsAlgorithm.PS256);

            var       encodedHeader    = Base64UrlEncoder.Encode(JsonConvert.SerializeObject(header));
            var       encodedPayload   = Base64UrlEncoder.Encode(JsonConvert.SerializeObject(payload));
            string    loadedRSA        = File.ReadAllText(pemFile);
            RSAHelper rsrHelper        = new RSAHelper(RSAType.RSA2, Encoding.UTF8, loadedRSA);
            var       encodedSignature = rsrHelper.Sign(string.Concat(encodedHeader, ".", encodedPayload));
            var       token            = string.Concat(encodedHeader, ".", encodedPayload, ".", encodedSignature);

            return(token);
        }
コード例 #2
0
        /// <summary>
        /// 签名方式2
        /// </summary>
        /// <param name="data">签名内容的json串格式</param>
        /// <param name="pemFile">私钥文件物理路径</param>
        /// <param name="customparas">签名附加内容</param>
        /// <returns></returns>
        public string CreateSignedToken(string data, string pemFile, IDictionary <string, object> customparas)
        {
            var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            //5分钟内有效
            var exp = ts.TotalMilliseconds + 30000;

            var header = new Dictionary <string, object>(new List <KeyValuePair <string, object> >()
            {
                new KeyValuePair <string, object>("alg", "PS256"),
                new KeyValuePair <string, object>("typ", "JWT")
            });

            var payload = new Dictionary <string, object>
            {
                { "sub", "chanjet" },
                { "exp", exp },
                { "datas", GetMd5(data) }
            };

            foreach (string key in customparas.Keys)
            {
                payload.Add(key, customparas[key]);
            }
            //string loadedRSA = File.ReadAllText("keys/private.rsa.pem");
            string loadedRSA = File.ReadAllText(pemFile);
            //RSAHelper rsrHelper=new RSAHelper(RSAType.RSA2, Encoding.UTF8, loadedRSA);
            //var token=  rsrHelper.Sign(
            //    "eyJhbGciOiJQUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0ZXIiLCJleHAiOjE1NTIzNzI1MjA5MzkuMzY0NSwiZGF0YXMiOiJlMjdjNjhjYzNiN2U2YTA3ZjFmZWE4OGUyYTdjMTU2YiJ9");
            ////RSACryptoServiceProvider privateRSAkey = Crypto.DecodeRsaPrivateKey(loadedRSA);
            //var token = JWT.Encode(payload, privateRSAkey, JwsAlgorithm.PS256);
            var       encodedHeader    = Base64UrlEncoder.Encode(JsonConvert.SerializeObject(header));
            var       encodedPayload   = Base64UrlEncoder.Encode(JsonConvert.SerializeObject(payload));
            RSAHelper rsrHelper        = new RSAHelper(RSAType.RSA2, Encoding.UTF8, loadedRSA);
            var       encodedSignature = rsrHelper.Sign(string.Concat(encodedHeader, ".", encodedPayload));
            var       token            = string.Concat(encodedHeader, ".", encodedPayload, ".", encodedSignature);

            return(token);
        }