示例#1
0
        /// <summary>
        /// singature签名的生成比较麻烦。
        /// 首先生成获取access_token
        /// (有效期7200秒,开发者必须在自己的服务全局缓存access_token)
        /// </summary>
        /// <returns></returns>
        public static string Getaccesstoken()
        {
            string         appid     = PayConfig.WxAppid();
            string         secret    = PayConfig.WxAppSecret();
            string         urljson   = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
            string         strjson   = "";
            UTF8Encoding   encoding  = new UTF8Encoding();
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(urljson);

            myRequest.Method      = "GET";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            HttpWebResponse response;
            Stream          responseStream;
            StreamReader    reader;
            string          srcString;

            response       = myRequest.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader         = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            srcString      = reader.ReadToEnd();
            reader.Close();
            if (srcString.Contains("access_token"))
            {
                //CommonJsonModel model = new CommonJsonModel(srcString);
                CommonJsonModel model = new CommonJsonModel(srcString);
                strjson = model.GetValue("access_token");
                SessionTools.SetSession("access_tokenzj", strjson);
            }
            return(strjson);
        }
示例#2
0
        private static Sys_User Registered(string code)
        {
            try
            {
                var client = new System.Net.WebClient();
                client.Encoding = System.Text.Encoding.UTF8;


                // 通过code换取网页授权access_token
                var strTokenUrl = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code",
                                                PayConfig.WxAppid(), PayConfig.WxAppSecret(), code);
                var strTokenData = client.DownloadString(strTokenUrl);
                var TokenEntity  = JsonHelper.DeserializeObject <WxAccessToken>(strTokenData);
                if (TokenEntity == null || string.IsNullOrEmpty(TokenEntity.access_token))
                {
                    return(null);
                }


                // 通过access_token和openid 拉取用户信息
                var strUserUrl  = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", TokenEntity.access_token, TokenEntity.openid);
                var strUserData = client.DownloadString(strUserUrl);
                var UserEntity  = JsonHelper.DeserializeObject <WxUserInfo>(strUserData);
                if (UserEntity == null || string.IsNullOrEmpty(UserEntity.openid))
                {
                    return(null);
                }


                // 注册
                var User = new UserService().Regist(UserEntity);
                if (User != null)
                {
                    SessionTools.UserID   = User.UserID;
                    SessionTools.UserName = User.UserName;
                    return(User);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(null);
        }