asp.net微信公众平台开发

16 篇文章 0 订阅


http://mp.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97 

微信公众平台接口指南

微信公众平台的开发比较简单,首先是网址接入

公众平台用户提交信息后,微信服务器将发送GET请求到填写的URL上,并且带上四个参数:

参数描述
signature微信加密签名
timestamp时间戳
nonce随机数
echostr随机字符串

开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,否则接入失败。

signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。

加密/校验流程:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
<pre class="csharp" name="code">   /// <summary>
    /// 验证签名
    /// </summary>
    /// <param name="signature"></param>
    /// <param name="timestamp"></param>
    /// <param name="nonce"></param>
    /// <returns></returns>
    public static bool CheckSignature(String signature, String timestamp, String nonce)
    {
        String[] arr = new String[] { token, timestamp, nonce };
        // 将token、timestamp、nonce三个参数进行字典序排序  
        Array.Sort<String>(arr);

        StringBuilder content = new StringBuilder();
        for (int i = 0; i < arr.Length; i++)
        {
            content.Append(arr[i]);
        }

        String tmpStr = SHA1_Encrypt(content.ToString());


        // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信  
        return tmpStr != null ? tmpStr.Equals(signature) : false;
    }


    /// <summary>
    /// 使用缺省密钥给字符串加密
    /// </summary>
    /// <param name="Source_String"></param>
    /// <returns></returns>
    public static string SHA1_Encrypt(string Source_String)
    {
        byte[] StrRes = Encoding.Default.GetBytes(Source_String);
        HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
        StrRes = iSHA.ComputeHash(StrRes);
        StringBuilder EnText = new StringBuilder();
        foreach (byte iByte in StrRes)
        {
            EnText.AppendFormat("{0:x2}", iByte);
        }
        return EnText.ToString();
    }
 
接入后是消息推送当普通微信用户向公众账号发消息时,微信服务器将POST该消息到填写的URL上。
<pre class="html" name="code">    protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.HttpMethod.ToUpper() == "GET")
        {
            // 微信加密签名  
            string signature = Request.QueryString["signature"];
            // 时间戳  
            string timestamp = Request.QueryString["timestamp"];
            // 随机数  
            string nonce = Request.QueryString["nonce"];
            // 随机字符串  
            string echostr = Request.QueryString["echostr"];
            if (WeixinServer.CheckSignature(signature, timestamp, nonce))
            {
                Response.Write(echostr);
            }

        }
        else if (Request.HttpMethod.ToUpper() == "POST")
        {

           StreamReader stream = new StreamReader(Request.InputStream);
           string xml = stream.ReadToEnd();

           processRequest(xml);
        }

 
    }


    /// <summary>
    /// 处理微信发来的请求 
    /// </summary>
    /// <param name="xml"></param>
    public void processRequest(String xml)
    {
        try
        {

            // xml请求解析  
            Hashtable requestHT = WeixinServer.ParseXml(xml);

            // 发送方帐号(open_id)  
            string fromUserName = (string)requestHT["FromUserName"];
            // 公众帐号  
            string toUserName = (string)requestHT["ToUserName"];
            // 消息类型  
            string msgType = (string)requestHT["MsgType"];

            //文字消息
            if (msgType == ReqMsgType.Text)
            {
               // Response.Write(str);

               string content = (string)requestHT["Content"];
                if(content=="1")
                {
                     // Response.Write(str);
                    Response.Write(GetNewsMessage(toUserName, fromUserName));
                    return;
                }
                if (content == "2")
                {
                    Response.Write(GetUserBlogMessage(toUserName, fromUserName));
                     return;
                }
                if (content == "3")
                {
                    Response.Write(GetGroupMessage(toUserName, fromUserName));
                   return;
                }
                if (content == "4")
                {
                    Response.Write(GetWinePartyMessage(toUserName, fromUserName));
                    return;
                }
                Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,")); 

           }
           else if (msgType == ReqMsgType.Event)
           {
               // 事件类型  
               String eventType = (string)requestHT["Event"];
               // 订阅  
               if (eventType==ReqEventType.Subscribe)
               {
                   
                   Response.Write(GetMainMenuMessage(toUserName, fromUserName, "谢谢您的关注!,"));
                   
               }
               // 取消订阅  
               else if (eventType==ReqEventType.Unsubscribe)
               {
                   // TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息  
               }
               // 自定义菜单点击事件  
               else if (eventType==ReqEventType.CLICK)
               {
                   // TODO 自定义菜单权没有开放,暂不处理该类消息  
               }  
           }
           else if (msgType == ReqMsgType.Location)
           {
           }

  
        }
        catch (Exception e)
        {
            
        }
    }<PRE class=csharp name="code">    protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.HttpMethod.ToUpper() == "GET")
        {
            // 微信加密签名  
            string signature = Request.QueryString["signature"];
            // 时间戳  
            string timestamp = Request.QueryString["timestamp"];
            // 随机数  
            string nonce = Request.QueryString["nonce"];
            // 随机字符串  
            string echostr = Request.QueryString["echostr"];
            if (WeixinServer.CheckSignature(signature, timestamp, nonce))
            {
                Response.Write(echostr);
            }

        }
        else if (Request.HttpMethod.ToUpper() == "POST")
        {

           StreamReader stream = new StreamReader(Request.InputStream);
           string xml = stream.ReadToEnd();

           processRequest(xml);
        }

 
    }


    /// <summary>
    /// 处理微信发来的请求 
    /// </summary>
    /// <param name="xml"></param>
    public void processRequest(String xml)
    {
        try
        {

            // xml请求解析  
            Hashtable requestHT = WeixinServer.ParseXml(xml);

            // 发送方帐号(open_id)  
            string fromUserName = (string)requestHT["FromUserName"];
            // 公众帐号  
            string toUserName = (string)requestHT["ToUserName"];
            // 消息类型  
            string msgType = (string)requestHT["MsgType"];

            //文字消息
            if (msgType == ReqMsgType.Text)
            {
               // Response.Write(str);

               string content = (string)requestHT["Content"];
                if(content=="1")
                {
                     // Response.Write(str);
                    Response.Write(GetNewsMessage(toUserName, fromUserName));
                    return;
                }
                if (content == "2")
                {
                    Response.Write(GetUserBlogMessage(toUserName, fromUserName));
                     return;
                }
                if (content == "3")
                {
                    Response.Write(GetGroupMessage(toUserName, fromUserName));
                   return;
                }
                if (content == "4")
                {
                    Response.Write(GetWinePartyMessage(toUserName, fromUserName));
                    return;
                }
                Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,")); 

           }
           else if (msgType == ReqMsgType.Event)
           {
               // 事件类型  
               String eventType = (string)requestHT["Event"];
               // 订阅  
               if (eventType==ReqEventType.Subscribe)
               {
                   
                   Response.Write(GetMainMenuMessage(toUserName, fromUserName, "谢谢您的关注!,"));
                   
               }
               // 取消订阅  
               else if (eventType==ReqEventType.Unsubscribe)
               {
                   // TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息  
               }
               // 自定义菜单点击事件  
               else if (eventType==ReqEventType.CLICK)
               {
                   // TODO 自定义菜单权没有开放,暂不处理该类消息  
               }  
           }
           else if (msgType == ReqMsgType.Location)
           {
           }

  
        }
        catch (Exception e)
        {
            
        }
    }</PRE><BR>
<PRE></PRE>
<BR>
<BR>



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET 微信开发源码是为了在ASP.NET平台上实现微信开发而提供的源代码。微信开发是指利用微信提供的开发接口和功能,开发出基于微信平台的应用程序或网站,以实现与微信用户的交互和功能扩展。 ASP.NET是一种用于构建Web应用程序和网站的开发框架,而微信开发源码则是在ASP.NET框架下,基于微信公众平台或企业微信开发接口,实现了与微信的集成和功能开发。它包括了与微信平台的交互代码、消息处理代码、用户验证代码、素材管理代码等一系列功能模块的实现。 使用ASP.NET微信开发源码,可以方便地实现微信公众号或企业微信开发需求,例如消息回复、菜单管理、用户管理、素材上传下载等功能。借助ASP.NET框架的优势,可以高效地处理微信用户的请求,并根据需要进行业务逻辑处理和数据交互。 ASP.NET微信开发源码通常会包含一个可扩展的框架,使开发者能够快速搭建一个与微信平台对接的应用程序。开发者可以根据自己的需求和业务场景,对源码进行定制和扩展,以实现更复杂的功能或个性化的交互体验。 在使用ASP.NET微信开发源码进行微信开发时,需要了解微信平台开发接口和规范,并熟悉ASP.NET框架的开发方式与特点。通过合理利用ASP.NET微信开发源码,可以快速、高效地开发出符合微信用户需求的应用程序,并与微信平台进行无缝对接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值