微信訂閱號,獲取用戶openid


在微信后台,啟用服務器配置。

服務器URL地址,要通過Token的驗證。

      private void Auth()
        {
            string token = ConfigurationManager.AppSettings["WeixinToken"];//從配置文件獲取Token
            if (string.IsNullOrEmpty(token))
            {               
                LogTextHelper.Error(string.Format("WeixinToken 配置項沒有配置!"));
            }

            string echoString = HttpContext.Current.Request.QueryString["echoStr"];
            string signature = HttpContext.Current.Request.QueryString["signature"];
            string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
            string nonce = HttpContext.Current.Request.QueryString["nonce"];

            if (CheckSignature(token, signature, timestamp, nonce))
            {
                if (!string.IsNullOrEmpty(echoString))
                {
                    HttpContext.Current.Response.Write(echoString);
                    HttpContext.Current.Response.End();
                }
            }
        }

  關注訂閱號,發一個文本消息。里面有用戶的openid

   /// <summary>
        /// 主入口
        /// </summary>
        /// <param name="msg"></param>
        private void OnRecvEvent(ReqMsgEventBase msg)
        {
            switch (msg.Event)
            {
                case Event.LOCATION:
                    break;
                case Event.subscribe:
                     string content = “openid=”+msg.FromUserName;
                    SendResponseText(msg, content);
                    break;
                case Event.unsubscribe:
                    break;
                default:
                    break;
            }

        }            

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM