這篇“C#怎么實(shí)現(xiàn)微信小程序服務(wù)端獲取用戶解密信息”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“C#怎么實(shí)現(xiàn)微信小程序服務(wù)端獲取用戶解密信息”文章吧。
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),炎陵企業(yè)網(wǎng)站建設(shè),炎陵品牌網(wǎng)站建設(shè),網(wǎng)站定制,炎陵網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,炎陵網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。C#微信小程序服務(wù)端獲取用戶解密信息實(shí)例代碼
實(shí)現(xiàn)代碼:
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 { /// <summary> /// wxapi 的摘要說(shuō)明 /// </summary> 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"; //向微信服務(wù)端 使用登錄憑證 code 獲取 session_key 和 openid string url = "/tupian/20230522/jscode2session string type = "utf-8"; AIOWeb.Models.GetUsersHelper GetUsersHelper = new AIOWeb.Models.GetUsersHelper(); string j = GetUsersHelper.GetUrltoHtml(url, type);//獲取微信服務(wù)器返回字符串 //將字符串轉(zhuǎn)換為json格式 JObject jo = (JObject)JsonConvert.DeserializeObject(j); result res = new result(); try { //微信服務(wù)器驗(yàn)證成功 res.openid = jo["openid"].ToString(); res.session_key = jo["session_key"].ToString(); } catch (Exception) { //微信服務(wù)器驗(yàn)證失敗 res.errcode = jo["errcode"].ToString(); res.errmsg = jo["errmsg"].ToString(); } if (!string.IsNullOrEmpty(res.openid)) { //用戶數(shù)據(jù)解密 GetUsersHelper.AesIV = iv; GetUsersHelper.AesKey = res.session_key; string result = GetUsersHelper.AESDecrypt(encryptedData); //存儲(chǔ)用戶數(shù)據(jù) JObject _usrInfo = (JObject)JsonConvert.DeserializeObject(result); userInfo userInfo = new userInfo(); userInfo.openId = _usrInfo["openId"].ToString(); try //部分驗(yàn)證返回值中沒(méi)有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 //創(chuàng)建連接池對(duì)象(與數(shù)據(jù)庫(kù)服務(wù)器進(jìn)行連接) SqlConnection conn = new SqlConnection("server=127.0.0.1;database=Test;uid=sa;pwd=1"); //打開連接池 conn.Open(); //創(chuàng)建命令對(duì)象 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() + "','來(lái)自微信小程序','1')"; SqlCommand cmdUp = new SqlCommand(str, conn); // 執(zhí)行操作 try { int row = cmdUp.ExecuteNonQuery(); } catch (Exception ex) { context.Response.Write(ex.ToString()); } } else { //多次訪問(wèn),記錄訪問(wèn)次數(shù)counts 更新unionId是預(yù)防最初沒(méi)有,后期關(guān)聯(lián)后卻仍未記錄 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(); } //關(guān)閉連接池 conn.Close(); #endregion //返回解密后的用戶數(shù)據(jù) context.Response.Write(result); } else { context.Response.Write(j); } } public bool IsReusable { get { return false; } } } }
GetUsersHelper 幫助類
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 { /// <summary> /// 獲取鏈接返回?cái)?shù)據(jù) /// </summary> /// <param name="Url">鏈接</param> /// <param name="type">請(qǐng)求類型</param> /// <returns></returns> 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 微信小程序用戶數(shù)據(jù)解密 public static string AesKey; public static string AesIV; /// <summary> /// AES解密 /// </summary> /// <param name="inputdata">輸入的數(shù)據(jù)encryptedData</param> /// <param name="AesKey">key</param> /// <param name="AesIV">向量128</param> /// <returns name="result">解密后的字符串</returns> 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 } }
以上就是關(guān)于“C#怎么實(shí)現(xiàn)微信小程序服務(wù)端獲取用戶解密信息”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)標(biāo)題:C#怎么實(shí)現(xiàn)微信小程序服務(wù)端獲取用戶解密信息-創(chuàng)新互聯(lián)
本文來(lái)源:http://jinyejixie.com/article24/dcjcje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、Google、全網(wǎng)營(yíng)銷推廣、電子商務(wù)、網(wǎng)站設(shè)計(jì)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容