public string AuthoriseUserOpenId(string wechatCode, string usercode, string password) { //需要再次去获取openid string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", sAppID, sEncodingAESKey, wechatCode); string jsonResult = WechatHttpClientUtil.dooGet(url); JObject jo = (JObject)JsonConvert.DeserializeObject(jsonResult); //{ // "access_token":"ACCESS_TOKEN", // "expires_in":7200, // "refresh_token":"REFRESH_TOKEN", // "openid":"OPENID", // "scope":"SCOPE" //} string openId = jo["openid"].ToString(); LogUtil.WebLog("openid:" + openId); string refresh_token = jo["refresh_token"].ToString(); string access_token = jo["access_token"].ToString(); /*将openid\usercode\password直接传入业务接口进行验证,验证成功后进行绑定,并将关系保存在后台。 */ LogUtil.WebLog(jo); return("success"); }
public ActionResult GetAuthoriseState(string wechatCode) { LogUtil.WebLog("wechatCode:" + wechatCode); ReturnResult <UserInfo> result = new ReturnResult <UserInfo>(); string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", sAppID, sEncodingAESKey, wechatCode); string jsonResult = WechatHttpClientUtil.dooGet(url); JObject jo = (JObject)JsonConvert.DeserializeObject(jsonResult); //LogUtil.WebLog(jo); //{ // "access_token":"ACCESS_TOKEN", // "expires_in":7200, // "refresh_token":"REFRESH_TOKEN", // "openid":"OPENID", // "scope":"SCOPE" //} string openId = jo["openid"].ToString(); LogUtil.WebLog("openid:" + openId); string access_token = jo["access_token"].ToString(); LogUtil.WebLog("access_token:" + access_token); string refresh_token = jo["refresh_token"].ToString(); LogUtil.WebLog("refresh_token:" + refresh_token); string TryGetAccessToken = AccessTokenContainer.TryGetAccessToken(sAppID, sEncodingAESKey); LogUtil.WebLog("TryGetAccessToken:" + TryGetAccessToken); //string refresh_token = jo["refresh_token"].ToString(); //string access_token = jo["access_token"].ToString(); /*通过业务接口判断openid是否已绑定用户;如果已经绑定,返回用户信息,无需用户再登录; *如果未绑定,则留在绑定界面进行绑定数据 */ Random rd = new Random(); if (rd.Next(10) > 5) { result.code = -1; result.data = null; result.message = "该用户未绑定!"; } else { result.code = 1; result.data = new UserInfo { ID = "01c90cf1-084e-4444-b78a-ccb8b44d34be", userCode = "Lisx24", userName = "******", }; result.message = "该用户已经绑定!openid为:" + openId; } string urlUserinfo = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", TryGetAccessToken, openId); string jsonResultUserinfo = WechatHttpClientUtil.dooGet(urlUserinfo); JObject joUserinfo = (JObject)JsonConvert.DeserializeObject(jsonResultUserinfo); //{ // "subscribe": 1, // "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M", // "nickname": "Band", // "sex": 1, // "language": "zh_CN", // "city": "广州", // "province": "广东", // "country": "中国", // "headimgurl":"http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0", // "subscribe_time": 1382694957, // "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL" // "remark": "", // "groupid": 0, // "tagid_list":[128,2] //} LogUtil.WebLog("获取到的用户信息为:" + joUserinfo.ToString()); LogUtil.WebLog("方法调用成功!" + result.message); return(this.Json(result, JsonRequestBehavior.AllowGet)); }