/// <summary> /// 通过code换取网页授权access_token和openid的返回数据,正确时返回的JSON数据包如下: /// { /// "access_token":"ACCESS_TOKEN", /// "expires_in":7200, /// "refresh_token":"REFRESH_TOKEN", /// "openid":"OPENID", /// "scope":"SCOPE", /// "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" /// } /// 其中access_token可用于获取共享收货地址。 /// openid是微信支付jsapi支付接口统一下单时必须的参数。 /// 更详细的说明请参考网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html /// </summary> /// <param name="code"></param> public void GetOpenidAndAccessTokenFromCode(string code) { try { //构造获取openid及access_token的url WxData data = new WxData(); data.SetValue("appid", WxConfig.APPID); data.SetValue("secret", WxConfig.APPSECRET); data.SetValue("code", code); data.SetValue("grant_type", "authorization_code"); string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl(); //请求url以获取数据 string result = HttpService.Get(url); Log.Debug(this.GetType().ToString(), "通过code换取网页授权(GetOpenidAndAccessTokenFromCode) response数据 : " + result); JsonData jd = JsonMapper.ToObject(result); string str_access_token = ""; try { str_access_token = (string)jd["access_token"]; if (!string.IsNullOrEmpty(str_access_token)) { //保存access_token,用于收货地址获取 access_token = (string)jd["access_token"]; //获取用户openid openid = (string)jd["openid"]; Log.Debug(this.GetType().ToString(), "Get openid : " + openid); Log.Debug(this.GetType().ToString(), "Get access_token : " + access_token); } else { Log.Error(this.GetType().ToString(), "通过code换取网页授权失败 errcode : " + (string)jd["errcode"]); Log.Error(this.GetType().ToString(), "通过code换取网页授权失败 errmsg : " + (string)jd["errmsg"]); } } catch { Log.Error(this.GetType().ToString(), "通过code换取网页授权失败 errcode : " + (string)jd["errcode"]); Log.Error(this.GetType().ToString(), "通过code换取网页授权失败 errmsg : " + (string)jd["errmsg"]); } } catch (Exception ex) { Log.Error(this.GetType().ToString(), ex.ToString()); throw new WxException(ex.ToString()); } }
/// <summary> /// 通过OpenID获取微信用户信息 /// </summary> /// <returns></returns> private WXUserInfo GetUserInfo(string openid, string ACCESS_TOKEN) { try { WxData data = new WxData(); data.SetValue("access_token", ACCESS_TOKEN); data.SetValue("openid", openid); data.SetValue("lang", "zh_CN"); string url = "https://api.weixin.qq.com/cgi-bin/user/info?" + data.ToUrl(); //请求url以获取数据 string result = WebRequestHelper.Get(url); //保存用户信息 WXUserInfo wxuserinfo = JsonConvert.DeserializeObject <WXUserInfo>(result); DAL.Log.Instance.Write("请求:" + data.ToUrl() + ",返回:" + result, "获取微信用户信息"); return(wxuserinfo); } catch (Exception ex) { DAL.Log.Instance.Write(ex.ToString(), "获取微信用户信息错误"); throw new Exception(ex.ToString()); } }
/// <summary> /// 获取收货地址js函数入口参数,详情请参考收货地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9 /// 返回值:共享收货地址js函数需要的参数,json格式可以直接做参数使用 /// </summary> /// <returns></returns> public string GetEditAddressParameters() { string parameter = ""; try { string scheme = page.Request.Url.Scheme; string host = page.Request.Url.Host; string path = page.Request.Path; string queryString = page.Request.Url.Query; //这个地方要注意,参与签名的是网页授权获取用户信息时微信后台回传的完整url string url = scheme + "://" + host + path + queryString; //构造需要用SHA1算法加密的数据 WxData signData = new WxData(); signData.SetValue("appid", WxConfig.APPID); signData.SetValue("url", url); signData.SetValue("timestamp", WxPayApi.GenerateTimeStamp()); signData.SetValue("noncestr", WxPayApi.GenerateNonceStr()); signData.SetValue("accesstoken", access_token); string param = signData.ToUrl(); Log.Debug(this.GetType().ToString(), "SHA1 encrypt param : " + param); //SHA1加密 string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1"); Log.Debug(this.GetType().ToString(), "SHA1 encrypt result : " + addrSign); //获取收货地址js函数入口参数 WxData afterData = new WxData(); afterData.SetValue("appId", WxConfig.APPID); afterData.SetValue("scope", "jsapi_address"); afterData.SetValue("signType", "sha1"); afterData.SetValue("addrSign", addrSign); afterData.SetValue("timeStamp", signData.GetValue("timestamp")); afterData.SetValue("nonceStr", signData.GetValue("noncestr")); //转为json格式 parameter = afterData.ToJson(); Log.Debug(this.GetType().ToString(), "Get EditAddressParam : " + parameter); } catch (Exception ex) { Log.Error(this.GetType().ToString(), ex.ToString()); throw new WxException(ex.ToString()); } return(parameter); }
/// <summary> /// 生成签名 /// </summary> /// <param name="noncestr">随机字符串</param> /// <param name="timestamp">时间戳</param> /// <param name="jsapi_ticket"></param> /// <param name="url">调用JS接口页面的完整URL</param> /// <returns></returns> public static string MakeSign(string noncestr, string timestamp, string jsapi_ticket, string url) { if (string.IsNullOrEmpty(noncestr) || string.IsNullOrEmpty(timestamp) || string.IsNullOrEmpty(jsapi_ticket) || string.IsNullOrEmpty(url)) { return(""); } //构造需要用SHA1算法加密的数据 WxData signData = new WxData(); signData.SetValue("url", url); signData.SetValue("timestamp", timestamp); signData.SetValue("noncestr", noncestr); signData.SetValue("jsapi_ticket", jsapi_ticket); string param = signData.ToUrl(); Log.Debug("SHA1加密前参数", param); //SHA1加密 string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1"); Log.Debug("SHA1加密后参数", addrSign); return(addrSign); }
/// <summary> /// 微信访问入口页,获取用户UnionID和OpenID /// </summary> /// <param name="intVisit">访问:1首页;2定制;3我的;4管家主页;5商家主页</param> /// <param name="strTargetID">目标编码:4管家编码;5商家编码</param> /// <returns></returns> public ActionResult Index(int intVisit = 0, string strTargetID = "") { ViewBag.UnionID = ""; ViewBag.OpenID = ""; ViewBag.Visit = intVisit; ViewBag.TargetID = strTargetID; ViewBag.ErrorMessage = ""; string strWxCode = "", strVisitResult = "", strAccessToken = ""; Dictionary <string, object> dicResult = null; try { if (string.IsNullOrEmpty(Request.QueryString["code"])) { #region 获取Code string strMyUrl = "http://" + Request.Url.Host + Request.Path + "?intVisit=" + intVisit + "&strTargetID=" + strTargetID; WxData wxdCode = new WxData(); wxdCode.SetValue("appid", WxConfig.AppID); wxdCode.SetValue("redirect_uri", HttpUtility.UrlEncode(strMyUrl)); wxdCode.SetValue("response_type", "code"); //wxdAuthorize.SetValue("scope", "snsapi_base"); wxdCode.SetValue("scope", "snsapi_userinfo"); wxdCode.SetValue("state", "STATE" + "#wechat_redirect"); Response.Redirect(WxConfig.AuthorizeUrl + wxdCode.ToUrl()); #endregion 获取Code } else { strWxCode = Request.QueryString["code"].Trim(); JavaScriptSerializer jss = new JavaScriptSerializer(); #region 获取OpenID WxData wxdOpen = new WxData(); wxdOpen.SetValue("appid", WxConfig.AppID); wxdOpen.SetValue("secret", WxConfig.AppSecret); wxdOpen.SetValue("code", strWxCode); wxdOpen.SetValue("grant_type", "authorization_code"); strVisitResult = WxHttp.Get(WxConfig.AccessTokenUrl + wxdOpen.ToUrl()); dicResult = (Dictionary <string, object>)jss.Deserialize(strVisitResult, typeof(Dictionary <string, object>)); if (dicResult.ContainsKey("errcode")) { throw new Exception("获取网页用户授权出错:" + dicResult["errmsg"].ToString()); } strAccessToken = dicResult["access_token"].ToString(); ViewBag.OpenID = dicResult["openid"].ToString(); #endregion 获取OpenID #region 获取UnionID strVisitResult = WxHttp.Get(WxConfig.UserUrl + "access_token=" + strAccessToken + "&openid=" + ViewBag.OpenID + "&lang=zh_CN"); dicResult = (Dictionary <string, object>)jss.Deserialize(strVisitResult, typeof(Dictionary <string, object>)); if (dicResult.ContainsKey("errcode")) { throw new Exception("获取用户信息出错:" + dicResult["errmsg"].ToString()); } ViewBag.UnionID = dicResult["unionid"].ToString(); #endregion 获取UnionID } } catch (Exception ex) { WriteLog("Weixin", "Index", "Exception:" + ex.Message.ToString()); ViewBag.ErrorMessage = ex.Message.ToString(); } return(View()); }
public ActionResult WxJsapiAuthorize() { string strUrl = ""; string strGet = ""; string strAccessToken = ""; //令牌 string strJsapiTicket = ""; //票据 DateTime dtmTicketExpire = DefaultTime; //票据到期时间 string strResult = ""; JsonData jsdResult; JsonResult jsrReturn = new JsonResult(); string strNonce = ""; string strTimestamp = ""; string strSignature = ""; try { if (string.IsNullOrEmpty(Request.Form["strUrl"])) { throw new Exception("strUrl为空!"); } strUrl = Request.Form["strUrl"].Trim(); WriteLog("Weixin", "WxJsapiAuthorize", "strUrl:" + strUrl); Dictionary <string, object> dicTicket = new Dictionary <string, object>(); dicTicket = GetJsapiTicket(strUrl); if (dicTicket.Count <= 0) { #region 获得微信JS接口的临时票据 strGet = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WxConfig.AppID + "&secret=" + WxConfig.AppSecret; strResult = WxHttp.Get(strGet); jsdResult = JsonMapper.ToObject(strResult); strAccessToken = (string)jsdResult["access_token"]; WriteLog("Weixin", "WxJsapiAuthorize", "strAccessToken:" + strAccessToken); strGet = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + strAccessToken + "&type=jsapi"; dtmTicketExpire = DateTime.Now; strResult = WxHttp.Get(strGet); jsdResult = JsonMapper.ToObject(strResult); strJsapiTicket = (string)jsdResult["ticket"]; WriteLog("Weixin", "WxJsapiAuthorize", "strJsapiTicket:" + strJsapiTicket); dtmTicketExpire = dtmTicketExpire.AddMinutes(110); dicTicket = new Dictionary <string, object>(); dicTicket.Add("Ticket", strJsapiTicket); dicTicket.Add("Expire", dtmTicketExpire); SetJsapiTicket(strUrl, dicTicket); #endregion 获得微信JS接口的临时票据 } else { if (dicTicket.ContainsKey("Ticket")) { strJsapiTicket = (string)dicTicket["Ticket"]; } if (dicTicket.ContainsKey("Expire")) { dtmTicketExpire = (DateTime)dicTicket["Expire"]; } if (dtmTicketExpire <= DateTime.Now || strJsapiTicket == "") { #region 获得微信JS接口的临时票据 strGet = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WxConfig.AppID + "&secret=" + WxConfig.AppSecret; strResult = WxHttp.Get(strGet); jsdResult = JsonMapper.ToObject(strResult); strAccessToken = (string)jsdResult["access_token"]; WriteLog("Weixin", "WxJsapiAuthorize", "strAccessToken:" + strAccessToken); strGet = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + strAccessToken + "&type=jsapi"; dtmTicketExpire = DateTime.Now; strResult = WxHttp.Get(strGet); jsdResult = JsonMapper.ToObject(strResult); strJsapiTicket = (string)jsdResult["ticket"]; WriteLog("Weixin", "WxJsapiAuthorize", "strJsapiTicket:" + strJsapiTicket); dtmTicketExpire = dtmTicketExpire.AddMinutes(110); dicTicket = new Dictionary <string, object>(); dicTicket.Add("Ticket", strJsapiTicket); dicTicket.Add("Expire", dtmTicketExpire); SetJsapiTicket(strUrl, dicTicket); #endregion 获得微信JS接口的临时票据 } } WxData wxdTicket = new WxData(); strNonce = WxApi.GenerateNonceStr(); wxdTicket.SetValue("noncestr", strNonce); wxdTicket.SetValue("jsapi_ticket", strJsapiTicket); strTimestamp = WxApi.GenerateTimeStamp(); wxdTicket.SetValue("timestamp", strTimestamp); wxdTicket.SetValue("url", strUrl); string strParam = wxdTicket.ToUrl(); WriteLog("Weixin", "WxJsapiAuthorize", "strParam:" + strParam); strSignature = GetSwcSH1(strParam);// FormsAuthentication.HashPasswordForStoringInConfigFile(strParam, "SHA1"); WriteLog("Weixin", "WxJsapiAuthorize", "strSignature:" + strSignature); jsrReturn.Data = new { Result = 1, AppID = WxConfig.AppID, Timestamp = strTimestamp, Noncestr = strNonce, Signature = strSignature }; } catch (Exception ex) { WriteLog("Weixin", "WxJsapiAuthorize", "Exception:" + ex.Message.ToString()); jsrReturn.Data = new { Result = -99, Message = ex.Message.ToString() }; } return(jsrReturn); }
/// <summary> /// 通过OpenID和AccessToken拉取用户信息的返回数据,正确时返回的JSON数据包如下: /// { /// "openid":" OPENID", //用户的唯一标识 /// "nickname": NICKNAME, //用户昵称 /// "sex":"1",//用户的性别,值为1时是男性,值为2时是女性,值为0时是未知 /// "province":"PROVINCE"//用户个人资料填写的省份 /// "city":"CITY",//普通用户个人资料填写的城市 /// "country":"COUNTRY",//国家,如中国为CN /// "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", //用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。 /// "privilege":["PRIVILEGE1","PRIVILEGE2"],//用户特权信息,json 数组,如微信沃卡用户为(chinaunicom) /// "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"//只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。 /// } /// 更详细的说明请参考:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html /// </summary> /// <param name="OpenID"></param> /// <param name="AccessToken"></param> public void GetUserInfoFromOpenidAndAccessToken(string OpenID, string AccessToken) { try { //构造拉取用户信息的url WxData data = new WxData(); data.SetValue("access_token", AccessToken); data.SetValue("openid", OpenID); data.SetValue("lang", "zh_CN"); string url = "https://api.weixin.qq.com/sns/userinfo?" + data.ToUrl(); //请求url以获取数据 string result = HttpService.Get(url); Log.Debug(this.GetType().ToString(), "通过OpenID和AccessToken拉取用户信息(GetUserInfoFromOpenidAndAccessToken) response数据 : " + result); JsonData jd = JsonMapper.ToObject(result); if (!string.IsNullOrEmpty((string)jd["openid"])) { UserInfoResult = new WxData(); UserInfoResult.SetValue("openid", (string)jd["openid"]); UserInfoResult.SetValue("nickname", (string)jd["nickname"]); UserInfoResult.SetValue("sex", (int)jd["sex"]); UserInfoResult.SetValue("language", (string)jd["language"]); UserInfoResult.SetValue("city", (string)jd["city"]); UserInfoResult.SetValue("province", (string)jd["province"]); UserInfoResult.SetValue("country", (string)jd["country"]); UserInfoResult.SetValue("headimgurl", (string)jd["headimgurl"]); if (jd["privilege"].IsArray) { string strArray = ""; JsonData jdItems = jd["privilege"]; for (int i = 0; i < jdItems.Count; i++) { strArray = strArray + (string)jdItems[i] + ","; } strArray = strArray.TrimEnd(','); UserInfoResult.SetValue("privilege", strArray); } if (((IDictionary)jd).Contains("unionid")) { UserInfoResult.SetValue("unionid", (string)jd["unionid"]); } Log.Debug(this.GetType().ToString(), "用户信息 openid: " + UserInfoResult.GetValue("openid")); Log.Debug(this.GetType().ToString(), "用户信息 nickname: " + UserInfoResult.GetValue("nickname")); Log.Debug(this.GetType().ToString(), "用户信息 sex: " + UserInfoResult.GetValue("sex")); Log.Debug(this.GetType().ToString(), "用户信息 language: " + UserInfoResult.GetValue("language")); Log.Debug(this.GetType().ToString(), "用户信息 city: " + UserInfoResult.GetValue("city")); Log.Debug(this.GetType().ToString(), "用户信息 province: " + UserInfoResult.GetValue("province")); Log.Debug(this.GetType().ToString(), "用户信息 country: " + UserInfoResult.GetValue("country")); Log.Debug(this.GetType().ToString(), "用户信息 headimgurl: " + UserInfoResult.GetValue("headimgurl")); Log.Debug(this.GetType().ToString(), "用户信息 privilege: " + UserInfoResult.GetValue("privilege")); if (UserInfoResult.IsSet("unionid")) { Log.Debug(this.GetType().ToString(), "用户信息 unionid: " + UserInfoResult.GetValue("unionid")); } } else { Log.Error(this.GetType().ToString(), "通过OpenID和AccessToken拉取用户信息失败 errcode : " + (string)jd["errcode"]); Log.Error(this.GetType().ToString(), "通过OpenID和AccessToken拉取用户信息失败 errmsg : " + (string)jd["errmsg"]); } } catch (Exception ex) { Log.Error(this.GetType().ToString(), ex.ToString()); throw new WxException(ex.ToString()); } }
/// <summary> /// 网页授权获取用户基本信息的全部过程。 /// 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html。 /// 第一步:利用url跳转获取code。 /// 第二步:利用code去获取openid和access_token。 /// 第三步:拉取用户信息(scopeType=1时) /// </summary> /// <param name="scopeType">0:snsapi_base;1:snsapi_userinfo;</param> public void GetOpenidAndAccessToken(int scopeType) { if (!string.IsNullOrEmpty(page.Request.QueryString["code"])) { //获取code码,以获取openid和access_token string code = page.Request.QueryString["code"]; Log.Debug(this.GetType().ToString(), "网页授权获取用户基本信息Get code : " + code); GetOpenidAndAccessTokenFromCode(code); if (scopeType == 1) { GetUserInfoFromOpenidAndAccessToken(openid, access_token); } } else { //构造网页授权获取code的URL //取提交页面的完整URL,包括请求参数 string redirect_uri = HttpUtility.UrlEncode(page.Request.Url.ToString()); WxData data = new WxData(); data.SetValue("appid", WxConfig.APPID); data.SetValue("redirect_uri", redirect_uri); data.SetValue("response_type", "code"); data.SetValue("scope", (scopeType == 0) ? "snsapi_base" : "snsapi_userinfo"); data.SetValue("state", "STATE" + "#wechat_redirect"); string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl(); Log.Debug(this.GetType().ToString(), "网页授权获取用户基本信息跳转到URL : " + url); try { //触发微信返回code码 //Redirect函数会抛出ThreadAbortException异常,不用处理这个异常 page.Response.Redirect(url); } catch (System.Threading.ThreadAbortException ex) { } } }