轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 终极进阶 查看主题

面向新手《十八》获取微信访问用户的openid,解密 C# 代码

发布者: mrb | 发布时间: 2017-2-23 07:45| 查看数: 5531| 评论数: 1|帖子模式

本系列面对全新选手,非全新选后可以路过;
一:获取微信访问用户的openid

   在微信开发项目中,获取openid是项目常遇的问题,本文通过主要讲解实现在微信小程序中如何获取用户的openid,案例实现非常简单

   具体实现方法是通过登录接口获取登录凭证,然后通过request请求微信的公共API,将凭证转换得到我们需要的openid,需要用到的微信API有以下:

    1.wx.login    调用接口获取登录凭证

    2.wx.request   发起的是 HTTPS 请求

    3.通过将登录凭证转换获取openid

    具体调佣到的公共api和参数,大家可以通过微信文档查阅相关内容,本文就不做详解,主要说明开发中遇到的一个小问题:

    由于微信的wx.request请求有白名单机制,并且只能请求安装了ssl证书的网站,因此通过以上步骤获取openid之前,我们先要到微信小程序后台配置,将api.weixin.qq.com的域名添加进白名单,然后才能实现代码,一下是代码的实现案例:


[javascript] view plain copy
  1. //调用微信登录接口  
  2.  wx.login({  
  3.   success: function (loginCode) {  
  4.     var appid = ''//填写微信小程序appid  
  5.     var secret = ''//填写微信小程序secret  
  6.   
  7.     //调用request请求api转换登录凭证  
  8.     wx.request({  
  9.       url: '‘+appid+’&secret=‘+secret+’&grant_type=authorization_code&js_code='+loginCode.code,  
  10.       header: {  
  11.           'content-type''application/json'  
  12.       },  
  13.       success: function(res) {  
  14.         console.log(res.data.openid) //获取openid  
  15.       }  
  16.     })  
  17.   }  
  18. })  
二:wx.getUserInfo 解密 C# 代码

花了6小时,弄出来的代码。网上的是PHP代码

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static string DecodeUserInfo(string raw, string signature,string encryptedData, string iv)
       {  
            
           byte[] iv2 = Convert.FromBase64String(iv);
 
           if (string.IsNullOrEmpty(encryptedData)) return "";
           Byte[] toEncryptArray = Convert.FromBase64String(encryptedData);
 
           System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
           {
               Key = Convert.FromBase64String(session_key),
               IV = iv2,
               Mode = System.Security.Cryptography.CipherMode.CBC,
               Padding = System.Security.Cryptography.PaddingMode.PKCS7
           };
 
           System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
           Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
 
           return Encoding.UTF8.GetString(resultArray);
       
   }

 


最新评论

『酷锐云』@会长 发表于 2022-4-26 16:40
app买卖网站

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表