Home  >  Article  >  WeChat Applet  >  C# WeChat development (server configuration)

C# WeChat development (server configuration)

高洛峰
高洛峰Original
2017-01-17 10:28:571491browse

The editor is very interested in WeChat development, and has reviewed relevant articles on the Internet to organize them so that everyone can learn together.

1. Register an account - fill in the server configuration

Register an account on the WeChat public platform at https://mp.weixin.qq.com/;

The service number is For the WeChat public account applied by the company, the subscription number is applied by an individual and has relatively few personal permissions;

After logging in to the official website of the WeChat public platform, click the "Modify Configuration" button on the public platform backend management page - Developer Center page , fill in the server address (URL), Token and EncodingAESKey, where URL is the interface URL used by developers to receive WeChat messages and events. Token can be filled in by developers and used to generate signatures (the Token will be compared with the Token contained in the interface URL to verify security). EncodingAESKey is manually filled in by the developer or randomly generated, and will be used as the message body encryption and decryption key.

At the same time, developers can choose the message encryption and decryption methods: plain text mode, compatibility mode and security mode. The mode selection and server configuration will take effect immediately after submission. Developers are advised to fill in and select carefully. The default state of encryption and decryption is plain text mode. To select compatibility mode and security mode, you need to configure the relevant encryption and decryption codes in advance. For details, please refer to the documentation on message body signature and encryption and decryption.

C# WeChat development (server configuration)

#URL must be port 80 and can only be on the server! ! !

2. Verify whether the URL is valid

C# WeChat development (server configuration)

private string Token = ConfigurationManager.AppSettings["Token"];
 
[HttpGet]
[ActionName("Index")]
public ActionResult Get(string signature, string timestamp, string nonce, string echostr)
{
 if (CheckSignature.Check(signature, timestamp, nonce, Token))
 {
 return Content(echostr);//返回随机字符串则表示验证通过
 }
 else
 {
 return Content("failed:" + signature + "," + CheckSignature.GetSignature(timestamp, nonce, Token) + "。如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。");
 }
}
/// <summary>
/// 检查签名是否正确
/// </summary>
/// <param name="signature"></param>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
/// <param name="token"></param>
/// <returns></returns>
public static bool Check(string signature, string timestamp, string nonce, string token = null)
{
 return signature == GetSignature(timestamp, nonce, token);
}
 
/// <summary>
/// 返回正确的签名
/// </summary>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
/// <param name="token"></param>
/// <returns></returns>
public static string GetSignature(string timestamp, string nonce, string token = null)
{
 token = token ?? Token;
 var arr = new[] { token, timestamp, nonce }.OrderBy(z => z).ToArray();
 var arrString = string.Join("", arr);
 //var enText = FormsAuthentication.HashPasswordForStoringInConfigFile(arrString, "SHA1");//使用System.Web.Security程序集
 var sha1 = System.Security.Cryptography.SHA1.Create();
 var sha1Arr = sha1.ComputeHash(Encoding.UTF8.GetBytes(arrString));
 StringBuilder enText = new StringBuilder();
 foreach (var b in sha1Arr)
 {
 enText.AppendFormat("{0:x2}", b);
 }
 
 return enText.ToString();
}

Submit the configuration. Only if the verification is successful can you continue to use more functions. If the submission fails, you can write a log yourself to check the reason.

The editor is also new to WeChat development. I have compiled several articles on asp.net WeChat development before. Today I will start to compile the relevant knowledge about C# WeChat development. Since the editor is also a beginner, if there are any rigorous or mistakes Where, please forgive me and let's make progress together.

For more articles related to C# WeChat development (server configuration), please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP WeChat scratch cardNext article:PHP WeChat scratch card