Esempio n. 1
0
        /// <summary>
        /// 得到永久授权码
        /// </summary>
        /// <param name="rc"></param>
        private void GetLongCode(object rc)
        {
            weixinStrct bs          = (weixinStrct)rc;
            string      ShortCode   = bs.ShortCode;
            string      corId       = bs.corpId;
            string      SuiteTicket = bs.SuiteTicket;

            log.AppenLog("线程:ShortCode:" + ShortCode + "_corId:" + corId + "_SuiteTicket:" + SuiteTicket);
            try
            {
                #region

                //通过临时授权码得到永久授权码
                //得到第三方应用凭证
                string suite_access_token = AccessTokenHelper.IssuitResult(corId, BonusHelper.AppConfig.Corpsecret, SuiteTicket);
                log.AppenLog("第三方应用凭证:" + suite_access_token);
                #region
                //ReturnJson rj = new ReturnJson();
                //if (!string.IsNullOrWhiteSpace(suitResult) && suitResult.Substring(0, 1) == "{" && suitResult.Substring(suitResult.Length - 1, 1) == "}")
                //{
                //    log.AppenLog("不为null");
                //    rj=JSON.parse<ReturnJson>(suitResult);
                //}
                //else
                //{
                //    log.AppenLog("为null");
                //    rj =new ReturnJson() { errmsg = string.IsNullOrWhiteSpace(suitResult) ? "error" : suitResult };
                //}
                #endregion
                //通过第三方应用凭证得到永久授权码
                Tuple <bool, LongShouquan> tu = GetLongCode(suite_access_token, ShortCode);
                if (tu.Item1)
                {
                    log.AppenLog("tu.Item1");
                    string sqlstr = string.Format($"exec proc_Weixin 'ZhuceCompany','{tu.Item2.auth_corp_info.corp_full_name}','{tu.Item2.auth_user_info.userid}','{tu.Item2.auth_corp_info.corp_square_logo_url}','{tu.Item2.permanent_code}','{tu.Item2.auth_corp_info.corpid}','{tu.Item2.auth_user_info.name}'");

                    log.AppenLog("Sqlstr:" + sqlstr);
                    //string str = "";
                    //Session[str] = tu.Item2;
                    string info = sql.EditDataCommand(sqlstr);
                    if (info == "0")
                    {
                        log.AppenLog("跟新成功");
                        //获取企业AccessToken  用于得到用户和部门

                        string Access_Token = AccessTokenHelper.GetQiye(tu.Item2.permanent_code.Replace("\"", ""), tu.Item2.auth_corp_info.corpid, string.Empty, DateTime.Now.AddDays(-1));
                        log.AppenLog("获取企业AccessToken:" + Access_Token);
                        if (Access_Token.Contains("access_token"))
                        {
                            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(QiYeaccess_token));
                            MemoryStream     ms            = new MemoryStream(Encoding.UTF8.GetBytes(Access_Token));
                            QiYeaccess_token obj           = (QiYeaccess_token)ser.ReadObject(ms);
                            string           sqlstr2       = string.Format($"exec proc_Weixin 'UpdateAtoken','','','','','{tu.Item2.auth_corp_info.corpid}','','{obj.access_token}','{DateTime.Now.AddSeconds(int.Parse(obj.expires_in)).ToString("yyyy-MM-dd HH:mm:ss")}'");
                            string           info2         = sql.EditDataCommand(sqlstr2);
                            BonusHelper.AppConfig.Access_Token_Qiye = obj.access_token;
                            BonusHelper.AppConfig.Qiye_YouXRQ       = DateTime.Now.AddSeconds(int.Parse(obj.expires_in)).ToString("yyyy-MM-dd HH:mm:ss");
                        }
                    }
                    else
                    {
                        log.AppenLog("跟新失败");
                    }
                }
                else
                {
                    log.AppenLog("false");
                }


                #endregion
            }
            catch (Exception ex)
            {
                log.AppenLog("线程错误:" + ex.Message);
            }
            //通过永久和id得到token
        }
Esempio n. 2
0
        /// <summary>
        /// 指令回调
        /// </summary>
        /// <param name="corpid">公司id</param>
        /// <returns></returns>
        public ActionResult ActionReturn(string corpid)
        {
            WXBizMsgCrypt wx = new WXBizMsgCrypt(BonusHelper.AppConfig.sToken, BonusHelper.AppConfig.sEncodingAESKey2, corpid);

            log.AppenLog("指令corpid:" + corpid);
            string      sCorpID = Request.QueryString["corpid"];
            weixinStrct bs      = new weixinStrct();

            if (sCorpID == "$CORPID$")
            {
                try
                {
                    //Timer ReadDBValue = new Timer(new TimerCallback(GetLongCode), null, 0, 60000);//从数据库中读数据到集合中
                    //log.AppenLog("ActionReturn:$CORPID$");
                    //log.AppenLog($"msg_signature:{Request.QueryString["msg_signature"]}");
                    //log.AppenLog($"timestamp:{Request.QueryString["timestamp"]}");
                    //log.AppenLog($"nonce:{Request.QueryString["nonce"]}");
                    //log.AppenLog("ActionReturn:1");
                    Stream requestStream = System.Web.HttpContext.Current.Request.InputStream;
                    byte[] requestByte   = new byte[requestStream.Length];
                    requestStream.Read(requestByte, 0, (int)requestStream.Length);
                    string requestStr = Encoding.UTF8.GetString(requestByte);
                    //log.AppenLog("ActionReturn:2");
                    if (string.IsNullOrWhiteSpace(requestStr))
                    {
                        //log.AppenLog("ActionReturn:3");
                        log.AppenLog("ActionReturn:requestStr is null");
                    }
                    else
                    {
                        //log.AppenLog($"ActionReturn:4 {requestStr}");
                        XmlDocument requestDocXml = new XmlDocument();
                        requestDocXml.LoadXml(requestStr);
                        XmlElement rootElement      = requestDocXml.DocumentElement;
                        string     XML              = "未知类型";
                        string     sVerifyMsgSig    = Request.QueryString["msg_signature"];
                        string     sVerifyTimeStamp = Request.QueryString["timestamp"];
                        string     sVerifyNonce     = Request.QueryString["nonce"];
                        string     sMsg             = "";
                        int        iresult          = wx.DecryptMsg(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, requestStr, ref sMsg);

                        //log.AppenLog($"ActionReturn:5 {sMsg}");
                        requestDocXml = new XmlDocument();
                        requestDocXml.LoadXml(sMsg);
                        rootElement = requestDocXml.DocumentElement;
                        XmlNode node = rootElement.SelectSingleNode("SuiteTicket");
                        if (node != null)
                        {
                            log.AppenLog("推送suite_ticket:" + node.InnerText);
                            BonusHelper.AppConfig.SuiteTicket = node.InnerText;
                        }

                        XmlNode node2 = rootElement.SelectSingleNode("SuiteId");//第三方应用的id
                        if (node2 != null)
                        {
                            log.AppenLog("第三方应用id:" + node2.InnerText);
                            BonusHelper.AppConfig.SuiteId = node2.InnerText;
                            bs.corpId = node2.InnerText;
                        }
                        XmlNode node_AuthCode = rootElement.SelectSingleNode("AuthCode");//临时授权码
                        if (node_AuthCode != null)
                        {
                            //BonusHelper.AppConfig.node_AuthCode = node_AuthCode.InnerText;
                            //Session["node_AuthCode"] = node_AuthCode.InnerText;
                            log.AppenLog("临时授权码:" + node_AuthCode.InnerText);
                            bs.ShortCode = node_AuthCode.InnerText;
                        }

                        //string AuthCode = rootElement.SelectSingleNode("AuthCode").InnerText;//临时授权码
                        //string InfoType = rootElement.SelectSingleNode("InfoType").InnerText;
                        string TimeStamp = rootElement.SelectSingleNode("TimeStamp").InnerText;//时间戳
                        log.AppenLog("时间戳:" + TimeStamp);
                        log.AppenLog("ActionReturn:6");

                        Thread t = new Thread(new ParameterizedThreadStart(GetLongCode));
                        bs.SuiteTicket = BonusHelper.AppConfig.SuiteTicket;
                        t.Start(bs);
                    }
                }
                catch (Exception ex)
                {
                    log.AppenLog($"ActionReturn: Exception:{ex.Message}");
                }

                return(Content("success"));
            }
            else
            {
                BonusHelper.AppConfig.sCorpID = corpid;
                log.AppenLog("sss");
                return(Content(MathUrl()));
            }
        }