入门C#微信开发第一步
不想废话,直接写了!因为是留给自己做随笔的,所以大神们看到别喷…… 1.必须有微信公众账号 /// <summary> /// 验证微信签名 /// </summary> /// <returns></returns> /// * 将token、timestamp、nonce三个参数进行字典序排序 /// * 将三个参数字符串拼接成一个字符串进行sha1加密 /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。 private bool CheckSignature() { var token = "token"; var signature = HttpContext.Current.Request.QueryString["signature"]; var timestamp = HttpContext.Current.Request.QueryString["timestamp"]; var nonce = HttpContext.Current.Request.QueryString["nonce"]; var echostr = HttpContext.Current.Request.QueryString["echostr"]; string[] ArrTmp = { token, timestamp, nonce }; Array.Sort(ArrTmp); //字典排序 var tmpStr = string.Join("", ArrTmp); tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");//加密方式 if (tmpStr.ToLower() == signature) { return true; } return false; } 这代码相当于是跟微信公众号中【开发】--》【基本配置】里面你所写的Token 一个一对一令牌通信握手,只要互相通了,那么你就算是搞定了! 搞定是后话,还有工作没做完,接着写! 看到这图,你也明白下一步怎么玩了吧。本地iis配上了域名。这简直屌爆了………… #region 接收消息 /// <summary> /// 接收微信发送的XML消息并且解析 /// </summary> private void ReceiveXml() { var requestStream = HttpContext.Current.Request.InputStream; var requestByte = new byte[requestStream.Length]; requestStream.Read(requestByte, 0, (int)requestStream.Length); var requestStr = Encoding.UTF8.GetString(requestByte); if (!string.IsNullOrEmpty(requestStr)) { //封装请求类 var requestDocXml = new XmlDocument(); requestDocXml.LoadXml(requestStr); var rootElement = requestDocXml.DocumentElement; if (rootElement == null) return; var wxXmlModel = new WxXmlModel { ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText, FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText, CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText, MsgType = rootElement.SelectSingleNode("MsgType").InnerText }; switch (wxXmlModel.MsgType) { case "text"://文本 wxXmlModel.Content = rootElement.SelectSingleNode("Content").InnerText; break; case "image"://图片 wxXmlModel.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText; break; case "event"://事件 wxXmlModel.Event = rootElement.SelectSingleNode("Event").InnerText; if (wxXmlModel.Event != "TEMPLATESENDJOBFINISH")//关注类型 { wxXmlModel.EventKey = rootElement.SelectSingleNode("EventKey").InnerText; } break; default: break; } ResponseXML(wxXmlModel);//回复消息 } } #endregion #region 回复消息 private void ResponseXML(WxXmlModel WxXmlModel) { var QrCodeApi = new QrCodeApi(); var XML = ""; switch (WxXmlModel.MsgType) { case "text"://文本回复 XML = ResponseMessage.GetText(WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.Content); break; case "event": switch (WxXmlModel.Event) { case "subscribe": if (string.IsNullOrEmpty(WxXmlModel.EventKey)) { XML = ResponseMessage.GetText(WxXmlModel.FromUserName, WxXmlModel.ToUserName, "关注成功"); } else { XML = ResponseMessage.SubScanQrcode(WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.EventKey);//扫描带参数二维码先关注后推送事件 } break; case "SCAN": XML = ResponseMessage.ScanQrcode(WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.EventKey);//扫描带参数二维码已关注 直接推送事件 break; } break; default://默认回复 break; } HttpContext.Current.Response.Write(XML); HttpContext.Current.Response.End(); } #endregion 上面一个发送,一个接收,还是在WhApi.ashx处理程序文件里面。我就想说清楚,呵呵! 还缺一个配置,就是vs要设置【调试】----【附加到进程】,只需要把下面【显示所有用户进程】一项打勾,你就能找到w3wp.exe 如果有多个这样的进程,那你还是要确认【用户名】那一列,选中跟你程序池名称一样的就ok,点附加,确定附加! 以上就是入门C#微信开发第一步的详细内容,更多请关注php中文网其它相关文章! |
- 上一篇:.Net开发微信公众平台之自定义菜单代码详解
- 下一篇:微信开发之获取公众号配置