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 configure Token for ASP.net Core Wechat platform development

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge about how to configure Token in the development of ASP.net Core Wechat platform. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

First, log in to the developer platform and click the developer tools-"Public platform Test account"

Then we need to configure our interface information.

First create a configuration entity

Public class WeChatRequestModel {public string signature {get; set;} public string timestamp {get; set;} public string nonce {get; set;} public string echostr {get; set;}}

Then we only need a piece of code on the HomeController controller to configure Token.

/ / define Token, which is consistent with Token on Wechat public platform / private const string Token = "StupidMe"; public void Valid (WeChatRequestModel model) {/ / get the echostr parameter string echoStr = model.echostr; / / from the request, for security reasons. (you can also skip) if (CheckSignature (model)) {if (! string.IsNullOrEmpty (echoStr)) {/ / output the randomly generated echostr parameters as-is Response.WriteAsync (echoStr); / / cut off the output stream / / Response.end () }

But for security reasons, a CheckSignature (model) is created to verify (it may not be just unsafe)

/ verify the signature, verify whether the request is issued from the Wechat server / request parameter model Model / verify whether the request parameter signature = model.signature is obtained through private bool CheckSignature (WeChatRequestModel model) {string signature, timestamp, nonce, tempStr; / / Timestamp = model.timest nonce = model.nonce; / / create an array, add three parameters Token, timestamp, nonce to the array string [] array = {Token, timestamp, nonce}; / / sort Array.Sort (array); / / concatenate into a string tempStr = String.Join ("", array) / / encrypt a pair of strings with SHA1 tempStr = Get_SHA1_Method2 (tempStr); / / determine whether signature is correct if (tempStr.Equals (signature)) {return true;} else {return false;}}

Because signature is encrypted by SHA1, so we also have to do some SHA1 encryption before we can make a comparison.

Public string Get_SHA1_Method2 (string strSource) {string strResult = ""; / / Create System.Security.Cryptography.SHA1 md5 = System.Security.Cryptography.SHA1.Create (); / / Note the selection of encoding UTF8, UTF7, Unicode, etc. Byte [] bytResult = md5.ComputeHash (System.Text.Encoding.UTF8.GetBytes (strSource)) / / an array of byte types is converted to the string for (int I = 0; I < bytResult.Length; iResult+) {/ / hexadecimal conversion strResult = strResult + bytResult [I] .ToString ("X");} return strResult.ToLower ();}

After our ASP.net Core wrote the code, we released it. Note that the Wechat requirement must be port 80 and HTTP protocol.

But because of the trouble of applying for domain names and servers, I turned off my computer's firewall and published the project to the local IIS. Then buy a domain name in the peanut shell and match your own project (it cost me 18 yuan, distressed).

Only in this way, our most basic configuration code is completed, and then our Valid interface is successfully matched!

(maybe it will fail at first, because the verification is slow, and it will be successful if you click on it a few times.)

These are all the contents of the article "how to configure Token for ASP.net Core Wechat platform development". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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