如何用Azure Web App Services接入微信公众号
2016-10-19 13:25 微软一站式示例代码库 阅读(1288) 评论(0) 编辑 收藏 举报注:本文提到的代码示例下载地址>如何用Azure Web App Services接入微信公众号
如何用Azure Web App Services接入微信公众号
简介
此示例演示如何创建Azure Web App Services、开发、部暑、接入微信公众号。
先决条件
Microsoft Visual Studio 2015
运行示例
• 登录http://portal.azure.com,创建Microsoft Azure 应用程序服务
• 设置FTP部署凭据
• 设置Web.config中TOKEN的value,Token可由开发者可以任意填写,用作生成微信公众号签名(该Token会和接口URL中包含的Token进行比对,从而验证安全性。
• 通过FTP把程序发布部暑到Azure应用程序服务
• 登录微信公众平台https://mp.weixin.qq.com,创建公众平台测试账号
• 填写服务器配置,验证服务器地址的有效性,URL是开发者用来接收微信消息和事件的接口URL。TOKEN值需跟web.confi设置的值一致
• 关注测试公众号,发送信息到公众号,测试程序是否自动回复内容
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | public static void Valid() { string signature = HttpContext.Current.Request[ "signature" ]; string timestamp = HttpContext.Current.Request[ "timestamp" ]; string nonce = HttpContext.Current.Request[ "nonce" ]; string echostr = HttpContext.Current.Request[ "echostr" ]; if (HttpContext.Current.Request.HttpMethod == "GET" ) { if (CheckSignature(signature, timestamp, nonce)) { HttpContext.Current.Response.Output.Write(echostr); } else { HttpContext.Current.Response.Output.Write( "Failed valid" ); } HttpContext.Current.Response.End(); } } public static void ResponseMsg() { if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST" ) { try { string postString = string .Empty; 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); } Hashtable postObj = ParseXml(postString); string fromUsername = postObj[ "FromUserName" ].ToString(); string toUsername = postObj[ "ToUserName" ].ToString(); string keyword = postObj[ "Content" ].ToString(); if (!String.IsNullOrEmpty(keyword)) { String responseContent = string .Format(Message_Text, fromUsername, toUsername, DateTime.Now.Ticks, "Welcome to OneCode OneScript!" + "\r\n<a href=\"https://gallery.technet.microsoft.com\">Click me</a>" ); HttpContext.Current.Response.Write(responseContent); } else { HttpContext.Current.Response.Write( "Input something..." ); } } catch (Exception ex) { Console.Error.WriteLine(ex.StackTrace); } } } private static bool CheckSignature(String signature, String timestamp, String nonce) { String[] arr = new String[] { ConfigurationManager.AppSettings[ "TOKEN" ].ToString(), 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()); return tmpStr != null ? tmpStr.Equals(signature) : false ; } private 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(); } private static Hashtable ParseXml(String xml) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xml); XmlNode bodyNode = xmlDocument.ChildNodes[0]; Hashtable ht = new Hashtable(); if (bodyNode.ChildNodes.Count > 0) { foreach (XmlNode xn in bodyNode.ChildNodes) { ht.Add(xn.Name, xn.InnerText); } } return ht; } private static string Message_Text { get { return @"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[{3}]]></Content> </xml>" ; } } |
更多信息
更多脚本样例, 访问微软One Code样例库:http://aka.ms/onescriptsamples
更多代码样例, 访问微软One Script样例库:http://aka.ms/onecodesamples
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!