Example #1
0
        /// <summary>
        /// 调用微信接口获取带参数永久二维码的ticket
        /// 使用方法:https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
        /// </summary>
        /// <param name="scene_id">二维码带的参数</param>
        /// <returns>json:ticket:换取二维码的凭证,expire_seconds:凭证有效时间,url:二维码解析后的地址。此处返回ticket 否则返回错误码</returns>
        public string GetQrcode(string appid, string appsecret, string scene_str)
        {
            string QrcodeUrl   = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}"; //WxQrcodeAPI接口
            string AccessToken = BasicApi.GetAccessToken(appid, appsecret);                          //拉取AccessToken

            QrcodeUrl = string.Format(QrcodeUrl, AccessToken);
            string PostJson = "{\"expire_seconds\": 1800, \"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": " + scene_str + "}}}";
            string ReText   = sohovan.com.common.CommonMethod.WebRequestPostOrGet(QrcodeUrl, PostJson);//此处省略了 WebRequestPostOrGet即为WebHttpRequest发送Post请求
            Dictionary <string, object> reDic = (Dictionary <string, object>)Jss.DeserializeObject(ReText);

            if (reDic.ContainsKey("ticket"))
            {
                return(reDic["ticket"].ToString());//成功
            }
            else
            {
                return(reDic["errcode"].ToString());//返回错误码
            }
        }
Example #2
0
        /// <summary>
        /// 获取JsApi权限配置的数组/四个参数
        /// </summary>
        /// <param name="Appid">应用id</param>
        /// <param name="Appsecret">密钥</param>
        /// <returns>json格式的四个参数</returns>
        public string GetJsApiInfo(string Appid, string Appsecret)
        {
            string timestamp    = CommonMethod.ConvertDateTimeInt(DateTime.Now).ToString();          //生成签名的时间戳
            string nonceStr     = CommonMethod.GetRandCode(16);                                      //生成签名的随机串
            string url          = System.Web.HttpContext.Current.Request.Url.AbsoluteUri.ToString(); //当前的地址
            string jsapi_ticket = "";

            //ticket 缓存7200秒
            if (System.Web.HttpContext.Current.Session["jsapi_ticket"] == null)
            {
                jsapi_ticket = CommonMethod.WebRequestPostOrGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + BasicApi.GetTokenSession(Appid, Appsecret) + "&type=jsapi", "");
                System.Web.HttpContext.Current.Session["jsapi_ticket"] = jsapi_ticket;
                System.Web.HttpContext.Current.Session.Timeout         = 7200;
            }
            else
            {
                jsapi_ticket = System.Web.HttpContext.Current.Session["jsapi_ticket"].ToString();
            }
            Dictionary <string, object> respDic = (Dictionary <string, object>)Jss.DeserializeObject(jsapi_ticket);

            jsapi_ticket = respDic["ticket"].ToString();//获取ticket
            string[] ArrayList = { "jsapi_ticket=" + jsapi_ticket, "timestamp=" + timestamp, "noncestr=" + nonceStr, "url=" + url };
            Array.Sort(ArrayList);
            string signature = string.Join("&", ArrayList);

            signature = FormsAuthentication.HashPasswordForStoringInConfigFile(signature, "SHA1").ToLower();
            return("{\"appId\":\"" + Appid + "\", \"timestamp\":" + timestamp + ",\"nonceStr\":\"" + nonceStr + "\",\"signature\":\"" + signature + "\"}");
        }