예제 #1
0
        public ActionResult GetAccessTokenBags()
        {
            if (!Request.IsLocal)
            {
                return(new HttpUnauthorizedResult());//只允许本地访问
            }
            var accessTokenBags = AccessTokenContainer.GetAllItems();

            return(Json(accessTokenBags, JsonRequestBehavior.AllowGet));
        }
예제 #2
0
        public ActionResult GetAccessTokenBags()
        {
            if (!Request.IsLocal())
            {
                return(new UnauthorizedResult());//只允许本地访问
            }
            var accessTokenBags = AccessTokenContainer.GetAllItems();

            return(Json(accessTokenBags));
        }
예제 #3
0
        public void ContainerTest()
        {
            //注册
            AccessTokenContainer.Register(base._appId, base._appSecret);

            //获取Token完整结果(包括当前过期秒数)
            DateTime dt1         = DateTime.Now;
            var      tokenResult = AccessTokenContainer.GetAccessTokenResult(base._appId);
            DateTime dt2         = DateTime.Now;

            Assert.IsNotNull(tokenResult);
            Console.WriteLine(tokenResult.access_token);
            Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);

            //只获取Token字符串
            dt1 = DateTime.Now;
            var token = AccessTokenContainer.GetAccessToken(base._appId);

            dt2 = DateTime.Now;
            Assert.AreEqual(tokenResult.access_token, token);
            Console.WriteLine(tokenResult.access_token);
            Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);

            //getNewToken
            {
                dt1   = DateTime.Now;
                token = AccessTokenContainer.TryGetAccessToken(base._appId, base._appSecret, false);
                dt2   = DateTime.Now;
                Assert.AreEqual(tokenResult.access_token, token);
                Console.WriteLine(tokenResult.access_token);

                Console.WriteLine("强制重新获取AccessToken");
                dt1   = DateTime.Now;
                token = AccessTokenContainer.TryGetAccessToken(base._appId, base._appSecret, true);
                dt2   = DateTime.Now;
                Assert.AreNotEqual(tokenResult.access_token, token);//如果微信服务器缓存,此处会相同
                Console.WriteLine(tokenResult.access_token);
                Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);
            }

            {
                tokenResult = AccessTokenContainer.GetAccessTokenResult(base._appId);
                Console.WriteLine("HashCode:{0}", tokenResult.GetHashCode());
                dt1 = DateTime.Now;
                var allItems = AccessTokenContainer.GetAllItems();
                dt2 = DateTime.Now;
                Assert.IsTrue(allItems.Count > 0);
                Assert.AreSame(tokenResult, allItems[0].AccessTokenResult);//证明缓存成功
                Console.WriteLine("All Items:{0}", allItems.Count);
                Console.WriteLine("HashCode:{0}", allItems[0].AccessTokenResult.GetHashCode());
                Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);
            }
        }
예제 #4
0
        public ActionResult Index()
        {
            var guid = Guid.NewGuid().ToString("n");

            ViewData["Guid"] = guid;

            var configHelper = new ConfigHelper();

            int qrCodeId = 0;
            CreateQrCodeResult qrResult = null;

            try
            {
                //chm二维码
                qrCodeId = configHelper.GetQrCodeId();
                qrResult = MP.AdvancedAPIs.QrCodeApi.Create(appId, 10000, qrCodeId, QrCode_ActionName.QR_SCENE);

                var qrCodeUrl = MP.AdvancedAPIs.QrCodeApi.GetShowQrCodeUrl(qrResult.ticket);
                ViewData["QrCodeUrl"] = qrCodeUrl;
            }
            catch (Exception e)
            {
                WeixinTrace.SendCustomLog("Document发生appsecret错误!", e.ToString());
                var accessTokenBags = AccessTokenContainer.GetAllItems();

                WeixinTrace.SendCustomLog("当前AccessToken信息", accessTokenBags.ToJson());
            }
            finally
            {
                ConfigHelper.CodeCollection[guid] = new CodeRecord()
                {
                    Key          = guid,
                    QrCodeId     = qrCodeId,
                    QrCodeTicket = qrResult
                };//添加对应关系

                //下载版本
                var config = configHelper.GetConfig();
                ViewData["Versions"]      = config.Versions;
                ViewData["WebVersions"]   = config.WebVersions;
                ViewData["DownloadCount"] = config.DownloadCount.ToString("##,###");
            }
            return(View());
        }
예제 #5
0
        public void ContainerTest()
        {
            MutipleCacheTestHelper.RunMutipleCache(() =>
            {
                //获取Token完整结果(包括当前过期秒数)
                DateTime dt1    = DateTime.Now;
                var tokenResult = AccessTokenContainer.GetAccessTokenResult(base._appId);
                DateTime dt2    = DateTime.Now;

                Assert.IsNotNull(tokenResult);
                Console.WriteLine(tokenResult.access_token);
                Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);

                if (base._useRedis)
                {
                    Thread.Sleep(2500);//等待缓存更新
                }

                //只获取Token字符串
                dt1       = DateTime.Now;
                var token = AccessTokenContainer.GetAccessToken(base._appId);
                dt2       = DateTime.Now;
                Assert.AreEqual(tokenResult.access_token, token);
                Console.WriteLine(tokenResult.access_token);
                Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);

                //getNewToken
                {
                    dt1   = DateTime.Now;
                    token = AccessTokenContainer.TryGetAccessToken(base._appId, base._appSecret, false);
                    dt2   = DateTime.Now;
                    Console.WriteLine(token);
                    Assert.AreEqual(tokenResult.access_token, token);

                    Console.WriteLine("强制重新获取AccessToken");
                    dt1   = DateTime.Now;
                    token = AccessTokenContainer.TryGetAccessToken(base._appId, base._appSecret, true);
                    dt2   = DateTime.Now;
                    Console.WriteLine(token);
                    Assert.AreNotEqual(tokenResult.access_token, token);//如果微信服务器缓存,此处会相同
                    Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);
                }

                {
                    tokenResult = AccessTokenContainer.GetAccessTokenResult(base._appId);
                    if (base._useRedis)
                    {
                        Thread.Sleep(2500);//等待缓存更新
                    }
                    Console.WriteLine("HashCode:{0}", tokenResult.GetHashCode());
                    dt1          = DateTime.Now;
                    var allItems = AccessTokenContainer.GetAllItems();
                    dt2          = DateTime.Now;
                    Assert.IsTrue(allItems.Count > 0);

                    //序列化
                    var d1 = StackExchangeRedisExtensions.Serialize(tokenResult);
                    var d2 = StackExchangeRedisExtensions.Serialize(allItems[0].AccessTokenResult);

                    Assert.AreEqual(String.Concat(d1), String.Concat(d2));//证明缓存成功
                    Console.WriteLine("All Items:{0}", allItems.Count);
                    Console.WriteLine("HashCode:{0}", allItems[0].AccessTokenResult.GetHashCode());
                    Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);
                }
            }, CacheType.Local, CacheType.Redis);
        }
예제 #6
0
        public void ContainerAsyncTest()
        {
            bool useRedis = true;

            if (useRedis)
            {
                var redisConfiguration = "localhost:6379";
                RedisManager.ConfigurationOption = redisConfiguration;
                CacheStrategyFactory.RegisterObjectCacheStrategy(() => RedisObjectCacheStrategy.Instance);//Redis
            }

            //注册
            AccessTokenContainer.Register(base._appId, base._appSecret);

            //获取Token完整结果(包括当前过期秒数)
            var dt1         = SystemTime.Now;
            var tokenResult = AccessTokenContainer.GetAccessTokenResultAsync(base._appId).Result;
            var dt2         = SystemTime.Now;

            Assert.IsNotNull(tokenResult);
            Console.WriteLine(tokenResult.access_token);
            Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);

            //只获取Token字符串
            dt1 = SystemTime.Now;
            var token = AccessTokenContainer.GetAccessTokenAsync(base._appId).Result;

            dt2 = SystemTime.Now;
            Assert.AreEqual(tokenResult.access_token, token);
            Console.WriteLine(tokenResult.access_token);
            Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);

            //getNewToken
            {
                dt1   = SystemTime.Now;
                token = AccessTokenContainer.TryGetAccessTokenAsync(base._appId, base._appSecret, false).Result;
                dt2   = SystemTime.Now;
                Assert.AreEqual(tokenResult.access_token, token);
                Console.WriteLine(tokenResult.access_token);

                Console.WriteLine("强制重新获取AccessToken");
                dt1   = SystemTime.Now;
                token = AccessTokenContainer.TryGetAccessTokenAsync(base._appId, base._appSecret, true).Result;
                dt2   = SystemTime.Now;
                Assert.AreNotEqual(tokenResult.access_token, token);//如果微信服务器缓存,此处会相同
                Console.WriteLine(tokenResult.access_token);
                Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);
            }

            {
                tokenResult = AccessTokenContainer.GetAccessTokenResultAsync(base._appId).Result;
                Console.WriteLine("HashCode:{0}", tokenResult.GetHashCode());
                dt1 = SystemTime.Now;
                var allItems = AccessTokenContainer.GetAllItems();
                dt2 = SystemTime.Now;
                Assert.IsTrue(allItems.Count > 0);
                Assert.AreSame(tokenResult, allItems[0].AccessTokenResult);//证明缓存成功
                Console.WriteLine("All Items:{0}", allItems.Count);
                Console.WriteLine("HashCode:{0}", allItems[0].AccessTokenResult.GetHashCode());
                Console.WriteLine("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds);
            }
        }