Ejemplo n.º 1
0
        public static async Task LogOut()
        {
            var request = new HttpRequestMessage(HttpMethod.Delete, "tokens");

            request.Headers.Authorization = new AuthenticationHeaderValue("Basic", $"{_token}:".ToBase64String());
            await Client.SendAsync(request).ConfigureAwait(false);

            _token = Guid.Empty;

            lock (UsersLock)
            {
                _usersClient.Dispose();
                _usersClient  = null;
                _usersCreated = false;
            }

            lock (ChatsLock)
            {
                _chatsClient.Dispose();
                _chatsClient  = null;
                _chatsCreated = false;
            }

            lock (MessagesLock)
            {
                _messagesClient?.Dispose();
                _messagesClient  = null;
                _messagesCreated = false;
            }

            _poller?.Dispose();
            _poller = null;
        }
Ejemplo n.º 2
0
        public static async Task <bool> LoginAsync(string login, string password)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, "tokens");

            request.Headers.Authorization =
                new AuthenticationHeaderValue("Basic", $"{login}:{password}".ToBase64String());
            try
            {
                var response = await Client.SendAsync(request).ConfigureAwait(false);

                var retString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    _token = Guid.Parse(new string(retString.Where(c => c != '"').ToArray()));



                    _poller             = new NewEventsPoller(ConnectionString, _token, UsersClient, ChatsClient);
                    _usersClient.Poller = _poller;
                    _chatsClient.Poller = _poller;
                    ((MessagesClient)MessagesClient).Poller = _poller;
                    return(true);
                }
                _token = Guid.Empty;
                return(false);
            }
            catch (Exception)
            {
                _token = Guid.Empty;
                return(false);
            }
        }