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

1.首先得有appid和appsecret

 .    public class WeiXin {

         public static string appid {
get {
string _appid = "wx3xxxxxxxxxxxxxxx";
return _appid;
}
}
public static string aseret {
get {
string appsecret = "b6719276d539796d94bxxxxxxxxxxxxxxx";
return appsecret;
}
} }

准备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)编码,还有回调页的域名需要和微信公众号里面设置的回调域名相同)

     public class ApplyVIPController : Controller
{ // GET: /ApplyVIP/ public ActionResult Index(string url)
{
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",
WeiXin.appid,
url,//回调页URL
Guid.NewGuid().ToString("N"));
return Redirect(_url);//这里微信会自动取出回调页URL,并且跳转到该url所属的页面
}

静默授权并且跳转回调页

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

       //controller
public string GetOpenId() {
string code = requset.querystring["code"];
string openid = "";
string json = "";
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
, WeiXin.appid, WeiXin.aseret, code);
HttpQuery.Get(url, null, msg => {
json = msg;
});
JObject job = (JObject)JsonConvert.DeserializeObject(json);
openid = job["openid"].ToString();
return openid;
}

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

    public class HttpQuery {
private static readonly string DefaultUserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; public static void Get(string url, object data, Action<string> callback) {
IDictionary<string, string> parameters = Getparameters(data); if (!(parameters == null || parameters.Count == )) {
url += "?";
foreach (var item in parameters) {
url += item.Key + "=" + item.Value + "&";
}
}
CreateGetHttpResponse(url, null, null, null, callback);
}
/// <summary>
/// 创建GET方式的HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
private static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent,
CookieCollection cookies, Action<string> callback, string encoding = "utf-8") {
if (string.IsNullOrEmpty(url)) {
return null;
//throw new ArgumentNullException("url");
}
try {
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.UserAgent = DefaultUserAgent;
if (!string.IsNullOrEmpty(userAgent)) {
request.UserAgent = userAgent;
}
if (timeout.HasValue) {
request.Timeout = timeout.Value;
}
if (cookies != null) {
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
} HttpWebResponse httpWebResponse = request.GetResponse() as HttpWebResponse; StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(),
System.Text.Encoding.GetEncoding(encoding)); string html = "";
//获取请求到的数据
html = reader.ReadToEnd();
//关闭
httpWebResponse.Close();
reader.Close(); callback(html);
return httpWebResponse;
}
} catch {
callback(null);
}
return null;
} }

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. deepin 下安装goland中文字体显示全是方块

    下载中文字体 apt-get install ttf-arphic-uming xfonts-intl-chinese 替换goland的汉化包,两个jar包.https://blog.csdn.ne ...

  2. Resources (being shared)

    论文下载求助论坛 数学杂志的模版 答辩PPT模版 发一篇文章的经历 数学期刊名称缩写 英文书籍下载 英文书籍下载 中文书籍下载 数学分析高等代数考研试题官方下载地址

  3. springMVC中 @RequestParam和@RequestBody的区别

    首先,不可以同时传进@RequestParam和@RequestBody,好像可以传进两个@RequestParam 如果不加@requestparam修饰,相当于 加上@requestparam且各 ...

  4. Unity3D 热更新方案总结

    如何评价腾讯在Unity下的xLua(开源)热更方案? Unity 游戏用XLua的HotFix实现热更原理揭秘 腾讯开源手游热更新方案,Unity3D下的Lua编程 [Unity]基于IL代码注入的 ...

  5. UE4 AR开发笔记

    1.基础使用 ArToolKit:生成图片特征,可以用彩图.(图片先灰化)    genTexData效准相机.由于有的相机照相有弧度.  calib_camera 2.使用UE4ARPlugins做 ...

  6. Hough transform(霍夫变换)

    主要内容: 1.Hough变换的算法思想 2.直线检测 3.圆.椭圆检测 4.程序实现 一.Hough变换简介 Hough变换是图像处理中从图像中识别几何形状的基本方法之一.Hough变换的基本原理在 ...

  7. Django-1-URL路由系统

    一.分发地址 在APP中创建urls.py文件,将属于该APP的url地址都写入到这个文件中,当程序收到用户发送的请求时,先在根目录的urls.py文件中查找该地址属于哪个APP,将这个请求分发到该A ...

  8. Python Libhunt

    有一个网站,Python Libhunt: https://python.libhunt.com 这个网站类似于Github的Awesome xxx系列,不过它是随时更新的,排序方式,分类也更加友好. ...

  9. 响应消息的内容类型 text/html; charset=utf-8 与绑定(application/soap+xml; charset=utf-8)的内容类型不匹配。

    问题表述: 响应消息的内容类型 text/html; charset=utf-8 与绑定(application/soap+xml; charset=utf-8)的内容类型不匹配. 说明: 此类问题当 ...

  10. 【原创】大叔经验分享(8)创建hive表时用内部表还是外部表

    内部表和外部表最主要的一个差别就是删除表或者删除分区时,底层的文件是否自动删除,内部表会自动删除,外部表不会自动删除,所以基础数据表一定要用外部表,即使误删表或分区之后,还可以很容易的恢复回来. 虽然 ...