微信开发记录1

微信公众号开发记录(1)

根据微信开公众开发平台官方文档,开发微信公众号的开发步骤

  1.申请微信公众开发平台账号,地址:https://mp.weixin.qq.com/

       2.进行微信开发的基本配置,完成 开发者的基本认证。【注意需要配置一个外网的地址,用于微信开发的身份认证】

  参考文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319

   官网给出的是PHP的代码示例,C# 验证的代码如下【网上搜索的仅供参考】

复制代码

  1  public class WxApi : IHttpHandler
  2     {
  3 
  4         public void ProcessRequest(HttpContext context)
  5         {
  6 
  7             string postString = string.Empty;
  8             if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
  9             {
 10                 using (Stream stream = HttpContext.Current.Request.InputStream)
 11                 {
 12                     Byte[] postBytes = new Byte[stream.Length];
 13                     stream.Read(postBytes, 0, (Int32)stream.Length);
 14                     postString = Encoding.UTF8.GetString(postBytes);
 15                 }
 16 
 17                 if (!string.IsNullOrEmpty(postString))
 18                 {
 19                     Execute(postString);
 20                 }
 21             }
 22             else
 23             {
 24                 Auth(); //微信接入的测试
 25             }
 26 
 27         }
 28 
 29         /// <summary>
 30         /// 成为开发者的第一步,验证并相应服务器的数据
 31         /// </summary>
 32         private void Auth()
 33         {
 34             string token = ConfigurationManager.AppSettings["WeixinToken"];//从配置文件获取Token
 35             if (string.IsNullOrEmpty(token))
 36             {
 37                 //LogTextHelper.Error(string.Format("WeixinToken 配置项没有配置!"));
 38             }
 39 
 40             string echoString = HttpContext.Current.Request.QueryString["echoStr"];
 41             string signature = HttpContext.Current.Request.QueryString["signature"];
 42             string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
 43             string nonce = HttpContext.Current.Request.QueryString["nonce"];
 44 
 45             if (CheckSignature(token, signature, timestamp, nonce))
 46             {
 47                 if (!string.IsNullOrEmpty(echoString))
 48                 {
 49                     HttpContext.Current.Response.Write(echoString);
 50                     HttpContext.Current.Response.End();
 51                 }
 52             }
 53         }
 54 
 55         #region 验证微信签名
 56         /// <summary>
 57         /// 验证微信签名
 58         /// </summary>
 59         public bool CheckSignature(string token, string signature, string timestamp, string nonce)
 60         {
 61             string[] ArrTmp = { token, timestamp, nonce };
 62 
 63             Array.Sort(ArrTmp);
 64             string tmpStr = string.Join("", ArrTmp);
 65 
 66             tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
 67             tmpStr = tmpStr.ToLower();
 68 
 69             if (tmpStr == signature)
 70             {
 71                 return true;
 72             }
 73             else
 74             {
 75                 return false;
 76             }
 77         }
 78         #endregion
 79 
 80 
 81         #region 处理各种请求信息并应答(通过POST的请求)
 82 
 83 
 84         /// <summary>
 85         /// 处理各种请求信息并应答(通过POST的请求)
 86         /// </summary>
 87         /// <param name="postStr">POST方式提交的数据</param>
 88         private void Execute(string postStr)
 89         {
 90             WeixinApiDispatch dispatch = new WeixinApiDispatch();
 91             string responseContent = dispatch.Execute(postStr);
 92 
 93             HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
 94             HttpContext.Current.Response.Write(responseContent);
 95         }
 96         #endregion
 97         public bool IsReusable
 98         {
 99             get
100             {
101                 return false;
102             }
103         }
104     }

复制代码

到这里完成了微型开发的接入,以及认证。

但是 微信平台接口权限的限制,很多权限都没有

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值