ToUrl() public method

public ToUrl ( ) : string
return string
Beispiel #1
0
        /**
         *
         * 通过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
         * @失败时抛异常WxPayException
         */
        public void GetOpenidAndAccessTokenFromCode(string code)
        {
            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", appid);
                data.SetValue("secret", 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);


                //保存access_token,用于收货地址获取
                JsonData jd = JsonMapper.ToObject(result);
                access_token = (string)jd["access_token"];

                //获取用户openid
                openid = (string)jd["openid"];
            }
            catch (Exception ex)
            {
                //
            }
        }
Beispiel #2
0
        /// <summary>
        /// 公众号AccessToken
        /// </summary>
        /// <param name="code"></param>
        public string inGetAccessToken(string appid, string secret)
        {
            string result = string.Empty;

            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", appid);
                data.SetValue("secret", secret);
                data.SetValue("grant_type", "client_credential");
                string url = "https://api.weixin.qq.com/cgi-bin/token?" + data.ToUrl();

                //请求url以获取数据
                result = HttpService.Get(url);

                Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result);

                //保存access_token,用于收货地址获取
                JsonData jd = JsonMapper.ToObject(result);
                try
                {
                    this.access_token = (string)jd["access_token"];
                }
                catch
                {
                }
            }
            catch (Exception ex)
            {
                Log.Error(this.GetType().ToString(), ex.ToString());
                throw new WxPayException(ex.ToString());
            }
            return(result);
        }
Beispiel #3
0
 /**
  *
  * 通过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
  * @失败时抛异常WxPayException
  */
 public void GetOpenidAndAccessTokenFromCode(string code, string companyID)
 {
     try
     {
         string    sql = "select * from asm_company where id=" + companyID;
         DataTable dt  = DbHelperSQL.Query(sql).Tables[0];
         //构造获取openid及access_token的url
         WxPayData data = new WxPayData();
         data.SetValue("appid", dt.Rows[0]["appId"].ToString());
         data.SetValue("secret", dt.Rows[0]["wx_appsecret"].ToString());
         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(), "GetOpenidAndAccessTokenFromCode response : " + result);
         //保存access_token,用于收货地址获取
         JsonData jd = JsonMapper.ToObject(result);
         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);
     }
     catch (Exception ex)
     {
         Log.Error(this.GetType().ToString(), ex.ToString());
         throw new WxPayException(ex.ToString());
     }
 }
Beispiel #4
0
 /// <summary>
 /// 获取token和openid
 /// </summary>
 /// <param name="code"></param>
 public void GetOpenidAndAccessTokenFromCode(string code)
 {
     try
     {
         //构造获取openid及access_token的url
         WxPayAPI.WxPayData data = new WxPayAPI.WxPayData();
         data.SetValue("appid", appid);
         data.SetValue("secret", secret);
         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 = WxPayAPI.HttpService.Get(url);
         //Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result);
         //保存access_token,用于收货地址获取
         LitJson.JsonData jd = JsonMapper.ToObject(result);
         token = (string)jd["access_token"];
         //获取用户openid
         openid = (string)jd["openid"];
     }
     catch (Exception ex)
     {
         WxPayAPI.Log.Error(this.GetType().ToString(), ex.ToString());
         //throw new WxPayAPI.WxPayException(ex.ToString());
     }
 }
Beispiel #5
0
        /**
         *
         * 通过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
         * @失败时抛异常WxPayException
         */
        public void GetOpenidAndAccessTokenFromCode(string code)
        {
            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", WxPayConfig._APPID);
                data.SetValue("secret", WxPayConfig._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.Info("GetOpenidAndAccessTokenFromCode=======> response : " + result);

                //保存access_token,用于收货地址获取
                JsonData jd = JsonMapper.ToObject(result);
                access_token = (string)jd["access_token"];

                //获取用户openid
                openid = (string)jd["openid"];

                log.Info("GetOpenidAndAccessTokenFromCode------>Get openid : " + openid);
                log.Info("GetOpenidAndAccessTokenFromCode------>Get access_token : " + access_token);
            }
            catch (Exception ex)
            {
                log.Info(ex.ToString());
                throw new WxPayException(ex.ToString());
            }
        }
Beispiel #6
0
        /**
         *
         * 通过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
         * @失败时抛异常WxPayException
         */
        public void GetOpenidAndAccessTokenFromCode(string code, HttpContextBase context)
        {
            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", WxPayConfig.APPID);
                data.SetValue("secret", WxPayConfig.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);

                //LogHelper.Error("GetOpenidAndAccessTokenFromCode response : " + result);

                //保存access_token,用于收货地址获取
                JsonData jd = JsonMapper.ToObject(result);
                access_token = (string)jd["access_token"];

                //获取用户openid
                openid = (string)jd["openid"];
                context.Session["openid"] = openid;
                //LogHelper.Error("context.Session openid " + context.Session["openid"]);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.ToString());
                throw new WxPayException(ex.ToString());
            }
        }
Beispiel #7
0
        /**
         *
         * 通过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
         * @失败时抛异常WxPayException
         */
        public void GetOpenidAndAccessTokenFromCode(string code)
        {
            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", WxPayConfig.APPID);
                data.SetValue("secret", WxPayConfig.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(), "GetOpenidAndAccessTokenFromCode response : " + result);

                //保存access_token,用于收货地址获取
                JObject jd = result.DeserializeJsonToObject <JObject>();
                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);
            }
            catch (Exception ex)
            {
                Log.Error(this.GetType().ToString(), ex.ToString());
                throw new WxPayException(ex.ToString());
            }
        }
Beispiel #8
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //writeFile("coming");
            context.Response.Write("coming");
            //context.Response.Write("Hello World");
            string code = HttpContext.Current.Request.QueryString["code"];

            writeFile(code);
            WxPayData data = new WxPayData();

            data.SetValue("appid", WxPayConfig.APPID);
            data.SetValue("secret", WxPayConfig.APPSECRET);
            data.SetValue("code", code);
            data.SetValue("grant_type", "authorization_code");
            string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl();

            writeFile(url);
            //请求url以获取数据
            string result = HttpService.Get(url);

            writeFile(result);
            Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result);

            //保存access_token,用于收货地址获取
            JsonData jd = JsonMapper.ToObject(result);

            //access_token = (string)jd["access_token"];

            //获取用户openid
            openid = (string)jd["openid"];
            writeFile(openid);
            context.Response.Write(openid);//获取H5调起JS API参数
        }
Beispiel #9
0
        /// <summary>
        /// 获取收货地址js函数入口参数
        /// </summary>
        /// <remarks>
        /// 详情请参考收货地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9
        /// </remarks>
        /// <returns>共享收货地址js函数需要的参数,json格式可以直接做参数使用</returns>
        public string GetEditAddressParameters(string accesstoken = "ACCESS_TOKEN")
        {
            string parameter = "";

            try
            {
                string host        = _httpContext.HttpContext.Request.Host.Host;
                string path        = _httpContext.HttpContext.Request.Path;
                string queryString = _httpContext.HttpContext.Request.QueryString.Value;

                //这个地方要注意,参与签名的是网页授权获取用户信息时微信后台回传的完整url
                string url = "http://" + host + path + queryString;

                //构造需要用SHA1算法加密的数据
                WxPayData signData = new WxPayData();
                signData.SetValue("appid", WxPayConfig.GetConfig().GetAppID());
                signData.SetValue("url", url);
                signData.SetValue("timestamp", WxPayApi.GenerateTimeStamp());
                signData.SetValue("noncestr", WxPayApi.GenerateNonceStr());
                signData.SetValue("accesstoken", accesstoken);
                string param = signData.ToUrl();

                Log.Debug(this.GetType().ToString(), "SHA1 encrypt param : " + param);

                //以字节方式存储  
                byte[] data = System.Text.Encoding.UTF8.GetBytes(param);
                var    sha1 = System.Security.Cryptography.SHA1.Create();

                //得到哈希值
                var hash = sha1.ComputeHash(data);

                //SHA1加密
                string addrSign = BitConverter.ToString(hash).Replace("-", "");
                Log.Debug(this.GetType().ToString(), "SHA1 encrypt result : " + addrSign);

                //获取收货地址js函数入口参数
                WxPayData afterData = new WxPayData();
                afterData.SetValue("appId", WxPayConfig.GetConfig().GetAppID());
                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 WxPayException(ex.ToString());
            }

            return(parameter);
        }
Beispiel #10
0
        /**
         *
         * 获取收货地址js函数入口参数,详情请参考收货地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9
         * @return string 共享收货地址js函数需要的参数,json格式可以直接做参数使用
         */

        public string GetEditAddressParameters()
        {
            string parameter = "";

            try
            {
                string host        = page.Request.Url.Host;
                string path        = page.Request.Path;
                string queryString = page.Request.Url.Query;

                //这个地方要注意,参与签名的是网页授权获取用户信息时微信后台回传的完整url
                string url = "http://" + host + path + queryString;

                //构造需要用SHA1算法加密的数据
                WxPayData signData = new WxPayData();
                signData.SetValue("appid", WxPayConfig.GetConfig().GetAppID());
                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函数入口参数
                WxPayData afterData = new WxPayData();
                afterData.SetValue("appId", WxPayConfig.GetConfig().GetAppID());
                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 WxPayException(ex.ToString());
            }

            return(parameter);
        }
Beispiel #11
0
        public static string GetAccessToken()
        {
            WxPayData data = new WxPayData();

            data.SetValue("grant_type", "client_credential");
            data.SetValue("appid", WxPayConfig.APPID);
            data.SetValue("secret", WxPayConfig.APPSECRET);
            string url = "https://api.weixin.qq.com/cgi-bin/token?" + data.ToUrl();

            string result = HttpService.Get(url);
            //请求url以获取数据
            JsonData jd = JsonMapper.ToObject(result);

            return((string)jd["access_token"]);
        }
Beispiel #12
0
        /// <summary>
        /// 通过code换取网页授权access_token和openid的返回数据
        /// </summary>
        /// <remarks>
        /// access_token可用于获取共享收货地址
        /// openid是微信支付jsapi支付接口统一下单时必须的参数
        /// 正确时返回的JSON数据包如下:
        /// {
        ///  "access_token":"ACCESS_TOKEN",
        ///  "expires_in":7200,
        ///  "refresh_token":"REFRESH_TOKEN",
        ///  "openid":"OPENID",
        ///  "scope":"SCOPE",
        ///  "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
        /// }
        /// 更详细的说明请参考网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
        /// </remarks>
        /// <param name="code"></param>
        /// <returns></returns>
        public JsonData GetOpenidAndAccessTokenFromCode(string code)
        {
            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", WxPayConfig.GetConfig().GetAppID());
                data.SetValue("secret", WxPayConfig.GetConfig().GetAppSecret());
                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(), "GetOpenidAndAccessTokenFromCode response : " + result);

                //保存access_token,用于收货地址获取
                JsonData jd = JsonMapper.ToObject(result);

                if (result.Contains("errcode"))
                {
                    string errmsg = jd["errmsg"].ToString();
                    Log.Error(this.GetType().ToString(), errmsg);
                }
                else
                {
                    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);
                }

                return(jd);
            }
            catch (Exception ex)
            {
                Log.Error(this.GetType().ToString(), ex.ToString());
                throw new WxPayException(ex.ToString());
            }
        }
Beispiel #13
0
        /// <summary>
        /// 构造网页授权获取code的URL
        /// </remarks>
        public string GetRedirectURL()
        {
            //构造网页授权获取code的URL
            string    host         = _httpContext.HttpContext.Request.Host.Host;
            string    path         = _httpContext.HttpContext.Request.Path;
            string    redirect_uri = HttpUtility.UrlEncode("http://" + host + path);
            WxPayData data         = new WxPayData();

            data.SetValue("appid", WxPayConfig.GetConfig().GetAppID());
            data.SetValue("redirect_uri", redirect_uri);
            data.SetValue("response_type", "code");
            data.SetValue("scope", "snsapi_base");
            data.SetValue("state", "STATE" + "#wechat_redirect");
            string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();

            Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url);

            return(url);
        }
Beispiel #14
0
        /**
         *
         * 通过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
         * @失败时抛异常WxPayException
         */
        public void GetOpenidAndAccessTokenFromCode(string code, string city)
        {
            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", WxPayConfig.APPID(city));
                data.SetValue("secret", WxPayConfig.APPSECRET(city));
                //data.SetValue("code", code);
                data.SetValue("js_code", code);
                //data.SetValue("scope", "snsapi_userinfo");
                data.SetValue("grant_type", "authorization_code");
                //string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl();
                string url = "https://api.weixin.qq.com/sns/jscode2session?" + data.ToUrl();

                //请求url以获取数据
                string result = HttpService.Get(url);

                Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result);

                //保存access_token,用于收货地址获取
                JsonData jd = JsonMapper.ToObject(result);
                try
                {
                    this.access_token = (string)jd["access_token"];
                }
                catch
                {
                }

                //获取用户openid
                this.openid = (string)jd["openid"];

                Log.Debug(this.GetType().ToString(), "Get openid : " + openid);
                Log.Debug(this.GetType().ToString(), "Get access_token : " + access_token);
            }
            catch (Exception ex)
            {
                Log.Error(this.GetType().ToString(), ex.ToString());
                throw new WxPayException(ex.ToString());
            }
        }
        /**
        * 
        * 网页授权获取用户基本信息的全部过程
        * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
        * 第一步:利用url跳转获取code
        * 第二步:利用code去获取openid和access_token
        * 
        */

        public  void weixin(string ThisUrl)
        {
            string redirect_uri = HttpUtility.UrlEncode(ThisUrl);
            WxPayData data = new WxPayData();
            data.SetValue("appid", WxPayConfig.APPID);
            data.SetValue("redirect_uri", redirect_uri);
            data.SetValue("response_type", "code");
            data.SetValue("scope", "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(), "Will Redirect to URL : " + url);
            try
            {
                System.Web.HttpContext.Current.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
            }
            catch (System.Threading.ThreadAbortException ex)
            {

            }
        }
Beispiel #16
0
        /**
         *
         * 网页授权获取用户基本信息的全部过程
         * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
         * 第一步:利用url跳转获取code
         * 第二步:利用code去获取openid和access_token
         *
         */

        public void weixin(string ThisUrl)
        {
            string    redirect_uri = HttpUtility.UrlEncode(ThisUrl);
            WxPayData data         = new WxPayData();

            data.SetValue("appid", WxPayConfig.APPID);
            data.SetValue("redirect_uri", redirect_uri);
            data.SetValue("response_type", "code");
            data.SetValue("scope", "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(), "Will Redirect to URL : " + url);
            try
            {
                System.Web.HttpContext.Current.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
            }
            catch (System.Threading.ThreadAbortException ex)
            {
            }
        }
Beispiel #17
0
        /**
         *
         * 网页授权获取用户基本信息的全部过程
         * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
         * 第一步:利用url跳转获取code
         * 第二步:利用code去获取openid和access_token
         *
         */
        public void GetCode()
        {
            //构造网页授权获取code的URL
            string    redirect_uri = HttpUtility.UrlEncode("http://api.wftx666.com/getcode");
            WxPayData data         = new WxPayData();

            data.SetValue("appid", WxPayConfig.APPID);
            data.SetValue("redirect_uri", redirect_uri);
            data.SetValue("response_type", "code");
            data.SetValue("scope", "snsapi_base");
            data.SetValue("state", "STATE" + "#wechat_redirect");
            string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();

            Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url);
            try
            {
                //触发微信返回code码到回调页面,请求url即可
                //Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
            }
            catch (System.Threading.ThreadAbortException ex)
            {
            }
        }
Beispiel #18
0
 /**
  *
  * 网页授权获取用户基本信息的全部过程
  * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
  * 第一步:利用url跳转获取code
  * 第二步:利用code去获取openid和access_token
  *
  */
 public void GetOpenidAndAccessToken(string oid)
 {
     if (!string.IsNullOrEmpty(page.Request.QueryString["code"]))
     {
         log.Info("GetOpenidAndAccessToken---->Code : " + page.Request.QueryString["code"]);
         //获取code码,以获取openid和access_token
         string code = page.Request.QueryString["code"];
         log.Info("M:GetOpenidAndAccessToken---parma:Get code : " + code);
         GetOpenidAndAccessTokenFromCode(code);
     }
     else
     {
         //构造网页授权获取code的URL
         string    host         = page.Request.Url.Host;
         string    path         = page.Request.Path;
         string    redirect_uri = HttpUtility.UrlEncode("http://" + host + path);
         WxPayData data         = new WxPayData();
         data.SetValue("appid", WxPayConfig._APPID);
         data.SetValue("redirect_uri", redirect_uri);
         data.SetValue("response_type", "code");
         data.SetValue("scope", "snsapi_base");
         data.SetValue("state", oid + "#wechat_redirect");
         string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();
         log.Info("GetOpenidAndAccessToken----Will Redirect to URL : " + url);
         try
         {
             //触发微信返回code码
             page.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
         }
         catch (System.Threading.ThreadAbortException ex)
         {
         }
     }
 }
        public void GetOpenidAndAccessToken(string UrlCode,string ThisUrl)
        {
            if (!string.IsNullOrEmpty(UrlCode))
            {
                //Write("C:\\debug.txt", "111111");
                //获取code码,以获取openid和access_token
                string code = UrlCode;
                //Console.Write(code);
                //page.Response.Write("<script>alert("+code+")</script>"); 
                Log.Debug(this.GetType().ToString(), "Get code : " + code);
                GetOpenidAndAccessTokenFromCode(code);
            }
            else
            {
                //Write("C:\\debug.txt", "222222");
                //构造网页授权获取code的URL
                //string host = UrlHost;
                //string path = UrlPath;
                string redirect_uri = HttpUtility.UrlEncode(ThisUrl);
                WxPayData data = new WxPayData();
                data.SetValue("appid", WxPayConfig.APPID);
                data.SetValue("redirect_uri", redirect_uri);
                data.SetValue("response_type", "code");
                data.SetValue("scope", "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(), "Will Redirect to URL : " + url);
                try
                {
                    url1 = url;
                    //Write("C:\\debug.txt", url);
                    //触发微信返回code码      
                    //string url1 = "http://www.baidu.com";
                    System.Web.HttpContext.Current.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
                }
                catch(System.Threading.ThreadAbortException ex)
                {

                }
            }
        }
Beispiel #20
0
        /**
        *
        * 获取收货地址js函数入口参数,详情请参考收货地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9
        * @return string 共享收货地址js函数需要的参数,json格式可以直接做参数使用
        */
        public string GetEditAddressParameters()
        {
            string parameter = "";
            try
            {
                string host = page.Request.Url.Host;
                string path = page.Request.Path;
                string queryString = page.Request.Url.Query;
                //这个地方要注意,参与签名的是网页授权获取用户信息时微信后台回传的完整url
                string url = "http://" + host + path + queryString;

                //构造需要用SHA1算法加密的数据
                WxPayData signData = new WxPayData();
                signData.SetValue("appid", WxPayConfig.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函数入口参数
                WxPayData afterData = new WxPayData();
                afterData.SetValue("appId", WxPayConfig.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 WxPayException(ex.ToString());
            }

            return parameter;
        }
Beispiel #21
0
        /**
         *
         * 网页授权获取用户基本信息的全部过程
         * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
         * 第一步:利用url跳转获取code
         * 第二步:利用code去获取openid和access_token
         *
         */
        public void GetOpenidAndAccessToken(string companyID)
        {
            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, companyID);
            }
            else
            {
                string    sql = "select * from asm_company where id=" + companyID;
                DataTable dt  = DbHelperSQL.Query(sql).Tables[0];
                //构造网页授权获取code的URL
                string host         = page.Request.Url.Host;
                string path         = page.Request.Path;
                string redirect_uri = HttpUtility.UrlEncode("http://" + host + path);

                WxPayData data = new WxPayData();
                data.SetValue("appid", dt.Rows[0]["appId"].ToString());
                data.SetValue("redirect_uri", redirect_uri);
                data.SetValue("response_type", "code");
                data.SetValue("scope", "snsapi_base");
                data.SetValue("state", "STATE" + "#wechat_redirect");

                string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();

                Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url);
                try
                {
                    //触发微信返回code码
                    page.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
                }
                catch (System.Threading.ThreadAbortException ex)
                {
                }
            }
        }
Beispiel #22
0
        /**
        *
        * 网页授权获取用户基本信息的全部过程
        * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
        * 第一步:利用url跳转获取code
        * 第二步:利用code去获取openid和access_token
        *
        */
        public void GetOpenidAndAccessToken()
        {
            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);
            }
            else
            {

                try
                {
                    //构造网页授权获取code的URL
                    //string host = page.Request.Url.Host;
                    //string path = page.Request.Path;
                    string redirect_uri = HttpUtility.UrlEncode("http://");
                    WxPayData data = new WxPayData();
                    data.SetValue("appid", WxPayConfig.APPID);
                    data.SetValue("redirect_uri", redirect_uri);
                    data.SetValue("response_type", "code");
                    data.SetValue("scope", "snsapi_base");
                    data.SetValue("state", "STATE" + "#wechat_redirect");
                    string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();
                    Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url);

                try
                {
                    //触发微信返回code码
                    page.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
                }
                catch (System.Threading.ThreadAbortException ex)
                {
                }

                }
                catch (Exception ex)
                {
                    page.Response.Write("<span style='color:#FF0000;font-size:20px'>" + ex.ToString() + "</span>");
                }

            }
        }
Beispiel #23
0
        /**
        *
        * 通过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
        * @失败时抛异常WxPayException
        */
        public void GetOpenidAndAccessTokenFromCode(string code)
        {
            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", WxPayConfig.APPID);
                data.SetValue("secret", WxPayConfig.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(), "GetOpenidAndAccessTokenFromCode response : " + result);

                //保存access_token,用于收货地址获取
                JsonData jd = JsonMapper.ToObject(result);
                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);
            }
            catch (Exception ex)
            {
                Log.Error(this.GetType().ToString(), ex.ToString());
                throw new WxPayException(ex.ToString());
            }
        }
Beispiel #24
0
 public void GetOpenidAndAccessToken(string UrlCode, string ThisUrl)
 {
     if (!string.IsNullOrEmpty(UrlCode))
     {
         //Write("C:\\debug.txt", "111111");
         //获取code码,以获取openid和access_token
         string code = UrlCode;
         //Console.Write(code);
         //page.Response.Write("<script>alert("+code+")</script>");
         Log.Debug(this.GetType().ToString(), "Get code : " + code);
         GetOpenidAndAccessTokenFromCode(code);
     }
     else
     {
         //Write("C:\\debug.txt", "222222");
         //构造网页授权获取code的URL
         //string host = UrlHost;
         //string path = UrlPath;
         string    redirect_uri = HttpUtility.UrlEncode(ThisUrl);
         WxPayData data         = new WxPayData();
         data.SetValue("appid", WxPayConfig.APPID);
         data.SetValue("redirect_uri", redirect_uri);
         data.SetValue("response_type", "code");
         data.SetValue("scope", "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(), "Will Redirect to URL : " + url);
         try
         {
             url1 = url;
             //Write("C:\\debug.txt", url);
             //触发微信返回code码
             //string url1 = "http://www.baidu.com";
             System.Web.HttpContext.Current.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
         }
         catch (System.Threading.ThreadAbortException ex)
         {
         }
     }
 }
Beispiel #25
0
 /**
  *
  * 网页授权获取用户基本信息的全部过程
  * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
  * 第一步:利用url跳转获取code
  * 第二步:利用code去获取openid和access_token
  *
  */
 public void GetOpenidAndAccessToken()
 {
     if (HttpContext.Current.Session["code"] != null)
     {
         //获取code码,以获取openid和access_token
         string code = HttpContext.Current.Session["code"].ToString();
         Log.Debug(this.GetType().ToString(), "Get code : " + code);
         GetOpenidAndAccessTokenFromCode(code);
     }
     else
     {
         //构造网页授权获取code的URL
         //string host = page.Request.Url.Host;
         //string path = page.Request.Path;
         //string redirect_uri = HttpUtility.UrlEncode("http://" + host + path);
         string    redirect_uri = HttpUtility.UrlEncode("http://ph.lmx.ren");
         WxPayData data         = new WxPayData();
         data.SetValue("appid", WxPayConfig.APPID);
         data.SetValue("redirect_uri", redirect_uri);
         data.SetValue("response_type", "code");
         data.SetValue("scope", "snsapi_base");
         data.SetValue("state", "STATE" + "#wechat_redirect");
         string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();
         Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url);
         HttpContext.Current.Session["url"] = url;
         //try
         //{
         //    //触发微信返回code码
         //    page.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
         //}
         //catch (System.Threading.ThreadAbortException ex)
         //{
         //}
     }
 }
Beispiel #26
0
 /**
  *
  * 网页授权获取用户基本信息的全部过程
  * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
  * 第一步:利用url跳转获取code
  * 第二步:利用code去获取openid和access_token
  *
  */
 public void GetOpenidAndAccessToken(string id, HttpContextBase context)
 {
     if (!string.IsNullOrEmpty(context.Request.QueryString["code"]))
     {
         //获取code码,以获取openid和access_token
         string code = context.Request.QueryString["code"];
         //LogHelper.Error("Get code : " + code);
         GetOpenidAndAccessTokenFromCode(code, context);
     }
     else
     {
         //构造网页授权获取code的URL
         string host         = context.Request.Url.Host;
         string path         = context.Request.Path;
         string redirect_uri = HttpUtility.UrlEncode("http://" + host + "/Pay/WxPay/" + id);
         //LogHelper.Error("开始获取4 url=" + redirect_uri);
         WxPayData data = new WxPayData();
         data.SetValue("appid", WxPayConfig.APPID);
         data.SetValue("redirect_uri", redirect_uri);
         data.SetValue("response_type", "code");
         data.SetValue("scope", "snsapi_base");
         data.SetValue("state", "STATE" + "#wechat_redirect");
         string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();
         //LogHelper.Error("Will Redirect to URL : " + url);
         try
         {
             //触发微信返回code码
             context.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
             //LogHelper.Error("url: " + url);
         }
         catch (System.Threading.ThreadAbortException ex)
         {
         }
     }
 }
 /**
  *
  * 网页授权获取用户基本信息的全部过程
  * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
  * 第一步:利用url跳转获取code
  * 第二步:利用code去获取openid和access_token
  *
  */
 public void GetOpenidAndAccessToken(string code)
 {
     if (!string.IsNullOrEmpty(code))
     {
         //获取code码,以获取openid和access_token
         Log.Debug(this.GetType().ToString(), "Get code : " + code);
         GetOpenidAndAccessTokenFromCode(code);
     }
     else
     {
         //构造网页授权获取code的URL
         string    host         = context.Request.Url.Host;
         string    path         = context.Request.Path;
         string    redirect_uri = HttpUtility.UrlEncode("http://" + host + path);
         WxPayData data         = new WxPayData();
         data.SetValue("appid", WxPayConfig.APPID);
         data.SetValue("redirect_uri", redirect_uri);
         data.SetValue("response_type", "code");
         data.SetValue("scope", "snsapi_base");
         data.SetValue("state", "STATE" + "#wechat_redirect");
         string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();
         Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url);
         try
         {
             //触发微信返回code码
             context.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
         }
         catch (System.Threading.ThreadAbortException ex)
         {
         }
     }
 }
Beispiel #28
0
        /**
        *
        * 通过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
        * @失败时抛异常WxPayException
        */
        public void GetOpenidAndAccessTokenFromCode(string code)
        {
            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", appid);
                data.SetValue("secret", 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);

                //保存access_token,用于收货地址获取
                JsonData jd = JsonMapper.ToObject(result);
                access_token = (string)jd["access_token"];

                //获取用户openid
                openid = (string)jd["openid"];
            }
            catch (Exception ex)
            {
                //
            }
        }
Beispiel #29
0
 /**
  *
  * 网页授权获取用户基本信息的全部过程
  * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
  * 第一步:利用url跳转获取code
  * 第二步:利用code去获取openid和access_token
  *
  */
 public void GetOpenidAndAccessToken()
 {
     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);
     }
     else
     {
         //构造网页授权获取code的URL
         string path = page.Request.Path;
         //回调域
         string domain = Business.Do <ISystemPara>()["WeixinReturl"].Value ?? WeiSha.Common.Request.Domain.MainName;
         string prefix = HttpContext.Current.Request.Url.ToString();
         if (prefix.IndexOf("://") > -1)
         {
             prefix = prefix.Substring(0, prefix.IndexOf("://"));
         }
         prefix = prefix + "://";
         //appid
         string appi = Business.Do <ISystemPara>()["WeixinAPPID"].String;
         //
         string    redirect_uri = HttpUtility.UrlEncode(prefix + domain + path);
         WxPayData data         = new WxPayData();
         data.SetValue("appid", WxPayConfig.APPID);
         data.SetValue("redirect_uri", redirect_uri);
         data.SetValue("response_type", "code");
         data.SetValue("scope", "snsapi_base");
         data.SetValue("state", "STATE" + "#wechat_redirect");
         string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();
         Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url);
         try
         {
             //触发微信返回code码
             page.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
         }
         catch (System.Threading.ThreadAbortException ex)
         {
         }
     }
 }
Beispiel #30
0
 /**
 *
 * 网页授权获取用户基本信息的全部过程
 * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
 * 第一步:利用url跳转获取code
 * 第二步:利用code去获取openid和access_token
 *
 */
 public void GetOpenidAndAccessToken()
 {
     if (!string.IsNullOrEmpty(page.Request.QueryString["code"]))
     {
         //获取code码,以获取openid和access_token
         string code = page.Request.QueryString["code"];
         GetOpenidAndAccessTokenFromCode(code);
     }
     else
     {
         //构造网页授权获取code的URL
         string host = page.Request.Url.Host;
         string path = page.Request.Path;
         string redirect_uri = HttpUtility.UrlEncode("http://" + host + path);
         WxPayData data = new WxPayData();
         data.SetValue("appid", appid);
         data.SetValue("redirect_uri", redirect_uri);
         data.SetValue("response_type", "code");
         data.SetValue("scope", "snsapi_base");
         data.SetValue("state", "STATE" + "#wechat_redirect");
         string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();
         try
         {
             //触发微信返回code码
             page.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
         }
         catch(System.Threading.ThreadAbortException ex)
         {
         }
     }
 }
Beispiel #31
0
        public WXUserModel GetOpenidAndAccessTokenFromCodeLogin(string code)
        {
            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", WxPayConfig.APPID);
                data.SetValue("secret", WxPayConfig.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(), "GetOpenidAndAccessTokenFromCode response : " + result);


                //保存access_token,用于收货地址获取
                JsonData jd            = JsonMapper.ToObject(result);
                string   access_token  = (string)jd["access_token"];
                string   refresh_token = (string)jd["refresh_token"];
                //获取用户openid
                openid = (string)jd["openid"];

                Log.Debug(this.GetType().ToString(), "Get openid : " + openid);
                Log.Debug(this.GetType().ToString(), "Get access_token : " + access_token);

                WxPayData data1 = new WxPayData();
                data1.SetValue("appid", WxPayConfig.APPID);
                data1.SetValue("grant_type", "refresh_token");
                data1.SetValue("refresh_token", refresh_token);
                string tokenurl = "https://api.weixin.qq.com/sns/oauth2/refresh_token?" + data1.ToUrl();

                //请求url以获取数据
                string result1 = HttpService.Get(tokenurl);

                JsonData jd1           = JsonMapper.ToObject(result1);
                string   access_token1 = (string)jd1["access_token"];


                WxPayData data2 = new WxPayData();
                data2.SetValue("access_token", access_token1);
                data2.SetValue("openid", openid);
                data2.SetValue("lang", "zh_CN");
                string      Url2    = "https://api.weixin.qq.com/sns/userinfo?" + data2.ToUrl();
                string      result2 = HttpService.Get(Url2);
                JsonData    jd2     = JsonMapper.ToObject(result2);
                WXUserModel WXModel = new WXUserModel();
                WXModel.openid   = openid;
                WXModel.nickname = jd2["nickname"].ToString();
                WXModel.sex      = jd2["sex"].ToString();
                WXModel.province = jd2["province"].ToString();
                WXModel.city     = jd2["city"].ToString();
                return(WXModel);
            }
            catch (Exception ex)
            {
                Log.Error(this.GetType().ToString(), ex.ToString());
                throw new WxPayException(ex.ToString());
            }
        }
Beispiel #32
0
        /**
         *
         * 网页授权获取用户基本信息的全部过程
         * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
         * 第一步:利用url跳转获取code
         * 第二步:利用code去获取openid和access_token
         *
         */
        public void GetOpenidAndAccessToken(bool IsForOpenId = true)
        {
            if (!string.IsNullOrEmpty(Context.Request.QueryString["code"]))
            {
                //获取code码,以获取openid和access_token
                string code = Context.Request.QueryString["code"];

                GetOpenidAndAccessTokenFromCode(code);
            }
            else
            {
                //构造网页授权获取code的URL
                string host         = Context.Request.Url.Host;
                string path         = Context.Request.Path;
                string redirect_uri = HttpUtility.UrlEncode("http://" + host + path);


                WxPayData data = new WxPayData();
                data.SetValue("appid", WxPayConfig.APPID);
                data.SetValue("redirect_uri", redirect_uri);
                data.SetValue("response_type", "code");
                if (IsForOpenId)
                {
                    data.SetValue("scope", "snsapi_base");
                }
                else
                {
                    data.SetValue("scope", "snsapi_userinfo");
                }

                data.SetValue("state", "STATE" + "#wechat_redirect");
                string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();


                try
                {
                    //触发微信返回code码
                    Context.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
                }
                catch (System.Threading.ThreadAbortException ex)
                {
                }
            }
        }