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); }
public void ProcessRequest(HttpContext context) { try { #region 获取套件配置参数 string mToken = ConfigurationManager.AppSettings["Token"]; string mSuiteKey = ""; string mEncodingAesKey = ConfigurationManager.AppSettings["EncodingAESKey"]; //mSuiteKey = "suite4xxxxxxxxxxxxxxx"; #endregion #region 获取回调URL里面的参数 //url中的签名 string msgSignature = context.Request["signature"]; //url中的时间戳 string timeStamp = context.Request["timestamp"]; //url中的随机字符串 string nonce = context.Request["nonce"]; //post数据包数据中的加密数据 string encryptStr = GetPostParam(context); #endregion string sEchoStr = ""; #region 验证回调的url SuiteAuth suiteAuth = new SuiteAuth(); var ret = suiteAuth.VerifyURL(mToken, mEncodingAesKey, msgSignature, timeStamp, nonce, encryptStr, ref mSuiteKey); if (ret != 0) { Helper.WriteLog("ERR: VerifyURL fail, ret: " + ret); return; } #endregion #region //构造DingTalkCrypt DingTalkCrypt dingTalk = new DingTalkCrypt(mToken, mEncodingAesKey, mSuiteKey); string plainText = ""; dingTalk.DecryptMsg(msgSignature, timeStamp, nonce, encryptStr, ref plainText); Hashtable tb = (Hashtable)JsonConvert.DeserializeObject(plainText, typeof(Hashtable)); string eventType = tb["EventType"].ToString(); string res = "success"; Helper.WriteLog("plainText:" + plainText); Helper.WriteLog("eventType:" + eventType); switch (eventType) { case "suite_ticket"://定时推送Ticket ConfigurationManager.AppSettings["SuiteTicket"] = tb["SuiteTicket"].ToString(); mSuiteKey = tb["SuiteKey"].ToString(); suiteAuth.SaveSuiteTicket(tb); break; case "tmp_auth_code"://钉钉推送过来的临时授权码 ConfigurationManager.AppSettings["TmpAuthCode"] = tb["AuthCode"].ToString(); suiteAuth.SaveTmpAuthCode(tb); break; case "change_auth":// do something; break; case "check_update_suite_url": res = tb["Random"].ToString(); mSuiteKey = tb["TestSuiteKey"].ToString(); break; } timeStamp = Helper.GetTimeStamp().ToString(); string encrypt = ""; string signature = ""; dingTalk = new DingTalkCrypt(mToken, mEncodingAesKey, mSuiteKey); dingTalk.EncryptMsg(res, timeStamp, nonce, ref encrypt, ref signature); Hashtable jsonMap = new Hashtable { {"msg_signature", signature}, {"encrypt", encrypt}, {"timeStamp", timeStamp}, {"nonce", nonce} }; string result = JsonConvert.SerializeObject(jsonMap); context.Response.Write(result); #endregion } catch (Exception ex) { Helper.WriteLog(DateTime.Now + ex.Message); } }
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; }