.NET6使用微信小程序授权登录获取手机号
更新时间:2023年08月16日 11:46:56 作者:黒貓警长
小程序手机号授权是在里打开小程序时弹窗请求允许使用某些功能,比如授权获取用户信息、授权获取手机号等,本文主要介绍了.NET6使用微信小程序授权登录获取手机号,感兴趣的可以了解一下
1.在appsettings配置你的小程序配置信息
//微信小程序信息配置 "WechatConfig": { "appid": "", //小程序ID "secret": "" //小程序秘钥 },
2.请求接口时先获取Access_token
#region 获取小程序的Access_token public object GetAccess_token(string appid, string secret) { string strUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl); req.Method = "GET"; req.ContentType = "textml;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)req.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); string jsonData = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); string jsonSr = jsonData; JObject jsons = JObject.Parse(jsonSr); string accesstoken = jsons["access_token"].ToString(); return accesstoken; } #endregion
3.授权时获取手机号
#region 获取微信绑定手机号 public object GetPhoneNumber(string code, string appid, string secret) { string errCode = ""; try { var accesstoken = GetAccess_token(appid, secret); string serviceAddress = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accesstoken; JObject codes = new JObject(); codes["code"] = code; string sendData = JsonConvert.SerializeObject(codes); //换成Post请求方式 string jsonString = UrlPost(sendData, serviceAddress); dynamic model = JToken.Parse(jsonString) as dynamic; JObject json = JObject.Parse(jsonString); errCode = json["errcode"].ToString(); string purePhoneNumber = json["phone_info"]["phoneNumber"].ToString(); //签发token //string token; //_authenservice.IsAuthenticated(code, out token); return new { Code = 200, Msg = "获取成功", Tel = purePhoneNumber, Token = token }; } catch (Exception e) { return new { Code = 0, Msg = "获取失败" + e.Message }; } } #endregion
4.微信接口要求必须使用Post请求方法
#region 提交Post请求 /// <summary> /// 提交Post请求 /// </summary> /// <param name="parameterData">参数集合</param> /// <param name="serviceUrl">URL</param> /// <param name="ContentType"></param> /// <param name="Accept"></param> /// <returns></returns> public static string UrlPost(string parameterData, string serviceUrl, string ContentType = "application/json", string Accept = "application/json") { //先根据用户请求的uri构造请求地址 //string serviceUrl = string.Format("{0}/{1}", this.BaseUri, uri); //创建Web访问对象 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); //把用户传过来的数据转成“UTF-8”的字节流 byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(parameterData); myRequest.Method = "POST"; //myRequest.Accept = "application/json"; //myRequest.ContentType = "application/json"; // //Content-Type: application/x-www-form-urlencoded myRequest.AutomaticDecompression = DecompressionMethods.GZip; myRequest.Accept = Accept; //myRequest.ContentType = ContentType; myRequest.ContentType = "application/json; charset=UTF-8"; myRequest.ContentLength = buf.Length; myRequest.MaximumAutomaticRedirections = 1; myRequest.AllowAutoRedirect = true; //myRequest.Headers.Add("content-type", "application/json"); //myRequest.Headers.Add("accept-encoding", "gzip"); //myRequest.Headers.Add("accept-charset", "utf-8"); //发送请求 Stream stream = myRequest.GetRequestStream(); stream.Write(buf, 0, buf.Length); stream.Close(); //通过Web访问对象获取响应内容 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法 string returnData = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾 reader.Close(); myResponse.Close(); return returnData; } #endregion
小程序官方文档
到此这篇关于.NET6使用微信小程序授权登录获取手机号的文章就介绍到这了,更多相关.NET6小程序授权获取手机号内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
asp.net的web页面(aspx)数据量过多时提交失败对策
asp.net的web页面,数据量过多时提交失败的情况想必有很多朋友都有遇到过吧,下面与大家分享下详细的解决方法2013-05-05阿里云上从ASP.NET线程角度对“黑色30秒”问题的全新分析
在这篇博文中,我们抛开对阿里云的怀疑,完全从ASP.NET的角度进行分析,看能不能找到针对问题现象的更合理的解释2015-09-09ASP.NET Core MVC 修改视图的默认路径及其实现原理解析
本章将和大家分享如何在ASP.NET Core MVC中修改视图的默认路径,以及它的实现原理,感兴趣的朋友跟随小编一起看看吧2021-09-09
最新评论