示例#1
0
        /// <summary>
        /// 获得token
        /// </summary>
        private string GetAccessToken()
        {
            //数据验证
            Validate();
            //获得cache
            Cache.ICache cache = Cache.CacheFactory.Cache();
            //cache签名使用CorpID+AgentID+Name
            //string signature = WeiXinUtils.Base64Encode(CorpID +"$"+ AgentID + "$"  + Name);
            ///根据secret确认唯一性
            string signature = Secret;

            //如果_access_token存在,并未超时,直接返回
            if (string.IsNullOrEmpty(cache.GetCache <string>(signature)))
            {
                lock (objLock)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=");
                    sb.Append(CorpID);
                    sb.Append("&corpsecret=");
                    sb.Append(Secret);
                    ///http请求
                    WebUtils           webutils = new WebUtils();
                    string             strGet   = webutils.DoGet(sb.ToString());
                    Domain.TokenEntity token    = strGet.jsonToObj <Domain.TokenEntity>();
                    if (token.ErrCode != 0)
                    {
                        throw new WeiXinException(token.ErrCode);
                    }
                    cache.WriteCache <string>(token.AccessToken, signature, DateTime.Now.AddSeconds(token.ExpiresIn));
                    return(token.AccessToken);
                }
            }
            else
            {
                return(cache.GetCache <string>(signature));
            }
        }
示例#2
0
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="sessionId"></param>
 /// <param name="userKey">存储用户的key</param>
 /// <param name="cache"></param>
 public Session(string sessionId, Cache.ICache <string> cache)
 {
     this.sessionId = sessionId;
     this.cache     = cache;
 }