Beispiel #1
0
 public ActionResult GetAccessToken()
 {
     try
     {
         //开始获取access_token
         HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxaa58982117696428&secret=bcfbc6d635104cb072bc127a8635a32e");
         HttpWebResponse reponse = (HttpWebResponse)request.GetResponse();
         using (TextReader reader = new StreamReader(reponse.GetResponseStream()))
         {
             //JsonResult js= Json(reader.ReadToEnd(), JsonRequestBehavior.AllowGet);
             WXAccessTokenJson wtj = new WXAccessTokenJson();
             wtj = (WXAccessTokenJson)JsonToObject(reader.ReadToEnd(), wtj);
             //将access_token放到Session中,方便下次调用
             SessionManager.AccessToken = wtj.access_token;
             //开始获取jsapi_ticket
             request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", wtj.access_token));
             reponse = (HttpWebResponse)request.GetResponse();
             TextReader reader2 = new StreamReader(reponse.GetResponseStream());
             WXAccessTokenJson wtj2 = new WXAccessTokenJson();
             wtj2 = (WXAccessTokenJson)JsonToObject(reader2.ReadToEnd(), wtj2);
             SessionManager.Ticket = wtj2.ticket;
             return RedirectToAction("GetShai");
         }
     }
     catch (Exception ex)
     {
         // Logger.LogInstance.Write(ex, MessageType.Error);
         return new JsonResult { Data = new { Flag = -1 } };
     }
 }
Beispiel #2
0
        public void WXVerification()
        {
            Utils.CreateFile(@"/log/Oauth22.txt", "wexin进来了...:");
            string varToken = "";

            #region --判断session 是否过期 7200秒
            //DateTime expireTime;
            //if (SessionManager.TokenExpireTime == null)
            //{
            //    expireTime = DateTime.Now.AddYears(-100);
            //}
            //else
            //{
            //    expireTime = DateTime.Parse(SessionManager.TokenExpireTime);
            //}
            //TimeSpan span = DateTime.Now - expireTime;
            //int leftSeconds = span.Seconds;
            //if (string.IsNullOrEmpty(SessionManager.AccessToken) || (string.IsNullOrEmpty(SessionManager.AccessToken) && leftSeconds > 7000))
            //{
            //    // token 已过期,重新获取
            //}
            #endregion

            //获取AccessToken
            #region --获取AccessToken
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa1ddf49daf3a8856&secret=b3eab302feba8ea9e4c9e865bf801310");
            HttpWebResponse reponse = (HttpWebResponse)request.GetResponse();
            using (TextReader reader = new StreamReader(reponse.GetResponseStream()))
            {
                WXAccessTokenJson wtj = new WXAccessTokenJson();
                wtj = (WXAccessTokenJson)JsonToObject(reader.ReadToEnd(), wtj);
                //将access_token放到Session中,方便下次调用
                SessionManager.AccessToken = wtj.access_token;
                varToken = wtj.access_token;
                SessionManager.TokenExpireTime = DateTime.Now.ToString();
            }
            #endregion

            //创建微信菜单
            #region -- 创建微信菜单
            FileStream ffss = new FileStream(HttpContext.Server.MapPath(@"/menu.txt"), FileMode.Open);
            StreamReader sr = new StreamReader(ffss, Encoding.GetEncoding("GBK"));
            string menu = sr.ReadToEnd();
            sr.Close();
            ffss.Close();
            string rr = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + varToken, menu);

            #endregion

            //发消息..............

            Utils.CreateFile(@"/log/Oauth22.txt", "消息开始...:" );
            #region --发送消息
            string weixin = "";
            weixin = PostInput();//获取xml数据
            if (!string.IsNullOrEmpty(weixin))
            {
                Utils.CreateFile(@"/log/Oauth22.txt", "消息meiyou ??");
                ResponseMsg(weixin);//调用消息适配器
            }
            #endregion
        }
Beispiel #3
0
        public ActionResult GetShai(string timeStamp, string url)
        {
            try
            {
                url = url.Replace("#", "&");
                string appId = System.Web.Configuration.WebConfigurationManager.AppSettings["APPID"];
                string appSecret = System.Web.Configuration.WebConfigurationManager.AppSettings["AppSecret"];
                string accessTokenURL = System.Web.Configuration.WebConfigurationManager.AppSettings["AccessTokenURL"];
                string ticketURL = System.Web.Configuration.WebConfigurationManager.AppSettings["TicketURL"];
                if (string.IsNullOrEmpty(SessionManager.Ticket))
                {
                    //开始获取access_token
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}?grant_type=client_credential&appid={1}&secret={2}", accessTokenURL, appId, appSecret));
                    HttpWebResponse reponse = (HttpWebResponse)request.GetResponse();
                    using (TextReader reader = new StreamReader(reponse.GetResponseStream()))
                    {
                        //JsonResult js= Json(reader.ReadToEnd(), JsonRequestBehavior.AllowGet);
                        WXAccessTokenJson wtj = new WXAccessTokenJson();
                        wtj = (WXAccessTokenJson)JsonToObject(reader.ReadToEnd(), wtj);
                        //将access_token放到Session中,方便下次调用
                        //VoBao.Base.Common.File_Ext.CreateFile(@"/log/1.txt", "wtj.access_token:" + wtj.access_token);
                        SessionManager.AccessToken = wtj.access_token;
                        //开始获取jsapi_ticket
                        request = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}?access_token={1}&type=jsapi", ticketURL, wtj.access_token));
                        reponse = (HttpWebResponse)request.GetResponse();
                        TextReader reader2 = new StreamReader(reponse.GetResponseStream());
                        WXAccessTokenJson wtj2 = new WXAccessTokenJson();
                        wtj2 = (WXAccessTokenJson)JsonToObject(reader2.ReadToEnd(), wtj2);
                        SessionManager.Ticket = wtj2.ticket;

                    }

                }
                if (!string.IsNullOrEmpty(SessionManager.Ticket))
                {
                    //String noncestr = Random.randomAlphanumeric(16);
                    //int timestamp = 0;
                    //string appId = "wxaa58982117696428";
                    string nonceStr = string.Empty, signature = string.Empty;
                    //开始获取sha1 签名字符串
                    nonceStr = Utils.NewUID().ToString();
                    //timestamp = ConvertDateTimeInt(DateTime.Now);
                    StringBuilder sbStr = new StringBuilder();
                    sbStr.AppendFormat("jsapi_ticket={0}", SessionManager.Ticket);
                    sbStr.AppendFormat("&noncestr={0}", nonceStr);
                    sbStr.AppendFormat("&timestamp={0}", timeStamp);
                    sbStr.AppendFormat("&url={0}", url);

                    byte[] cleanBytes = Encoding.Default.GetBytes(sbStr.ToString());
                    byte[] hashedBytes = System.Security.Cryptography.SHA1.Create().ComputeHash(cleanBytes);
                    signature = BitConverter.ToString(hashedBytes).Replace("-", "");
                    return new JsonResult { Data = new { Flag = 1, appId = appId, timestamp = timeStamp, nonceStr = nonceStr, signature = signature } };
                }
                else
                {
                    return new JsonResult { Data = new { Flag = -1, Message = "Ticket为空,可能会造成分享朋友圈失败,请尝试退出微信后重新进入" } };
                }
            }
            catch (Exception ex)
            {
                return new JsonResult { Data = new { Flag = -1, Message = "Exception,可能会造成分享朋友圈失败,请尝试退出微信后重新进入" } };
            }
        }