Пример #1
0
        /// <summary>
        /// Try to login into user account.
        /// </summary>
        /// <param name="login">users login.</param>
        /// <param name="password">users password.</param>
        /// <returns>server's response.</returns>
        /// <exception cref="ConnectionInterruptedException"></exception>
        /// <exception cref="CertificateException"></exception>
        public async Task <LoginRegisterResponse> Login(string login, string password)
        {
            if (isLoggedIn)
            {
                return(LoginRegisterResponse.Error);
            }

            // certificate
            X509Certificate2 cert = SslTools.CreateCertificate(typeof(Client), "CryptoMessenger.Certificate.cert.pfx");

            await client.ConnectWithTimeoutAsync(ip, port, cert, Properties.Settings.Default.WaitServerConnectionDelayMsec);

            LoginRegisterResponseMessage serverResp;

            try
            {
                // send login request
                client.SendMessage(new LoginRequestMessage
                {
                    Login    = login,
                    Password = password
                });

                // async wait for server's response
                serverResp = (LoginRegisterResponseMessage)await client.ReceiveMessageAsync();
            }
            catch (ConnectionInterruptedException)
            {
                client.Close();
                throw;
            }

            // dont disconnect if login success
            if (LoginRegisterResponse.Success.Equals(serverResp.Response))
            {
                isLoggedIn = true;
                Name       = login;
            }
            else
            {
                client.Close();
            }

            return(serverResp.Response);
        }