Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use .net to realize the authentication of Wechat public service platform development

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report