微信开放平台之网站授权微信登录功能
1 微信开放平台:https://open.weixin.qq.com/
2 微信官方教程:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN
3.pc页面显示
4. 通过官方提供的文档,我们可以看出一共分4个步骤
第一步:请求CODE
第二步:通过code获取access_token
第三步:通过access_token调用接口
第4步:获取用户个人信息(UnionID机制)
api:核心代码
public class weixin_helper { public weixin_helper() { } /// <summary> /// 根据AppID和AppSecret获得access token(默认过期时间为2小时) /// </summary> /// <returns>Dictionary</returns> public static Dictionary<string, object> get_access_token() { //获得配置信息 oauth_config config = oauth_helper.get_config(2); string send_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + config.oauth_app_id + "&secret=" + config.oauth_app_key + ""; //发送并接受返回值 string result = Utils.HttpGet(send_url); if (result.Contains("errmsg")) { return null; } try { Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(result); return dic; } catch { return null; } } /// <summary> /// 取得临时的Access Token(默认过期时间为2小时) /// </summary> /// <param name="code">临时Authorization Code</param> /// <param name="state">防止CSRF攻击,成功授权后回调时会原样带回</param> /// <returns>Dictionary</returns> public static Dictionary<string, object> get_access_token(string code, string state) { //获得配置信息 oauth_config config = oauth_helper.get_config(2); string send_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + config.oauth_app_id + "&secret=" + config.oauth_app_key + "&code="+code+"&grant_type=authorization_code"; //发送并接受返回值 string result = Utils.HttpGet(send_url); if (result.Contains("errmsg")) { return null; } try { Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(result); return dic; } catch { return null; } } /// <summary> /// 根据access_token判断access_token是否过期 /// </summary> /// <param name="access_token"></param> /// <returns>true表示未失效</returns> public static bool check_access_token(string access_token) { //获得配置信息 oauth_config config = oauth_helper.get_config(2); string send_url = "https://api.weixin.qq.com/sns/auth?access_token=" + access_token + "&openid=" + config.oauth_app_id; //发送并接受返回值 string result = Utils.HttpGet(send_url); try { Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(result); if (dic.ContainsKey("errmsg")) { if (dic["errmsg"].ToString()=="ok") { return true; } else { return false; } } return false; } catch { return false; } } /// <summary> /// 若fresh_token已过期则根据refresh_token取得新的refresh_token /// </summary> /// <param name="refresh_token">refresh_token</param> /// <returns>Dictionary</returns> public static Dictionary<string, object> get_refresh_token(string refresh_token) { //获得配置信息 oauth_config config = oauth_helper.get_config(2); string send_url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + config.oauth_app_id + "&grant_type=refresh_token&refresh_token=" + refresh_token; //发送并接受返回值 string result = Utils.HttpGet(send_url); if (result.Contains("errmsg")) { return null; } try { Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(result); return dic; } catch { return null; } } /// <summary> /// 获取登录用户自己的基本资料 /// </summary> /// <param name="access_token">临时的Access Token</param> /// <param name="open_id">用户openid</param> /// <returns>Dictionary</returns> public static Dictionary<string, object> get_user_info(string access_token, string open_id) { //获得配置信息 oauth_config config = oauth_helper.get_config(2); //发送并接受返回值 string send_url = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+open_id; //发送并接受返回值 string result = Utils.HttpGet(send_url); if (result.Contains("errmsg")) { return null; } //反序列化JSON Dictionary<string, object> dic = JsonHelper.DataRowFromJSON(result); return dic; } }
控制器的核心代码:
#region 微信登录 /// <summary> /// 微信登录 /// </summary> public ActionResult WeChat() { //获得配置信息 oauth_config config = oauth_helper.get_config(2); //主键id if (config == null) { return Content("出错了,您尚未配置微信相关的API信息!"); } string state = Guid.NewGuid().ToString().Replace("-", ""); Session["oauth_state"] = state; string send_url = "https://open.weixin.qq.com/connect/qrconnect?appid=" + config.oauth_app_id + "&redirect_uri=" + Utils.UrlEncode(config.return_uri.ToLower()) + "&response_type=code&scope=snsapi_login&state=" + state + "#wechat_redirect"; //开始发送 return Redirect(send_url); //跳转到微信自己 指定的关联登陆页面 } /// <summary> /// 微信登录返回action /// </summary> public ActionResult WeChatReturnUrl(string state, string code) { //取得返回参数 string access_token = string.Empty; string expires_in = string.Empty; string client_id = string.Empty; string openid = string.Empty; string refresh_token = string.Empty; if (Session["oauth_state"] == null || Session["oauth_state"].ToString() == "" || state != Session["oauth_state"].ToString() || string.IsNullOrEmpty(code))//若返回参数中未包含code或者state没有通过验证则提示出错 { return Content("出错啦,state未初始化!"); } //第一步:通过code来获取Access Token以及openid Dictionary<string, object> dic1 = weixin_helper.get_access_token(code, state); if (dic1 == null || !dic1.ContainsKey("access_token")) { return Content("错误代码:,无法获取Access Token,请检查App Key是否正确!"); } if (dic1 == null || !dic1.ContainsKey("openid")) { if (dic1.ContainsKey("errmsg")) { return Content("errcode:" + dic1["errcode"] + ",errmsg:" + dic1["errmsg"]); } else { return Content("出错啦,无法获取用户授权Openid!"); } } access_token = dic1["access_token"].ToString();//获取access_token expires_in = dic1["expires_in"].ToString();//获取过期时间 refresh_token = dic1["refresh_token"].ToString();//获取用于重新刷新access_token的凭证 openid = dic1["openid"].ToString();//用户唯一标示openid //储存获取数据用到的信息 Session["oauth_name"] = "webchat"; Session["oauth_access_token"] = access_token; Session["oauth_openid"] = openid; Session["oauth_refresh_token"] = refresh_token; #region todo 将获取到的用户信息保存到数据库中 #endregion //第二步:通过Access Token以及openid来获取用户的基本信息 //Dictionary<string, object> dic2 = weixin_helper.get_user_info(access_token,openid); //第三步:跳转到指定页面 return Content(WeChatResultJson()); } /// <summary> /// 微信登录返回action, 处理用户信息 /// </summary> public string WeChatResultJson() { string oauth_access_token = string.Empty; string oauth_openid = string.Empty; string oauth_name = string.Empty; string oauth_refresh_token = string.Empty; if (Session["oauth_name"] == null || Session["oauth_access_token"] == null || Session["oauth_openid"] == null) { return "{\"ret\":\"1\", \"msg\":\"出错啦,Access Token已过期或不存在!\"}"; } oauth_name = Session["oauth_name"].ToString(); oauth_access_token = Session["oauth_access_token"].ToString(); oauth_openid = Session["oauth_openid"].ToString(); oauth_refresh_token = Session["oauth_refresh_token"].ToString(); if (!weixin_helper.check_access_token(oauth_access_token)) //调用access_token前需判断是否过期 { Dictionary<string, object> dic1 = weixin_helper.get_refresh_token(oauth_refresh_token);//如果已过期则重新换取新的access_token if (dic1 == null || !dic1.ContainsKey("access_token")) { return "{\"openid\":\"0\", \"msg\":\"出错啦,无法获取access_token!\"}"; } oauth_access_token = dic1["access_token"].ToString(); } Dictionary<string, object> dic = weixin_helper.get_user_info(oauth_access_token, oauth_openid); if (dic == null) { return "{\"openid\":\"0\", \"msg\":\"出错啦,无法获取授权用户信息!\"}"; } try { StringBuilder str = new StringBuilder(); str.Append("{"); str.Append("\"openid\": \"" + dic["openid"].ToString() + "\", "); str.Append("\"nickname\": \"" + dic["nickname"].ToString() + "\", "); str.Append("\"sex\": \"" + dic["sex"].ToString() + "\", "); str.Append("\"province\": \"" + dic["province"].ToString() + "\", "); str.Append("\"city\": \"" + dic["city"].ToString() + "\", "); str.Append("\"country\": \"" + dic["country"].ToString() + "\", "); str.Append("\"headimgurl\": \"" + dic["headimgurl"].ToString() + "\", "); str.Append("\"privilege\": \"" + dic["privilege"].ToString() + "\", "); str.Append("\"unionid\": \"" + dic["unionid"].ToString() + "\""); str.Append("\"oauth_name\": \"" + oauth_name + "\""); str.Append("\"oauth_access_token\": \"" + oauth_access_token + "\""); str.Append("\"oauth_openid\": \"" + oauth_openid + "\""); str.Append("}"); return str.ToString(); } catch { return "{\"ret\":\"0\", \"msg\":\"出错啦,无法获取授权用户信息!\"}"; } } #endregion
栏 目:C#教程
下一篇:C#实现刷新桌面的方法
本文标题:微信开放平台之网站授权微信登录功能
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/6954.html
您可能感兴趣的文章
- 01-10C#微信开发(服务器配置)
- 01-10C#微信公众平台开发之高级群发接口
- 01-10C#微信公众平台开发之access
- 01-10C#微信开发之微信公众号标签管理功能
- 01-10C#微信公众号与订阅号接口开发示例代码
- 01-10C#微信开发第一章
- 01-10C#实现的微信网页授权操作逻辑封装示例
- 01-10WPF微信聊天和通讯录按钮样式代码分享
- 01-10使用TypeScript开发微信小程序的方法
- 01-10c#版在pc端发起微信扫码支付的实例
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-10delphi制作wav文件的方法
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 04-02jquery与jsp,用jquery
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11ajax实现页面的局部加载
- 01-10SublimeText编译C开发环境设置
- 01-10C#中split用法实例总结