注:本文提到的代碼示例下載地址>如何用Azure Web App Services接入微信公衆號web
如何用Azure Web App Services接入微信公衆號安全
簡介服務器
此示例演示如何建立Azure Web App Services、開發、部暑、接入微信公衆號。微信
先決條件app
Microsoft Visual Studio 2015微信公衆平臺
運行示例ide
• 登陸http://portal.azure.com,建立Microsoft Azure 應用程序服務post
• 設置FTP部署憑據測試
• 設置Web.config中TOKEN的value,Token可由開發者能夠任意填寫,用做生成微信公衆號簽名(該Token會和接口URL中包含的Token進行比對,從而驗證安全性。ui
• 經過FTP把程序發佈部暑到Azure應用程序服務
• 登陸微信公衆平臺https://mp.weixin.qq.com,建立公衆平臺測試帳號
• 填寫服務器配置,驗證服務器地址的有效性,URL是開發者用來接收微信消息和事件的接口URL。TOKEN值需跟web.confi設置的值一致
• 關注測試公衆號,發送信息到公衆號,測試程序是否自動回覆內容
代碼
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>"; } }
更多信息