Using C# WeChat Development Server Configuration Instructions

高洛峰
Release: 2017-03-12 09:07:56
Original
1623 people have browsed it

This article mainly introduces C# related content related to service server configuration in WeChat development. Interested friends can refer to it

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

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

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

After logging in to the official website of the WeChat public platform, manage the page in the backend of the public platform - On the developer center page, click the "Modify Configuration" button, fill in the server address (URL), Token and EncodingAESKey, where the URL is used by developers to receive WeChat messages and ##InterfaceURL of #event. The Token can be filled in by the developer arbitrarily and used to generate a signature (the Token will be compared with the Token contained in the interface URL to verify the 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 the encryption and decryption method is plaintext mode. Selecting compatibility mode and security mode requires configuring the relevant encryption and decryption codes in advance. For details, please refer to the documentation on message body signature and encryption and decryption.

Using C# WeChat Development Server Configuration Instructions

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

2. Verify whether the URL is valid

Using C# WeChat Development Server Configuration Instructions

##
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();
}
Copy after login

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

The editor has just come into contact with WeChat development. I have compiled several articles on

asp.net

WeChat development. Today I will start to compile relevant knowledge about C# WeChat development. Since I am also a beginner, If there are any mistakes or mistakes, please forgive me and we can make progress together.

The above is the detailed content of Using C# WeChat Development Server Configuration Instructions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!