Ejemplo n.º 1
0
        public string GetSignature()
        {
            //String plain = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", ticket, nonceStr, timeStamp, url);
            string randomStr = DingTalkSignatureUtil.GetRandomStr(10);
            string timeStamp = TopUtils.GetCurrentTimeMillis().ToString();

            Dictionary <string, object> dic = new Dictionary <string, object>();

            dic.Add(Constants.JSAPI_TICKET, _cacheHelper.Get(Constants.JSAPI_TICKET).ToString());
            dic.Add(Constants.NONCESTR, randomStr);
            dic.Add(Constants.TIMESTAMP, timeStamp);
            dic.Add(Constants.URL, AppSettings.Get(Constants.URL));

            _cacheHelper.Add(Constants.NONCESTR, randomStr);
            _cacheHelper.Add(Constants.TIMESTAMP, timeStamp);

            try
            {
                byte[] bytes             = Encoding.UTF8.GetBytes(ConvertDictionaryToString(dic));
                byte[] digest            = SHA1.Create().ComputeHash(bytes);
                string digestBytesString = BitConverter.ToString(digest).Replace("-", "");
                _cacheHelper.Add(Constants.SIGNATURE, digestBytesString.ToLower());
                return(digestBytesString.ToLower());
            }
            catch (Exception e)
            {
                throw;
            }
        }
Ejemplo n.º 2
0
 public BaseDingTalkService()
 {
     corpid      = AppSettings.Get("corpid");
     corpsecret  = AppSettings.Get("corpsecret");
     agentid     = AppSettings.Get("agentid");
     appkey      = AppSettings.Get("appkey");
     appsecret   = AppSettings.Get("appsecret");
     url         = AppSettings.Get("url");
     registerurl = AppSettings.Get("registerurl");
     timestamp   = TopUtils.GetCurrentTimeMillis().ToString();
     noncestr    = DingTalkSignatureUtil.GetRandomStr(10);
 }
Ejemplo n.º 3
0
        private T DoExecuteOApi <T>(IDingTalkRequest <T> request, string session, string accessKey, string accessSecret, string suiteTicket, string corpId, DateTime timestamp) where T : DingTalkResponse
        {
            long start = DateTime.Now.Ticks;

            // 提前检查业务参数
            try
            {
                request.Validate();
            }
            catch (TopException e)
            {
                return(CreateErrorResponse <T>(e.ErrorCode, e.ErrorMsg));
            }

            this.format = Constants.FORMAT_JSON;

            // 添加协议级请求参数
            TopDictionary txtParams = new TopDictionary(request.GetParameters());

            txtParams.Add(DingTalkConstants.ACCESS_TOKEN, session);

            // 添加头部参数
            if (this.useGzipEncoding)
            {
                request.GetHeaderParameters()[Constants.ACCEPT_ENCODING] = Constants.CONTENT_ENCODING_GZIP;
            }

            string realServerUrl = null;

            // 签名优先
            if (accessKey != null)
            {
                long dingTimestamp = GetTimestamp(DateTime.UtcNow);
                // 验证签名有效性
                String canonicalString          = DingTalkSignatureUtil.GetCanonicalStringForIsv(dingTimestamp, suiteTicket);
                String signature                = DingTalkSignatureUtil.ComputeSignature(accessSecret, canonicalString);
                IDictionary <String, String> ps = new Dictionary <String, String>();
                ps.Add("accessKey", accessKey);
                ps.Add("signature", signature);
                ps.Add("timestamp", dingTimestamp + "");
                if (suiteTicket != null)
                {
                    ps.Add("suiteTicket", suiteTicket);
                }
                if (corpId != null)
                {
                    ps.Add("corpId", corpId);
                }

                String queryStr = DingTalkSignatureUtil.ParamToQueryString(ps, "utf-8");
                if (this.serverUrl.IndexOf("?") > 0)
                {
                    realServerUrl = this.serverUrl + "&" + queryStr;
                }
                else
                {
                    realServerUrl = this.serverUrl + "?" + queryStr;
                }
            }
            else
            {
                if (this.serverUrl.IndexOf("?") > 0)
                {
                    realServerUrl = this.serverUrl + (session != null && session != "" ? ("&access_token=" + session) : "");
                }
                else
                {
                    realServerUrl = this.serverUrl + (session != null && session != "" ? ("?access_token=" + session) : "");
                }
            }

            try
            {
                string body;

                if (request.GetHttpMethod() == "POST")
                {
                    if (request is IDingTalkUploadRequest <T> ) // 是否需要上传文件
                    {
                        IDingTalkUploadRequest <T>     uRequest   = (IDingTalkUploadRequest <T>)request;
                        IDictionary <string, FileItem> fileParams = TopUtils.CleanupDictionary(uRequest.GetFileParameters());
                        body = webUtils.DoPost(realServerUrl, null, fileParams, request.GetHeaderParameters());
                    }
                    else
                    {
                        IDictionary <String, Object> jsonParams = new Dictionary <String, Object>();
                        foreach (string key in request.GetParameters().Keys)
                        {
                            string value = request.GetParameters()[key];
                            if (value.StartsWith("[") && value.EndsWith("]"))
                            {
                                IList childMap = (IList)TopUtils.JsonToObject(value);
                                jsonParams.Add(key, childMap);
                            }
                            else if (value.StartsWith("{") && value.EndsWith("}"))
                            {
                                IDictionary <string, Object> childMap = (IDictionary <string, Object>)TopUtils.JsonToObject(value);
                                jsonParams.Add(key, childMap);
                            }
                            else
                            {
                                jsonParams.Add(key, value);
                            }
                        }


                        body = webUtils.DoPostWithJson(realServerUrl, jsonParams, request.GetHeaderParameters());
                    }
                }
                else
                {
                    body = webUtils.DoGet(realServerUrl, request.GetParameters());
                }

                // 解释响应结果
                T rsp;
                if (disableParser)
                {
                    rsp      = Activator.CreateInstance <T>();
                    rsp.Body = body;
                }
                else
                {
                    ITopParser <T> tp = new DingTalkJsonParser <T>();
                    rsp = tp.Parse(body);
                }

                // 追踪错误的请求
                if (rsp.IsError)
                {
                    TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
                    TraceApiError(request.GetApiName(), serverUrl, txtParams, latency.TotalMilliseconds, rsp.Body);
                }
                return(rsp);
            }
            catch (Exception e)
            {
                TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
                TraceApiError(request.GetApiName(), serverUrl, txtParams, latency.TotalMilliseconds, e.GetType() + ": " + e.Message);
                throw e;
            }
        }