How to use .net to realize the authentication of Wechat public service platform development
Editor to share with you how to use .net to achieve Wechat public service platform development authentication, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
This code will be used once for authentication, and will not be used later:
Const string Token = "XXXXX"; / / your token protected void Page_Load (object sender, EventArgs e) {string postStr = ""; if (Request.HttpMethod.ToLower () = = "post") {System.IO.Stream s = System.Web.HttpContext.Current.Request.InputStream; byte [] b = new byte [s.Length]; s.Read (b, 0, (int) s.Length); postStr = System.Text.Encoding.UTF8.GetString (b) If (! string.IsNullOrEmpty (postStr)) {/ / ResponseMsg (postStr); Response.Write (ResponseMsg (postStr)); Response.End ();} / / WriteLog ("postStr:" + postStr);} else {Valid () }} / verify Wechat signature / * sort the three parameters token, timestamp and nonce in lexicographic order / * concatenate the three parameter strings into a string for sha1 encryption / * the encrypted string obtained by the developer can be compared with signature to indicate that the request originated from Wechat. / / private bool CheckSignature () {string signature = Request.QueryString ["signature"] .ToString (); string timestamp = Request.QueryString ["timestamp"] .ToString (); string nonce = Request.QueryString ["nonce"] .ToString (); string [] ArrTmp = {Token, timestamp, nonce}; Array.Sort (ArrTmp); / / in dictionary order string tmpStr = string.Join ("", ArrTmp); tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile (tmpStr, "SHA1"); tmpStr = tmpStr.ToLower () If (tmpStr = = signature) {return true;} else {return false;} .private void Valid () {string echoStr = Request.QueryString ["echoStr"] .private void Valid (); if (CheckSignature ()) {if (! string.IsNullOrEmpty (echoStr)) {Response.Write (echoStr); Response.End () }} / write log (for tracking) / private void WriteLog (string strMemo) {string filename = Server.MapPath ("/ logs/log.txt"); if (! Directory.Exists (Server.MapPath ("/ / logs//")) Directory.CreateDirectory ("/ / logs//"); StreamWriter sr = null Try {if (! File.Exists (filename)) {sr = File.CreateText (filename);} else {sr = File.AppendText (filename);} sr.WriteLine (strMemo);} catch {} finally {if (sr! = null) sr.Close () }} the above is all the contents of the article "how to use .net to achieve the certification of Wechat public service platform development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!