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 develop Wechat Authentication message function by asp.net

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces asp.net how to develop Wechat authentication message function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to know about it.

Verify the authenticity of the message

Add a filter to the project where MVC Controller is located and override it in the filter

Public override void OnActionExecuting (ActionExecutingContext filterContext) method

Create a new data model

Note: when the server receives a message, it is no longer signature but msg_signature

Example of a HTTP request message that a Wechat server pushes messages to a server

POST / cgi-bin/wxpush? Msg_signature=477715d11cdb4164915debcba66cb864d751f3e6×tamp=1409659813&nonce=1372623149 HTTP/1.1

Host: qy.weixin.qq.com

Method override to validate the message

Call the verification method when Wechat is connected, but the parameters need to be changed slightly and the new data model is used.

Add filter properties on the Action method or on Controller

Code example

Model

/ / Wechat push message model / public class WeChatMsgRequestModel {public string timestamp {get; set;} public string nonce {get; set;} public string msg_signature {get; set;}}

Filter

Public class WeChatRequestValidAttribute: ActionFilterAttribute {private const string Token = "StupidMe"; public override void OnActionExecuting (ActionExecutingContext filterContext) {/ / Parameter adaptation Model.FormatModel.WeChatMsgRequestModel model = new Model.FormatModel.WeChatMsgRequestModel () {nonce= filterContext.HttpContext.Request.QueryString ["nonce"], msg_signature= filterContext.HttpContext.Request.QueryString ["msg_signature"], timestamp= filterContext.HttpContext.Request.QueryString ["timestamp"]} / / verify that if (CheckSignature (model)) {base.OnActionExecuting (filterContext);}} private bool CheckSignature (Model.FormatModel.WeChatMsgRequestModel model) {string signature, timestamp, nonce, tempStr; / / get the parameter signature = model.msg_signature; timestamp = model.timest nonce = model.nonce from the request / / 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); / / SHA1 encrypt a pair of strings tempStr = FormsAuthentication.HashPasswordForStoringInConfigFile (tempStr, "SHA1"). ToLower () / / determine whether signature is correct if (tempStr.Equals (signature)) {return true;} else {return false;}}

Controller Code

/ Log Assistant / private static Common.LogHelper logger = new Common.LogHelper (typeof (HomeController)) [Filters.WeChatRequestValid] public void Valid (Model.FormatModel.WeChatMsgRequestModel model) {if (ModelState.IsValid) {try {/ / determine whether it is a POST request if (HttpContext.Request.HttpMethod.ToUpper () = = "POST") {/ / get request information using (Stream stream) from the requested data stream = HttpContext.Request.InputStream) {byte [] postBytes = new byte [stream.Length] Stream.Read (postBytes, 0, (int) stream.Length); string postString = System.Text.Encoding.UTF8.GetString (postBytes); Handle (postString,model);} catch (Exception ex) {logger.Error ("exception occurs, exception message:" + ex.Message + ex.StackTrace) } Thank you for reading this article carefully. I hope the article "how to develop Wechat Authentication message function by asp.net" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

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