示例#1
0
        public AccessToken GetAccessToken()
        {
            try
            {
                AccessToken accessToken = null;
                WeiXinToken token       = _tokenProvider.GetToken();
                if (token.AccessToken != null)
                {
                    accessToken = token.AccessToken;
                    return(accessToken);
                }

                string         url      = string.Format(WeiXinConst.ACCESS_TOKNE_URL, _config.AppId, _config.AppSecret);
                HttpWebRequest request  = (HttpWebRequest)WebRequest.Create(url);
                WebResponse    response = request.GetResponse();
                if (response.GetResponseStream() != null)
                {
                    StreamReader         sr       = new StreamReader(response.GetResponseStream());
                    string               json     = sr.ReadToEnd();
                    JavaScriptSerializer serlizer = new JavaScriptSerializer();
                    var obj = serlizer.Deserialize <dynamic>(json);
                    accessToken.Value    = obj.access_token;
                    accessToken.ExpireIn = obj.expires_in;
                    token.AccessToken    = accessToken;
                    _tokenProvider.SaveToken(token);
                }
                return(accessToken);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 通过code换取网页授权access_token和openid的返回数据
        /// </summary>
        /// <param name="code">code</param>
        public async Task <WeiXinToken> GetOpenidAndAccessTokenFromCodeAsync(string userid, string code)
        {
            WeiXinToken token       = null;
            string      redisKey    = $"{RedisKey}:userid:{userid}";
            string      tokenString = RedisHelper.Database.StringGet(redisKey);

            if (!string.IsNullOrWhiteSpace(tokenString))
            {
                var wexintoken = JsonConvert.DeserializeObject <WeiXinToken>(tokenString);
                //失效时间大于当前时间
                if (DateTime.Now >= token.expires_date)
                {
                    return(token);
                }
            }
            OAuthAccessTokenResult accessToken = OAuthApi.GetAccessToken(AppID, AppSecret, code);

            if (accessToken.errcode == ReturnCode.请求成功)
            {
                token = new WeiXinToken();
                token.access_token  = accessToken.access_token;
                token.create_date   = DateTime.Now;
                token.expires_in    = accessToken.expires_in;
                token.expires_date  = token.create_date.AddSeconds(accessToken.expires_in);
                token.openid        = accessToken.openid;
                token.refresh_token = accessToken.refresh_token;
                token.scope         = accessToken.scope;
                token.unionid       = accessToken.unionid;
                RedisHelper.Database.StringSet(redisKey, token.access_token, new TimeSpan(1, 50, 0));
            }
            return(token);
        }
示例#3
0
        /// <summary>
        /// 获取token(带缓存)
        /// </summary>
        /// <returns></returns>
        public WeiXinToken GetAccessTokenCache(string appid = "")
        {
            //var model = RedisHelper<WeiXinToken>.GetValus("WeiXinToken");
            //if (model != null)
            //{
            //    return model;
            //}
            var model = new WeiXinToken();

            model.WxToken        = GetAccessToken();
            model.ExpirationTime = DateTime.Now.AddMinutes(110);
            //微信token只有120分钟有效期,这里我们设置为110分钟,提前重新获取token
            //RedisHelper<string>.SetValus("WeiXinToken", JsonHelper.ObjectToJSON(model), model.ExpirationTime);
            return(model);
        }
示例#4
0
        public JsTicket GetJsTicket()
        {
            try
            {
                JsTicket    ticket = null;
                WeiXinToken token  = _tokenProvider.GetToken();
                if (token.JsTicket != null)
                {
                    ticket = token.JsTicket;
                    return(ticket);
                }

                AccessToken accessToken = this.GetAccessToken();


                string         url      = string.Format(WeiXinConst.JS_TICKET_URL, accessToken.Value);
                HttpWebRequest request  = (HttpWebRequest)WebRequest.Create(url);
                WebResponse    response = request.GetResponse();
                if (response.GetResponseStream() != null)
                {
                    StreamReader sr   = new StreamReader(response.GetResponseStream());
                    string       json = sr.ReadToEnd();

                    JavaScriptSerializer serlizer = new JavaScriptSerializer();
                    var obj = serlizer.Deserialize <dynamic>(json);
                    ticket.Value    = obj.ticket;
                    ticket.ExpireIn = obj.expires_in;
                    token.JsTicket  = ticket;
                    _tokenProvider.SaveToken(token);
                }
                return(ticket);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }