public virtual HttpResponseMessage Login(WeChatLoginArgEntity arg) { BaseJsonResult <WeChatUserInfoEntity> resultMsg = null; Logger(this.GetType(), "微信登陆-Login", () => { if (this.CheckBaseArgument(arg, out resultMsg)) { HttpItem httpItem = new HttpItem { Url = string.Format(WeChatBaseInfo.I_GET_USER_INFO, arg.access_token, arg.openid, "zh_CN"), Method = "GET", ContentType = "application/json" }; HttpResult result = httpHelper.GetHtml(httpItem); if (result.StatusCode == HttpStatusCode.OK) { if (!string.IsNullOrEmpty(result.Html)) { BaseJsonResult <WeChatUserInfoEntity> jsonResult = this.PreprocessingWeChatData <WeChatUserInfoEntity>(result.Html); if (jsonResult.Status == (int)JsonObjectStatus.Success && jsonResult.Data != null) { WeChatUserInfoEntity userInfo = jsonResult.Data; resultMsg = this.GetBaseJsonResult <WeChatUserInfoEntity>(userInfo, JsonObjectStatus.Success); } else { resultMsg = jsonResult; } } } } }, e => { resultMsg = this.GetBaseJsonResult <WeChatUserInfoEntity>(JsonObjectStatus.Exception, ",异常信息:" + e.Message); }); return(resultMsg.TryToHttpResponseMessage()); }
/// <summary> /// 验证微信访问 /// </summary> public static void Auth(HttpContext webContext) { string requestURL = webContext.Request.Url.AbsoluteUri; try { // 用户访问微信页面有3种情况: // 1.第一次访问,没code // 2.有code,没cookie; // 3.有code,有cookie // 1.第一次访问,没code,没cookie:跳转到Oauth2.0认证 if (string.IsNullOrEmpty(webContext.Request["code"])) { string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect", CORPID, webContext.Server.UrlEncode(requestURL)); webContext.Response.Redirect(url, false); } else if (!string.IsNullOrEmpty(webContext.Request["code"]) && string.IsNullOrEmpty(CookieHelper.GetCookie("WXToken"))) { // 2.有code,没cookie:根据code获取userID string code = webContext.Request["code"]; string userId = ""; string userInfo = ""; #region 1)根据code获取userId string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}", GetAccessToken(), code); string responseText = HttpHelper.Instance.get(url); /* * API:http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3#.E6.A0.B9.E6.8D.AEcode.E8.8E.B7.E5.8F.96.E6.88.90.E5.91.98.E4.BF.A1.E6.81.AF * 正确的Json返回示例: * { * "UserId":"USERID", * "DeviceId":"DEVICEID" * } * 未关注企业号时返回: * { * "OpenId":"OPENID", * "DeviceId":"DEVICEID" * } * 错误的Json返回示例: * { * "errcode": "40029", * "errmsg": "invalid code" * } */ WeChatUserCodeEntity codeEn = JsonHelper.GetEntity <WeChatUserCodeEntity>(responseText); if (codeEn.errcode > 0) { throw new Exception(codeEn.errmsg); } else if (string.IsNullOrEmpty(codeEn.UserId)) { throw new Exception("请先关注企业号!"); } userId = codeEn.UserId; #endregion #region 2)根据userId获取用户信息 url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={0}&userid={1}", GetAccessToken(), userId); responseText = HttpHelper.Instance.get(url); /* * API:http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E8.8E.B7.E5.8F.96.E6.88.90.E5.91.98 * 正确的Json返回示例: * { * "errcode": 0, * "errmsg": "ok", * "userid": "zhangsan", * "name": "李四", * "department": [1, 2], * "position": "后台工程师", * "mobile": "15913215421", * "gender": "1", * "email": "*****@*****.**", * "weixinid": "lisifordev", * "avatar": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3WJ6DSZUfiakYe37PKnQhBIeOQBO4czqrnZDS79FH5Wm5m4X69TBicnHFlhiafvDwklOpZeXYQQ2icg/0", * "status": 1, * "extattr": {"attrs":[{"name":"爱好","value":"旅游"},{"name":"卡号","value":"1234567234"}]} * } * 错误的Json返回示例: * { * "errcode": "40029", * "errmsg": "invalid code" * } */ WeChatUserInfoEntity userInfoEn = JsonHelper.GetEntity <WeChatUserInfoEntity>(responseText); if (userInfoEn.errcode > 0) { throw new Exception(userInfoEn.errmsg); } userInfo = responseText; #endregion // 3.把userInfo传入到cookie里 CookieHelper.SetCookie("WXToken", userInfo, -1); } else if (!string.IsNullOrEmpty(webContext.Request["code"]) && !string.IsNullOrEmpty(CookieHelper.GetCookie("WXToken"))) { #region 3.有code,有cookie:校验cookie // TODO:在上面进行存入cookie时可采用AES加密,在这部进行解密校验 // CookieHelper.SetCookie("WXToken", "", -1); #endregion } else { throw new Exception("非授权访问!"); } } catch (Exception ex) { throw ex; } }