C#微信公众号/订阅号开发 接口源码
2016-12-29 00:00
477 查看
using System; using System.Web; using System.IO; using System.Text; using System.Web.Security; using weixin_api; public class wxgz_api : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string postString = string.Empty; if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST") { //微信服务器对接口消息 using (Stream stream = HttpContext.Current.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); postString = Encoding.UTF8.GetString(postBytes); Handle(postString); } } else { //微信进行的Get测试(开发者认证) WxAuth(); } } /// <summary> /// 处理信息并应答 /// </summary> private void Handle(string postStr) { messageHelp help = new messageHelp(); string responseContent = help.ReturnMessage(postStr); HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(responseContent); } #region 微信验证 public void WxAuth() { string token = "xxxxxxxx"; if (string.IsNullOrEmpty(token)) { return; } 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(); } } } /// <summary> /// 验证微信签名 /// </summary> public bool CheckSignature(string token, string signature, string timestamp, string nonce) { string[] ArrTmp = { token, timestamp, nonce }; Array.Sort(ArrTmp); string tmpStr = string.Join("", ArrTmp); tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); tmpStr = tmpStr.ToLower(); if (tmpStr == signature) { return true; } else { return false; } } #endregion public bool IsReusable { get { return false; } } }
相关文章推荐
- C#/ASP.NET MVC微信公众号接口开发之从零开发(三)回复消息 (附源码)
- C#/ASP.NET MVC微信公众号接口开发之从零开发(四) 微信自定义菜单(附源码)
- C#开发XML WebService接口(SOAP)
- C# .NET 开发框架 ClassGenerator 源码生成器 (C/S框架网)
- asp.net(c#) 开发taobao支付宝接口
- 第8章 C# .NET 开源可进行二次开发的即时通讯组件提供全部源码下载
- ASP.NET,C#.NET 通用权限系统组件源码使用说明,程序开发的尚方宝剑,程序架构标准参考,程序开发必备知识
- 即时通讯开发平台 (部分C#源码)
- WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
- 单机版简易考试系统开发过程讲解(C#注册机、用户注册、考试系统、有偿提供全部源码)
- 单机版简易考试系统开发过程讲解(C#注册机、用户注册、考试系统、有偿提供全部源码)
- asp.net(c#) 开发taobao支付宝接口
- 飞信WEB接口的一个返回网页C#处理源码
- 阿里软件接口开发基础(淘宝网) C#
- 第8章 C# .NET 开源可进行二次开发的即时通讯组件提供全部源码下载
- 在C#开发BHO时,关于IObjectWithsite接口实现问题
- 串行接口 C#开发
- 第8章 C# .NET 开源可进行二次开发的即时通讯组件提供全部源码下载
- 单机版简易考试系统开发过程讲解(C#注册机、用户注册、考试系统、有偿提供全部源码)
- WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法