开发者社区> 问答> 正文

Net开发-获取Config参数值


       //相当于接口帐号密码
        const string strApiCorpId = "XXXXXXXX";
        const string strApiCorpSecret = "XXXXXXXX";
        const string strApiUriHead = "https://oapi.dingtalk.com/";
        static CookieContainer cookieCon = new CookieContainer();
        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name="time"> DateTime时间格式</param>
        /// <returns>Unix时间戳格式</returns>
        public int ConvertDateTimeInt(DateTime time)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }
        //获取token
        public string GetToken()
        {
            if (HttpContext.Current.Session["token"]==null)
            {
                const string strApiTokenUriFormat =
              strApiUriHead + "gettoken/?corpid=" + strApiCorpId + "&corpsecret=" + strApiCorpSecret;
                //获得access_token
                string strJson = HTMLHelper.GetHtml(strApiTokenUriFormat, cookieCon);

                TokenUserInfo info = ExpandFunction.ToInfo<TokenUserInfo>(strJson);
                HttpContext.Current.Session["token"] = info.access_token;
            }
            return CommonFunction.ToStr(HttpContext.Current.Session["token"]);
        }
        // 获取dd.config参数值
        public string GetDingDingConfig(string strUrl, string strQueryurl)
        {
            string strToken = GetToken();
            string strNowTime = CommonFunction.ToStr(ConvertDateTimeInt(DateTime.Now));
            if (HttpContext.Current.Session["ticket"] == null)
            {
                const string strApiGetJsApiTicket = strApiUriHead + "get_jsapi_ticket?access_token={0}&type=jsapi";
                string strJsapiJson = HTMLHelper.GetHtml(string.Format(strApiGetJsApiTicket, strToken), cookieCon);
                JsapiticketInfo ticketinfo = ExpandFunction.ToInfo<JsapiticketInfo>(strJsapiJson);
                HttpContext.Current.Session["ticket"] = ticketinfo.ticket;
            }
            string strticket = CommonFunction.ToStr(HttpContext.Current.Session["ticket"]);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("noncestr", "asdfg");
            dic.Add("timestamp", strNowTime);
            dic.Add("jsapi_ticket", strticket);
            dic.Add("url", "页面地址,需要跟微应用后台配置的微应用网址完全一样 包括大小写");
            var list = dic.OrderBy(s => s.Key);
            string param = "";
            foreach (var s in list)
                param += s.Key + "=" + s.Value + "&";
            param = param.Trim(new char[] { '&' });
            string strSignTrue = sha1(param);
            return "{'appId':'XXXXXX','corpId':'XXXXXX','timeStamp':'" + strNowTime + "','nonceStr':'asdfg','ticketinfo':'" + strticket + "','signature':'" + strSignTrue + "','access_token':'" + strToken + "'}";

        }
       //sha1 加密
        private static string sha1(string strSignTrue)
        {
            //建立SHA1对象
            SHA1 sha = new SHA1CryptoServiceProvider();

            //将mystr转换成byte[]
            ASCIIEncoding enc = new ASCIIEncoding();
            byte[] dataToHash = enc.GetBytes(strSignTrue);

            //Hash运算
            byte[] dataHashed = SHA1.Create().ComputeHash(dataToHash );

            //将运算结果转换成string
            string hash = BitConverter.ToString(dataHashed).Replace("-", "").ToLower();

            return hash;

        }

展开
收起
雷锋大人 2015-10-15 10:48:25 7045 0
3 条回答
写回答
取消 提交回答
  • ReNet开发-获取Config参数值
    请问,客户端怎么设置这个获得的config?

    -------------------------

    ReNet开发-获取Config参数值
    dic.OrderBy(s => s.Key);
    这句报错,没有这个方法啊?
    2015-10-19 16:15:58
    赞同 展开评论 打赏
  • 回1楼钉钉剑桥的帖子
    我这是可以的啊

    -------------------------

    回3楼junhm的帖子
    //ViewBag.TicketInfo 中就是getconfig方法返回来的值
    var varconfig = "@(Html.Raw(ViewBag.TicketInfo))";

            var data = eval("(" + varconfig + ")");
            dd.config({
                appId: data.appId,
                corpId: data.corpId,
                timeStamp: data.timeStamp,
                nonceStr: data.nonceStr,
                signature: data.signature,
                jsApiList: ['device.notification.confirm',
                            'device.notification.alert',
                            'biz.chat.pickConversation',
                            'biz.util.chosen',
                            'biz.ding.post']
            });

    -------------------------

    回4楼junhm的帖子
    这句话代表dic中按key值排序
    2015-10-16 10:39:43
    赞同 展开评论 打赏
  • ReNet开发-获取Config参数值
    调试不通么
    2015-10-15 18:21:21
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Build Your Next Apache Spark Job in .NET Using Mobius 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载