【微信小程序】c# 实现获取openid、session_key 服务端

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tianchao7c/article/details/83413558

c#写一个获取微信小程序 openid和session_key 的方法。。

1,微信小程序端

// 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res.code);
        wx.request({
          url: 'http://localhost:10000/getcode/getcode/',
          data:{
            'json_code': res.code
          },
          method: 'GET',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          success:function(res){
            console.log(res.data.data)
          }
        })
      }
    })

2,c#后台

#region 获取小程序openid 和session_key
        [HttpGet]
        public void GetCode(string json_code)
        {
            string serviceAddress = "https://api.weixin.qq.com/sns/jscode2session?appid=" + "APPID" + "&secret="
                + "APPSECRET" + "&js_code=" + json_code + "&grant_type=authorization_code";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
            request.Method = "GET";
            request.ContentType = "text/html;charset=utf-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, System.Text.Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            var obj = new
            {
                data = retString,
                Success = true
            };
            Formatting microsoftDataFormatSettings = default(Formatting);
            string result = JsonConvert.SerializeObject(obj, microsoftDataFormatSettings);
            Response.Write(result);
        }
        #endregion

然后就完成了。。。

上一篇:Android 使用 Gradle 多渠道打包


下一篇:ActiveMQ从源码构建