1 微信开放平台:https://open.weixin.qq.com/
3.pc页面显示
4. 通过官方提供的文档,我们可以看出一共分4个步骤
第一步:请求CODE
第二步:通过code获取access_token
第三步:通过access_token调用接口
第4步:获取用户个人信息(UnionID机制)
5.源码下载地址:http://pan.baidu.com/s/1pLyG66J
因代码加密:现贴出代码如下(下载后替换):
Controllers--》DefaultController.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Text; 5 using System.Web; 6 using System.Web.Mvc; 7 8 using WebWeChat.Models; 9 10 namespace WebWeChat.Controllers 11 { 12 public class DefaultController : Controller 13 { 14 #region 微信登录 15 16 /// <summary> 17 /// 微信登录 18 /// </summary> 19 public ActionResult WeChat() 20 { 21 //获得配置信息 22 string oauthAppId = ConfigurationManager.AppSettings["oauth_app_id"]; 23 string redirectUri = ConfigurationManager.AppSettings["Redirect_uri"]; 24 if (oauthAppId == null) 25 { 26 return this.Content("出错了,您尚未配置微信相关的API信息!"); 27 } 28 string state = Guid.NewGuid().ToString().Replace("-", ""); 29 this.Session["oauth_state"] = state; 30 string sendUrl = 31 "https://open.weixin.qq.com/connect/qrconnect?appid=" + oauthAppId + 32 "&redirect_uri=" + HttpUtility.UrlEncode(redirectUri.ToLower()) + 33 "&response_type=code&scope=snsapi_login&state=" + state + 34 "#wechat_redirect"; 35 36 //开始发送 37 return this.Redirect(sendUrl); //跳转到微信自己 指定的关联登陆页面 38 } 39 40 /// <summary> 41 /// 微信登录返回action 42 /// </summary> 43 public ActionResult WeChatReturnUrl(string state, string code) 44 { 45 //取得返回参数 46 47 if (this.Session["oauth_state"] == null || this.Session["oauth_state"].ToString() == "" || 48 state != this.Session["oauth_state"].ToString() || string.IsNullOrEmpty(code)) //若返回参数中未包含code或者state没有通过验证则提示出错 49 { 50 return this.Content("出错啦,state未初始化!"); 51 } 52 53 //第一步:通过code来获取Access Token以及openid 54 Dictionary<string, object> dic1 = WeixinHelper.get_access_token(code, state); 55 if (dic1 == null || !dic1.ContainsKey("access_token")) 56 { 57 return this.Content("错误代码:,无法获取Access Token,请检查App Key是否正确!"); 58 } 59 if (!dic1.ContainsKey("openid")) 60 { 61 return dic1.ContainsKey("errmsg") 62 ? this.Content("errcode:" + dic1["errcode"] + ",errmsg:" + dic1["errmsg"]) 63 : this.Content("出错啦,无法获取用户授权Openid!"); 64 } 65 66 var accessToken = dic1["access_token"].ToString(); 67 var refreshToken = dic1["refresh_token"].ToString(); 68 var openid = dic1["openid"].ToString(); 69 70 //储存获取数据用到的信息 71 this.Session["oauth_name"] = "webchat"; 72 this.Session["oauth_access_token"] = accessToken; 73 this.Session["oauth_openid"] = openid; 74 this.Session["oauth_refresh_token"] = refreshToken; 75 76 #region todo 将获取到的用户信息保存到数据库中 77 78 #endregion 79 80 //第二步:通过Access Token以及openid来获取用户的基本信息 81 //Dictionary<string, object> dic2 = weixin_helper.get_user_info(access_token,openid); 82 83 //第三步:跳转到指定页面 84 return this.Content(this.WeChatResultJson()); 85 } 86 87 /// <summary> 88 /// 微信登录返回action, 处理用户信息 89 /// </summary> 90 public string WeChatResultJson() 91 { 92 if (this.Session["oauth_name"] == null || this.Session["oauth_access_token"] == null || 93 this.Session[<