示例#1
0
        public int VerifyURL(string mToken, string mEncodingAesKey, string msgSignature, string timeStamp, string nonce, string encryptStr, ref string suiteKey)
        {
            var ret = 0;
            List <SuiteKeyInfo> suikKeyList = new Select().From(SuiteKeyInfo.Schema).ExecuteTypedList <SuiteKeyInfo>();

            foreach (SuiteKeyInfo suiteKeyInfo in suikKeyList)
            {
                DingTalkCrypt dingTalk = new DingTalkCrypt(mToken, mEncodingAesKey, suiteKeyInfo.SuiteKey);
                string        sEchoStr = "";
                ret = dingTalk.VerifyURL(msgSignature, timeStamp, nonce, encryptStr, ref sEchoStr);
                if (ret == 0)
                {
                    IsvReceive isvReceive = new IsvReceive
                    {
                        Signature  = msgSignature,
                        Timestamp  = timeStamp,
                        Nonce      = nonce,
                        Encrypt    = encryptStr,
                        EchoString = sEchoStr,
                        CreateTime = DateTime.Now
                    };
                    isvReceive.Save();
                    suiteKey = suiteKeyInfo.SuiteKey;
                    return(ret);
                }
            }
            return(ret);
        }
示例#2
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            #region 获取套件配置参数
            string m_Token = ConfigurationManager.AppSettings["Token"];
            if (string.IsNullOrEmpty(m_Token))
            {
                Helper.WriteLog("token没有配置");
            }
            string m_SuiteKey = ConfigurationManager.AppSettings["SuiteKey"];
            if (string.IsNullOrEmpty(m_Token))
            {
                Helper.WriteLog("token没有配置");
            }
            string m_EncodingAESKey = ConfigurationManager.AppSettings["EncodingAESKey"];
            if (string.IsNullOrEmpty(m_Token))
            {
                Helper.WriteLog("token没有配置");
            }
            #endregion
            //构造DingTalkCrypt
            DingTalkCrypt dingTalk = new DingTalkCrypt(m_Token, m_EncodingAESKey, m_SuiteKey);

            string sVerifyMsgSig    = txtMsgSig.Text.Trim();
            string sVerifyTimeStamp = txtTimeStamp.Text.Trim();
            string sVerifyNonce     = txtNonce.Text.Trim();
            string encryptStr       = txtEncryptStr.Text.Trim();

            string sEchoStr = "";

            int ret = dingTalk.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, encryptStr, ref sEchoStr);
            txtEchoStr.Text = sEchoStr;


            //string sMsg = "";
            //ret = dingTalk.DecryptMsg(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, encryptStr, ref sMsg);

            //string sEncryptMsg = "";
            //dingTalk.EncryptMsg("success", sVerifyTimeStamp, sVerifyNonce, ref sEncryptMsg);

            // sVerifyTimeStamp = "1441713878094";
            // sVerifyNonce = "ZjJmiMAu";
            //dingTalk.EncryptMsg("success", sVerifyTimeStamp, sVerifyNonce, ref sEncryptMsg);
        }
示例#3
0
        public JsonResult Receive(string signature, string timestamp, string nonce, PostBody body)
        {
            LogHelper.WriteProgramLogInFolder(DateTime.Now.ToString() + " 接收回调!", "DDCallBack");
            Dictionary <int, string> dic = new Dictionary <int, string>();

            dic.Add(900001, "加密明文文本非法");
            dic.Add(900002, "加密时间戳参数非法");
            dic.Add(900003, "加密随机字符串参数非法");
            dic.Add(900004, "不合法的aeskey");
            dic.Add(900005, "签名不匹配");
            dic.Add(900006, "计算签名错误");
            dic.Add(900007, "计算加密文字错误");
            dic.Add(900008, "计算解密文字错误");
            dic.Add(900009, "计算解密文字长度不匹配");
            dic.Add(900010, "计算解密文字suiteKey不匹配");

            //接收encrypt参数
            string encryptStr = body.encrypt.Replace("{\"encrypt\":\"", "").Replace("\"}", "");

            //注册时填写的token、aes_key、suitekey
            string token = ConfigurationManager.AppSettings["SuiteToken"];
            //token = "123456";//钉钉测试文档中的token
            string aes_key = ConfigurationManager.AppSettings["Suiteaes_key"];
            //aes_key = "4g5j64qlyl3zvetqxz5jiocdr586fn2zvjpa8zls3ij";//钉钉测试文档中的aes_key
            string suitekey = ConfigurationManager.AppSettings["DD_corpid"];

            //suitekey = "suite4xxxxxxxxxxxxxxx";//钉钉测试文档中的suitekey

            #region 验证回调的url
            DingTalkCrypt dingTalk = new DingTalkCrypt(token, aes_key, suitekey);
            string        sEchoStr = "";
            int           ret      = dingTalk.VerifyURL(signature, timestamp, nonce, encryptStr, ref sEchoStr);
            if (ret != 0)
            {
                string message = "";
                if (dic.ContainsKey(ret))
                {
                    message = dic.Where(e => e.Key == ret).FirstOrDefault().Value;
                }

                LogHelper.WriteProgramLogInFolder(DateTime.Now.ToString() + " 验证回调发生错误,错误代码为:" + ret + " " + message, "DDCallBack");
                return(Json(new object()));
            }
            #endregion

            #region 解密接受信息,进行事件处理
            string plainText = "";
            ret = dingTalk.DecryptMsg(signature, timestamp, nonce, encryptStr, ref plainText);
            if (ret != 0)
            {
                string message = "";
                if (dic.ContainsKey(ret))
                {
                    message = dic.Where(e => e.Key == ret).FirstOrDefault().Value;
                }

                LogHelper.WriteProgramLogInFolder(DateTime.Now.ToString() + " 解密信息发生错误,错误代码为:" + ret + " " + message, "DDCallBack");
                return(Json(new object()));
            }

            Hashtable tb        = (Hashtable)JsonConvert.DeserializeObject(plainText, typeof(Hashtable));
            string    eventType = tb["EventType"].ToString();
            string    res       = "success";

            LogHelper.WriteProgramLogInFolder(DateTime.Now.ToString() + " 接收到的事件类型为:" + eventType, "DDCallBack");
            switch (eventType)
            {
            case "bpms_task_change":    //审批任务开始,结束,转交
                #region 执行代码 在此处添加业务逻辑代码,处理获取的审批单信息
                ApproveModel modelTask = JsonConvert.DeserializeObject <ApproveModel>(plainText);
                #endregion
                break;

            case "bpms_instance_change":    //审批实例开始,结束
                #region 执行代码 在此处添加业务逻辑代码,处理获取的审批单信息
                ApproveModel modelInstance = JsonConvert.DeserializeObject <ApproveModel>(plainText);
                #endregion
                break;

            default:
                break;
            }

            timestamp = DateTime.Now.GetTimeStamp().ToString();
            string encrypt    = "";
            string signature2 = "";
            dingTalk = new DingTalkCrypt(token, aes_key, suitekey);
            ret      = dingTalk.EncryptMsg(res, timestamp, nonce, ref encrypt, ref signature2);
            if (ret != 0)
            {
                string message = "";
                if (dic.ContainsKey(ret))
                {
                    message = dic.Where(e => e.Key == ret).FirstOrDefault().Value;
                }

                LogHelper.WriteProgramLogInFolder(DateTime.Now.ToString() + " 解密信息发生错误,错误代码为:" + ret + " " + message, "DDUserInfoUpdate");
                return(Json(new object()));
            }

            Hashtable jsonMap = new Hashtable
            {
                { "msg_signature", signature2 },
                { "encrypt", encrypt },
                { "timeStamp", timestamp },
                { "nonce", nonce }
            };

            return(Json(jsonMap));

            #endregion
        }