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 implement WeChat Mini Programs server to obtain user decryption information by C#

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

Share

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

Most people do not understand the knowledge points of this article "how to achieve WeChat Mini Programs server to obtain user decryption information", so the editor summarizes the following contents for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "how to achieve WeChat Mini Programs server to obtain user decryption information".

C# WeChat Mini Programs server obtains the instance code of user decryption information

Implementation code:

Summary description of using AIOWeb.Models; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; namespace AIOWeb {/ wxapi / / public class wxapi: IHttpHandler {public void ProcessRequest (HttpContext context) {context.Response.ContentType = "text/plain"; string code = ""; string iv = "" String encryptedData = ""; try {code = HttpContext.Current.Request.QueryString ["code"] .ToString (); iv = HttpContext.Current.Request.QueryString ["iv"] .ToString (); encryptedData = HttpContext.Current.Request.QueryString ["encryptedData"] .ToString ();} catch (Exception ex) {context.Response.Write (ex.ToString ()) } string Appid = "wxdb2641f85b04f1b3"; string Secret = "8591d8cd7197b9197e17b3275329a1e7"; string grant_type= "authorization_code"; / / use login credential code to obtain session_key and openid string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + Appid +" & secret= "+ Secret +" & js_code= "+ code +" & grant_type= "+ grant_type from Wechat server String type = "utf-8"; AIOWeb.Models.GetUsersHelper GetUsersHelper = new AIOWeb.Models.GetUsersHelper (); string j = GetUsersHelper.GetUrltoHtml (url, type); / / get the string returned by Wechat server / / convert the string to json format JObject jo = (JObject) JsonConvert.DeserializeObject (j); result res = new result () Try {/ / Wechat server verification succeeded res.openid = jo ["openid"] .ToString (); res.session_key = jo ["session_key"] .ToString ();} catch (Exception) {/ / Wechat server verification failed res.errcode = jo ["errcode"] .ToString () Res.errmsg = jo ["errmsg"]. ToString ();} if (! string.IsNullOrEmpty (res.openid)) {/ / user data decryption GetUsersHelper.AesIV = iv; GetUsersHelper.AesKey = res.session_key; string result = GetUsersHelper.AESDecrypt (encryptedData); / / Storage user data JObject _ usrInfo = (JObject) JsonConvert.DeserializeObject (result) UserInfo userInfo = new userInfo (); userInfo.openId = _ usrInfo ["openId"] .ToString (); try / / partial verification returns no unionId {userInfo.unionId = _ usrInfo ["unionId"] .ToString ();} catch (Exception) {userInfo.unionId = "unionId" } userInfo.nickName = _ usrInfo ["nickName"]. ToString (); userInfo.gender = _ usrInfo ["gender"]. ToString (); userInfo.city = _ usrInfo ["city"]. ToString (); userInfo.province = _ usrInfo ["province"]. ToString (); userInfo.country = _ usrInfo ["country"]. ToString (); userInfo.avatarUrl = _ usrInfo ["avatarUrl"]. ToString () Object watermark = _ usrInfo ["watermark"] .ToString (); object appid = _ usrInfo ["watermark"] ["appid"] .ToString (); object timestamp = _ usrInfo ["watermark"] ["timestamp"] .ToString (); # region / / create a connection pool object (connect to the database server) SqlConnection conn = new SqlConnection ("server=127.0.0.1;database=Test;uid=sa;pwd=1") / / Open connection pool conn.Open (); / / create command object string Qrystr = "SELECT * FROM WeChatUsers WHERE openId='" + userInfo.openId + "'"; SqlCommand cmdQry = new SqlCommand (Qrystr, conn); object obj = cmdQry.ExecuteScalar () If ((Object.Equals (obj, null)) | | (Object.Equals (obj, System.DBNull.Value)) {string str = "INSERT INTO WeChatUsers ([UnionId], [OpenId], [NickName], [Gender], [City], [Province], [Country], [AvatarUrl], [Appid], 1592866058, [Memo], [counts]) VALUES ('+ userInfo.unionId +','+ userInfo.openId +",'+ userInfo.nickName + "' "+ userInfo.gender +", "+ userInfo.city +", "" + userInfo.province + ","+ userInfo.country +", "" + userInfo.avatarUrl + "',"+ appid.ToString () +", "" + timestamp.ToString () + ", 'from WeChat Mini Programs','1')" SqlCommand cmdUp = new SqlCommand (str, conn); / / execute operation try {int row = cmdUp.ExecuteNonQuery ();} catch (Exception ex) {context.Response.Write (ex.ToString ()) }} else {/ / multiple visits, record the number of visits counts update unionId is not prevented at first, but still not recorded after the later association string str = "UPDATE dbo.WeChatUsers SET counts = counts+1,UnionId ='" + userInfo.unionId + "'WHERE OpenId='" + userInfo.openId + "'"; SqlCommand cmdUp = new SqlCommand (str, conn) Int row = cmdUp.ExecuteNonQuery ();} / / close connection pool conn.Close (); # endregion / / return decrypted user data context.Response.Write (result);} else {context.Response.Write (j) }} public bool IsReusable {get {return false;}

GetUsersHelper help class

Using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks Namespace AIOWeb.Models {public class GetUsersHelper {/ Link / request type / public string GetUrltoHtml (string Url, string type) {try {System.Net.WebRequest wReq = System.Net.WebRequest.Create (Url); / / Get the response instance. System.Net.WebResponse wResp = wReq.GetResponse (); System.IO.Stream respStream = wResp.GetResponseStream (); / / Dim reader As StreamReader = New StreamReader (respStream) using (System.IO.StreamReader reader = new System.IO.StreamReader (respStream, Encoding.GetEncoding (type) {return reader.ReadToEnd ();}} catch (System.Exception ex) {return ex.Message }} # region WeChat Mini Programs user data decryption public static string AesKey; public static string AesIV / AES decrypts / input data encryptedData / key / / Vector 128C / string public string AESDecrypt (string inputdata) {try {AesIV = AesIV.Replace ("", "+"); AesKey = AesKey.Replace ("", "+") Inputdata = inputdata.Replace ("", "+"); byte [] encryptedData = Convert.FromBase64String (inputdata); RijndaelManaged rijndaelCipher = new RijndaelManaged (); rijndaelCipher.Key = Convert.FromBase64String (AesKey); / / Encoding.UTF8.GetBytes (AesKey); rijndaelCipher.IV = Convert.FromBase64String (AesIV); / / Encoding.UTF8.GetBytes (AesIV); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7 ICryptoTransform transform = rijndaelCipher.CreateDecryptor (); byte [] plainText = transform.TransformFinalBlock (encryptedData, 0, encryptedData.Length); string result = Encoding.UTF8.GetString (plainText); return result;} catch (Exception) {return null }} # endregion}} the above is about the content of this article on "how to achieve WeChat Mini Programs server to obtain user decryption information". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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