第一次开发微信版网页,对最重要的获取微信OpenId,特此记录下来

1.首先得有appid和appsecret

  1. . public class WeiXin {
  2.  
  3. public static string appid {
  4. get {
  5. string _appid = "wx3xxxxxxxxxxxxxxx";
  6. return _appid;
  7. }
  8. }
  9. public static string aseret {
  10. get {
  11. string appsecret = "b6719276d539796d94bxxxxxxxxxxxxxxx";
  12. return appsecret;
  13. }
  14. }
  15.  
  16. }

准备appid和appsecret

2.只获取用户的openID,,在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开如下页面,以snsapi_base为scope发起的网页授权,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(下面代码中的url参数就是回调页,静态的可以写成:string url = https://wx.baidu.com/controller/GetOpenId,注意URL需要进行HttpUtility.UrlEncode(url)编码,还有回调页的域名需要和微信公众号里面设置的回调域名相同)

  1. public class ApplyVIPController : Controller
  2. {
  3.  
  4. // GET: /ApplyVIP/
  5.  
  6. public ActionResult Index(string url)
  7. {
  8. string _url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}#wechat_redirect",
  9. WeiXin.appid,
  10. url,//回调页URL
  11. Guid.NewGuid().ToString("N"));
  12. return Redirect(_url);//这里微信会自动取出回调页URL,并且跳转到该url所属的页面
  13. }

静默授权并且跳转回调页

3.获取code,并且通过code获取Openid,正确时返回的JSON数据包如下:{ "access_token":"ACCESS_TOKEN",    "expires_in":7200,   "refresh_token":"REFRESH_TOKEN",     "openid":"OPENID",    "scope":"SCOPE" },这里面就包含了所需要的OPENID

  1. //controller
  2. public string GetOpenId() {
  3. string code = requset.querystring["code"];
  4. string openid = "";
  5. string json = "";
  6. string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code "//通过appid,appaseret,code
  7. , WeiXin.appid, WeiXin.aseret, code);
  8. HttpQuery.Get(url, null, msg => {
  9. json = msg;
  10. });
  11. JObject job = (JObject)JsonConvert.DeserializeObject(json);
  12. openid = job["openid"].ToString();
  13. return openid;
  14. }

4.请求获取Openid的httpquery.get()方法

  1. public class HttpQuery {
  2. private static readonly string DefaultUserAgent =
  3. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
  4.  
  5. public static void Get(string url, object data, Action<string> callback) {
  6. IDictionary<string, string> parameters = Getparameters(data);
  7.  
  8. if (!(parameters == null || parameters.Count == )) {
  9. url += "?";
  10. foreach (var item in parameters) {
  11. url += item.Key + "=" + item.Value + "&";
  12. }
  13. }
  14. CreateGetHttpResponse(url, null, null, null, callback);
  15. }
  16. /// <summary>
  17. /// 创建GET方式的HTTP请求
  18. /// </summary>
  19. /// <param name="url">请求的URL</param>
  20. /// <param name="timeout">请求的超时时间</param>
  21. /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
  22. /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
  23. /// <returns></returns>
  24. private static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent,
  25. CookieCollection cookies, Action<string> callback, string encoding = "utf-8") {
  26. if (string.IsNullOrEmpty(url)) {
  27. return null;
  28. //throw new ArgumentNullException("url");
  29. }
  30. try {
  31. HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  32. request.Method = "GET";
  33. request.UserAgent = DefaultUserAgent;
  34. if (!string.IsNullOrEmpty(userAgent)) {
  35. request.UserAgent = userAgent;
  36. }
  37. if (timeout.HasValue) {
  38. request.Timeout = timeout.Value;
  39. }
  40. if (cookies != null) {
  41. request.CookieContainer = new CookieContainer();
  42. request.CookieContainer.Add(cookies);
  43. }
  44.  
  45. HttpWebResponse httpWebResponse = request.GetResponse() as HttpWebResponse;
  46.  
  47. StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(),
  48. System.Text.Encoding.GetEncoding(encoding));
  49.  
  50. string html = "";
  51. //获取请求到的数据
  52. html = reader.ReadToEnd();
  53. //关闭
  54. httpWebResponse.Close();
  55. reader.Close();
  56.  
  57. callback(html);
  58. return httpWebResponse;
  59. }
  60. } catch {
  61. callback(null);
  62. }
  63. return null;
  64. }
  65.  
  66. }

MVC 微信开发获取用户OpenID的更多相关文章

  1. 微信开发获取用户OpenID

    第一次开发微信版网页,对最重要的获取微信OpenId,特此记录下来 1.首先得有appid和appsecret . public class WeiXin { public static string ...

  2. 微信开发 获取用户openId 与路由控制

    w实践,满足当前需求. www.w.com www.w.com/w1.php $wxurl='https://open.weixin.qq.com/connect/oauth2/authorize?a ...

  3. 微信接口-获取用户openid基本信息

    一.协助获取微信用户openid功能 https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri= ...

  4. 微信授权获取用户openId等信息

    在我们开发小程序的时候,需要通过授权获取用户的信息. 第一种使用wx.getUserInfo直接获取微信头像,昵称 // 必须是在用户已经授权的情况下调用 wx.getUserInfo({ succe ...

  5. 微信授权获取用户openid前端实现

    近来,倒霉的后台跟我说让我拿个openid做微信支付使用,寻思很简单,开始干活.   首先引导用户打开如下链接,只需要将appid修改为自己的就可以,redirect_url写你的重定向url   h ...

  6. ASP微信开发获取用户经纬度

    wx.config({ //debug: true, debug: true, appId: '<%= appId %>', timestamp: '<%= timestamp %& ...

  7. 微信测试号开发之九 微信网页授权:页面获取用户openid

    原文链接:https://blog.csdn.net/qq_37936542/article/details/78981369 一:配置接口 注意:这里填写的是域名(是一个字符串),而不是URL,因此 ...

  8. asp.net core 微信获取用户openid

    获取openid流程为首先根据微信开发参数构造AuthorizeUrl认证链接,用户跳转到该链接进行授权,授权完成将跳转到回调页(首次认证需要授权,后面将直接再跳转至回调页),此时回调页中带上一个GE ...

  9. 微信公众平台实现获取用户OpenID的方法

    这篇文章主要介绍了微信公众平台实现获取用户OpenID的方法,需要开发人员经过微信授权后获取高级接口才能使用此功能,用户OpenID对于微信公众平台建设有着非常广泛的用途,需要的朋友可以参考下 本文实 ...

随机推荐

  1. DirectX11--HLSL中矩阵的内存布局和mul函数探讨

    前言 说实话,我感觉这是一个大坑,不知道为什么要设计成这样混乱的形式. 在我用的时候,以row_major矩阵,并且mul函数以向量左乘矩阵的形式来绘制时的确能够正常显示,并不会有什么感觉.但是也有人 ...

  2. 基于Rabbit实现的RPC

    最近在学习项目中的通用技术,其中一个是在项目中会经常使用的基于RabbitMQ实现的RPC.这里一共有三个点要学习,分别是:RPC是什么?RabbitMQ是什么?如何使用RabbitMQ实现RPC.奔 ...

  3. Collections of Zujin Zhang's Published works

    I am not good, but I shall do my best to be better. Any questions, please feel free to contact zhang ...

  4. SQLServer数据库文件由高版本向低版本转换

    这个只能用2012的生成脚本功能,在高级里面把脚本兼容设置成2008,并且选择生成架构和数据(默认是只有架构)拿这个脚本在2008上跑一次就行了 sqlserver 中使用sqlcmd 执行*.sql ...

  5. RT-SA-2019-004 Cisco RV320 Unauthenticated Diagnostic DataRetrieval

    Advisory: Cisco RV320 Unauthenticated Diagnostic Data Retrieval RedTeam Pentesting discovered that t ...

  6. 查看和设置MySQL数据库字符集(转)

    查看和设置MySQL数据库字符集作者:scorpio 2008-01-21 10:05:17 标签: 杂谈 Liunx下修改MySQL字符集:1.查找MySQL的cnf文件的位置find / -ina ...

  7. vue组件化的应用

    前言:vue组件化的应用涉及到vue-cli的内容,所以在应用之前是需要安装node和vue-cli的,具体如何安装我就不一一赘述了.可能一会儿我心情好的时候,可以去整理一下. 1.应用的内容:在一个 ...

  8. XL4001 典型应用电路

    典型的应用电路如下: 中文数据手册:https://wenku.baidu.com/view/98ad2ed86f1aff00bed51ec7.html 在做毕设的时候用到的一款350ma的DC/DC ...

  9. 【easy】88. Merge Sorted Array 合并两个有序数组

    合并两个有序的list 把排序好的nums2插入nums1中,假设nums1这个vector的空间永远是够的 思路:倒序!! class Solution { public: void merge(v ...

  10. C# 微信开发-----微信会员卡(三)激活会员卡

    在会员领取了会员卡之后需要做 一个跳转性激活,模式请看下图: 在创建会员卡的时候需要配置下这个参数的值: memberActivate.aspx页面代码如下: <%@ Page Language ...