Пример #1
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                                      CancellationToken cancellationToken)
        {
            if (request.AuthHeadersInvalid())
            {
                SessionExpired?.Invoke(this, EventArgs.Empty);
                return(new UnauthorizedResponse());
            }

            var refreshTask = _refreshTask;

            if (!refreshTask.IsCompleted)
            {
                var refreshSucceeded = await refreshTask;
                return(await ResendAsync(request, cancellationToken, refreshSucceeded));
            }

            var response = await base.SendAsync(request, cancellationToken);

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                try
                {
                    var refreshSucceeded = await Refresh(refreshTask, cancellationToken);

                    return(await ResendAsync(request, cancellationToken, refreshSucceeded));
                }
                finally
                {
                    response.Dispose();
                }
            }

            return(response);
        }
        private async Task <HttpResponseMessage> ResendAsync(HttpRequestMessage request, CancellationToken cancellationToken,
                                                             bool refreshSucceeded)
        {
            if (refreshSucceeded)
            {
                PrepareRequest(request);
                return(await base.SendAsync(request, cancellationToken));
            }

            SessionExpired?.Invoke(this, EventArgs.Empty);
            return(new UnauthorizedResponse());
        }
Пример #3
0
        // Static Methods

        /// <summary>
        /// Clears any cached session for the specified <paramref name="sessionID"/>.
        /// </summary>
        /// <param name="sessionID">Identifier of session to clear.</param>
        /// <returns><c>true</c> if session was found and cleared; otherwise, <c>false</c>.</returns>
        public static bool ClearSessionCache(Guid sessionID)
        {
            Tuple <DynamicViewBag, Ticks> session;

            if (s_sessionCache.TryRemove(sessionID, out session))
            {
                SessionExpired?.Invoke(null, new EventArgs <Guid, DynamicViewBag>(sessionID, session.Item1));
                return(true);
            }

            return(false);
        }
        private async Task <HttpResponseMessage> ResendAsync(HttpRequestMessage request,
                                                             CancellationToken cancellationToken,
                                                             RefreshTokenStatus refreshTokenStatus)
        {
            switch (refreshTokenStatus)
            {
            case RefreshTokenStatus.Success:
                PrepareRequest(request);
                return(await base.SendAsync(request, cancellationToken));

            case RefreshTokenStatus.Unauthorized:
                SessionExpired?.Invoke(this, EventArgs.Empty);
                return(new UnauthorizedResponse());

            default:
                return(new UnauthorizedResponse());
            }
        }
Пример #5
0
        public async Task CheckNewMessages()
        {
            string syncCheckResultCopy;

            while (checkingMessages)
            {
                try
                {
                    SyncCheckStarted?.Invoke(this, null);
                    string syncCheckUrl = "https://webpush." + host + "/cgi-bin/mmwebwx-bin/synccheck?r=" + GenerateMessageID() + "&skey=" + System.Net.WebUtility.UrlEncode(save.skey) + "&sid=" + System.Net.WebUtility.UrlEncode(save.wxsid) + "&uin=" + System.Net.WebUtility.UrlEncode(save.wxuin) + "&deviceid=e177768502217168&synckey=";
                    for (int i = 0; i < SyncCheckKeys.Count; i++)
                    {
                        if (i == SyncCheckKeys.Count - 1)
                        {
                            syncCheckUrl += SyncCheckKeys[i].Key + "_" + SyncCheckKeys[i].Val;
                        }
                        else
                        {
                            syncCheckUrl += SyncCheckKeys[i].Key + "_" + SyncCheckKeys[i].Val + "%7C";
                        }
                    }
                    string syncCheckResult = await netHandler.GETRequestAsync(syncCheckUrl);

                    syncCheckResultCopy = syncCheckResult;
                    NetworkHandler.ReadUntil(ref syncCheckResult, "retcode:\"");
                    string retCode = NetworkHandler.ReadUntil(ref syncCheckResult, "\"");
                    NetworkHandler.ReadUntil(ref syncCheckResult, "selector:\"");
                    string selector = NetworkHandler.ReadUntil(ref syncCheckResult, "\"");
                    SyncCheckCompleted?.Invoke(this, new SyncCheckCompletedEventArgs()
                    {
                        retcode = retCode, selector = selector
                    });
                    if (selector != "0")
                    {
SyncAgain:
                        SyncStarted?.Invoke(this, null);
                        SyncRequest SR = new SyncRequest()
                        {
                            BaseRequest = BR, rr = -Convert.ToInt64(GenerateMessageID())
                        };
                        SR.SyncKey = new SyncKey()
                        {
                            Count = SyncKeys.Count
                        };
                        SR.SyncKey.List = new List <SyncKeyItem>();
                        foreach (SyncKeyItem key in SyncKeys)
                        {
                            SR.SyncKey.List.Add(key);
                        }
                        string syncResult = await netHandler.POSTRequestAsync("https://" + host + "/cgi-bin/mmwebwx-bin/webwxsync?sid=" + System.Net.WebUtility.UrlEncode(save.wxsid) + "&skey=" + System.Net.WebUtility.UrlEncode(save.skey), JsonConvert.SerializeObject(SR));

                        SyncResponse syncRes = JsonConvert.DeserializeObject <SyncResponse>(syncResult);

                        try
                        {
                            List <SyncKeyItem> SyncCheckKeysCopy = new List <SyncKeyItem>();
                            List <SyncKeyItem> SyncKeysCopy      = new List <SyncKeyItem>();
                            foreach (SyncKeyItem key in syncRes.SyncKey.List)
                            {
                                SyncKeysCopy.Add(key);
                            }
                            foreach (SyncKeyItem key in syncRes.SyncCheckKey.List)
                            {
                                SyncCheckKeysCopy.Add(key);
                            }
                            SyncCheckKeys = SyncCheckKeysCopy;
                            SyncKeys      = SyncKeysCopy;
                        }
                        catch
                        {
                            SyncOrSyncCheckError?.Invoke(this, new SyncOrSyncCheckErrorEventArgs()
                            {
                                errorMessage = "Sync returns EMPTY"
                            });
                        }

                        SyncCompleted?.Invoke(this, new SyncCompletedEventArgs()
                        {
                            syncResponse = syncRes
                        });
                    }
                    else if (retCode == "1101")
                    {
                        SessionExpired?.Invoke(this, null);
                        checkingMessages = false;
                        return;
                    }
                }
                catch (Exception ex)
                {
                    SyncOrSyncCheckError?.Invoke(this, new SyncOrSyncCheckErrorEventArgs()
                    {
                        errorMessage = ex.ToString()
                    });
                }
            }
        }
Пример #6
0
 private void OnSessionExpired()
 {
     SessionExpired?.Invoke(this, System.EventArgs.Empty);
 }
Пример #7
0
 private static void OnSessionExpired(Guid sessionID, DynamicViewBag sessionState)
 {
     SessionExpired?.Invoke(typeof(SessionHandler), new EventArgs <Guid, DynamicViewBag>(sessionID, sessionState));
 }