Ejemplo n.º 1
0
        private void Connecting(string userName, string password)
        {
#if NETCOREAPP
            using (var clientRSA = RSA.Create())
#else
            using (var clientRSA = new RSACng())
#endif
            {
                var clientPK = clientRSA.Export();
                using (DataPackage request = new DataPackage("UserName.String.32", "PublicKey.String.256"))
                {
                    request.Headers.Add("ActionID", "Sys.LoginRequest");
                    request.Headers.Add("UserName", userName);
                    request.UpdateHeaders();
                    request.AddNew();
                    request["UserName"]  = userName;
                    request["PublicKey"] = clientPK;
                    request.Update();
                    using (var response = SendAndRecieve(request))
                    {
                        if (response.Headers.ContainsKey("Status") &&
                            (string)response.Headers["Status"] == "ERROR")
                        {
                            throw new TCPConnectorException(response);
                        }

                        response.GoDataTop();
                        response.Read();
                        _loginInfo = new LoginInfo();
                        _loginInfo.Import(clientRSA.Decrypt(((string)response["LoginInfo"]).ToByteArray(), _padding));
                        _clientAes = Aes.Create()
                                     .ImportBin(_loginInfo.CryptoKey);
                    }
                }

                using (var request = new DataPackage("UserName.String.32", "SessionID.String.34", "EncryptedKey.String.256"))
                {
                    request.Headers.Add("ActionID", "Sys.Logon");
                    request.Headers.Add("UserName", userName);
                    request.UpdateHeaders();
                    request.AddNew();
                    request["UserName"]     = userName;
                    request["SessionID"]    = _loginInfo.SessionID.ToString();
                    request["EncryptedKey"] = _clientAes
                                              .EncryptBin(Encoding.UTF8.GetBytes(password))
                                              .ToBASE64String();
                    request.Update();
                    using (var response = SendAndRecieve(request))
                    {
                        if (response.Headers.ContainsKey("Status") &&
                            (string)response.Headers["Status"] == "ERROR")
                        {
                            throw new TCPConnectorException(response);
                        }

                        response.GoDataTop();
                        response.Read();
                        var token = (string)response["Ticket"];
                        _ticket = new Guid(_clientAes.DecryptBin(token.ToByteArray()).Take(16).ToArray());
                        //_ticket = new Guid(((BinaryDataBuffer)_clientAes.DecryptBin(token.ToByteArray())).Slice(0, 16));
                        //_userName = userName;
                        Connected = true;
                    }
                }
            }
        }