.Net+Uniapp实现微信登录获取用户信息

Uniapp端:

          uni.login({
					success: (res2) => {
						var code = res2.code;
						uni.getUserInfo({
							success: (res3) => {
								var authInfo = res3;
								console.log(authInfo.userInfo);
								var data = {
									code:code,
									iv:authInfo.iv,
									rawData:authInfo.rawData,
									signature:authInfo.signature,
									encryptedData:authInfo.encryptedData,
								}
								
								uni.request({
									url: 'http://127.0.0.1:5000/api/wechat/login',
									method: 'POST',
									header: {
										'content-type': 'application/x-www-form-urlencoded'
									},
									data: {
										code:code,
										iv:authInfo.iv,
										rawData:authInfo.rawData,
										signature:authInfo.signature,
										encryptedData:authInfo.encryptedData,
									},
									success: (res) => {
										
									}
								})
							}
						});
					}
				})

 

后端代码:

        /// <summary>
        /// 微信小程序encryptedData解密
        /// </summary>
        /// <param name="encryptedDataStr"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string WeChatAES_decrypt(string encryptedDataStr, string key, string iv)
        {
            RijndaelManaged rijalg = new RijndaelManaged();
            //-----------------    
            //设置 cipher 格式 AES-128-CBC    

            rijalg.KeySize = 128;

            rijalg.Padding = PaddingMode.PKCS7;
            rijalg.Mode = CipherMode.CBC;

            rijalg.Key = Convert.FromBase64String(key);
            rijalg.IV = Convert.FromBase64String(iv);


            byte[] encryptedData = Convert.FromBase64String(encryptedDataStr);
            //解密    
            ICryptoTransform decryptor = rijalg.CreateDecryptor(rijalg.Key, rijalg.IV);

            string result;

            using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {

                        result = srDecrypt.ReadToEnd();
                    }
                }
            }

            return result;
        }





 /// <summary>
        /// 登录方法
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Login([FromForm]string code,[FromForm] string encryptedData, [FromForm] string iv)
        {

            var result = await HttpHelper.HttpGetAsync("https://api.weixin.qq.com/sns/jscode2session?appid="+ "xxx" + "&secret="+ "xxx" + "&js_code="+ code + "&grant_type=authorization_code");
            var sns = JsonConvert.DeserializeObject<WechatSns>(result);
            var ss = EncryptHelper.WeChatAES_decrypt(encryptedData, sns.session_key, iv);

            return Content("");
        }


   public class WechatSns
        {
            public string session_key { get; set; }
            public string openid { get; set; }
        }