Exemplo n.º 1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Token"] == null)
            {
                if (HttpContext.Current.Request.QueryString["code"] != null)
                {
                    var code        = HttpContext.Current.Request.QueryString["code"];
                    var redirectUri = Session["Url"].ToString();
                    var token       = AccessToken.GetAccessToken(_connectUrl, code, redirectUri, _clientId, _clientSecret);
                    var tokenString = token.Token;
                    if (tokenString != null)
                    {
                        tcMainContainer.Enabled = true;
                        var profile  = Profile.GetProfile(_apiUrl, tokenString);
                        var accounts = TradingAccount.GetTradingAccounts(_apiUrl, tokenString);
                        ddlTradingAccounts.DataSource = accounts;
                        ddlTradingAccounts.DataBind();
                        Session["Token"]        = tokenString;
                        Session["RefreshToken"] = token.RefreshToken;
                    }
                }
                else
                {
                    Session["Url"] = HttpContext.Current.Request.Url.AbsoluteUri;
                    Response.Redirect(_connectUrl + "apps/auth?&client_id=" + _clientId + "&redirect_uri=" + HttpContext.Current.Request.Url.AbsoluteUri + "&scope=trading");
                }
            }

            _tcpClient = new TcpClient(_apiHost, _apiPort);;
            _apiSocket = new SslStream(_tcpClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
            _apiSocket.AuthenticateAsClient(_apiHost);
        }
Exemplo n.º 2
0
        public static ConnectAPI GetConnectAPI(AppIdentityDbContext identitycontext, AppDbContext context, string userName, int?acId)
        {
            ConnectAPI connectAPI   = new ConnectAPI();
            var        useraccounts = identitycontext.AspNetUserForexAccount.Where(u => u.AppIdentityUser.UserName == userName).ToList();
            var        frxaccounts  = new List <FrxAccount>();

            if (useraccounts.Count != 0)
            {
                frxaccounts = context.FrxAccount.Where(x => useraccounts.SingleOrDefault(s => s.AccountNumber == x.AccountNumber && s.Password == x.Password) != null).ToList();
            }
            if (frxaccounts.Count == 0)
            {
                connectAPI.AccountId = 0;
                return(connectAPI);
            }
            var frxaccount = new FrxAccount();

            if (acId == null)
            {
                var tempac1 = new AspNetUserForexAccount();
                var tempac2 = useraccounts.SingleOrDefault(x => x.Alive == true);
                if (tempac2 == null)
                {
                    tempac1 = useraccounts[0];
                }
                else
                {
                    tempac1 = tempac2;
                }
                frxaccount = frxaccounts.SingleOrDefault(x => x.AccountNumber == tempac1.AccountNumber);
            }
            else
            {
                frxaccount = frxaccounts.SingleOrDefault(x => x.AccountId == acId);
            }
            var account = TradingAccount.GetTradingAccounts(frxaccount.ApiUrl, frxaccount.AccessToken).SingleOrDefault(x => x.AccountId == frxaccount.AccountId);

            if (account != null)
            {
                frxaccount.Balance = account.Balance / 100;
            }
            else
            {
                connectAPI.AccountId = 0;
                return(connectAPI);
            }
            connectAPI.UserId                      = useraccounts[0].AppIdentityUserId;
            connectAPI.AccountId                   = frxaccount.AccountId;
            connectAPI.Balance                     = frxaccount.Balance;
            connectAPI.PreciseLeverage             = frxaccount.PreciseLeverage;
            connectAPI.ClientId                    = frxaccount.ClientId;
            connectAPI.ClientSecret                = frxaccount.ClientSecret;
            connectAPI.AccessToken                 = frxaccount.AccessToken;
            connectAPI.RefreshToken                = frxaccount.RefreshToken;
            connectAPI.ConnectUrl                  = frxaccount.ConnectUrl;
            connectAPI.ApiUrl                      = frxaccount.ApiUrl;
            connectAPI.ApiHost                     = frxaccount.ApiHost;
            connectAPI.ApiPort                     = frxaccount.ApiPort;
            connectAPI.TraderRegistrationTimestamp = frxaccount.TraderRegistrationTimestamp;
            return(connectAPI);
        }
Exemplo n.º 3
0
        // GET: Forex/FrxUserAccount
        public async Task <IActionResult> Index()
        {
            AppIdentityUser _user = await _userManager.FindByNameAsync(User.Identity.Name);

            if (User.Identity.Name == "lee890720" || _user.ConnectAPI)
            {
                string _accessToken = "";
                string _apiUrl      = "";
                _accessToken = _user.AccessToken;
                _apiUrl      = _user.ApiUrl;
                #region GetAccount
                var accounts = TradingAccount.GetTradingAccounts(_apiUrl, _accessToken);
                foreach (var a in accounts)
                {
                    var result = _context.FrxAccount.SingleOrDefault(x => x.AccountId == a.AccountId);
                    if (result == null)
                    {
                        var temp = new FrxAccount();
                        temp.AccountId     = a.AccountId;
                        temp.AccountNumber = a.AccountNumber;
                        temp.BrokerName    = a.BrokerTitle;
                        temp.Currency      = a.DepositCurrency;
                        temp.TraderRegistrationTimestamp = a.TraderRegistrationTimestamp;
                        temp.IsLive          = a.Live;
                        temp.Balance         = a.Balance;
                        temp.PreciseLeverage = a.Leverage;
                        temp.ClientId        = _user.ClientId;
                        temp.ClientSecret    = _user.ClientSecret;
                        temp.AccessToken     = _user.AccessToken;
                        temp.RefreshToken    = _user.RefreshToken;
                        temp.ConnectUrl      = _user.ConnectUrl;
                        temp.ApiUrl          = _user.ApiUrl;
                        temp.ApiHost         = _user.ApiHost;
                        temp.ApiPort         = _user.ApiPort;
                        _context.Add(temp);
                        await _context.SaveChangesAsync();
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(_user.ClientId) && !string.IsNullOrEmpty(_user.ClientSecret))
                        {
                            if (!string.IsNullOrEmpty(_user.AccessToken) && !string.IsNullOrEmpty(_user.RefreshToken))
                            {
                                if (!string.IsNullOrEmpty(_user.ConnectUrl) && !string.IsNullOrEmpty(_user.ApiUrl))
                                {
                                    if (!string.IsNullOrEmpty(_user.ApiHost) && _user.ApiPort != 0)
                                    {
                                        result.ClientId     = _user.ClientId;
                                        result.ClientSecret = _user.ClientSecret;
                                        result.AccessToken  = _user.AccessToken;
                                        result.RefreshToken = _user.RefreshToken;
                                        result.ConnectUrl   = _user.ConnectUrl;
                                        result.ApiUrl       = _user.ApiUrl;
                                        result.ApiHost      = _user.ApiHost;
                                        result.ApiPort      = _user.ApiPort;
                                        _context.Update(result);
                                        await _context.SaveChangesAsync();
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion
            }

            return(View(await _identitycontext.AspNetUserForexAccount.Where(x => x.AppIdentityUserId == _user.Id).ToListAsync()));
        }