コード例 #1
0
        public void TestCreateFileUpload()
        {
            using (var client = PrivateClient.CreateClient(new PrivateServerSettings()
            {
                ServerUrl = server, LoggerFactory = TestLoggerFactoryAttribute.CurrentLoggerFactory
            }))
                using (var session = client.CreateSession(auth))
                {
                    var    cookie = PrivateConnectionHooks.CookieExtractor(out Func <string, string> func);
                    string token  = func.Invoke("YII_CSRF_TOKEN");

                    var str = "Client ID,Client Name,Date Updated,Updated by";

                    var createFileUploadRequest = CreateFileUploadRequest.Builder()
                                                  .FrmIf(new HRBCClientPrivate.API.File.JsonFile.FrmIf {
                        Size = "86", Name = "ImportTestCSVFile.csv", ContentType = "application/vnd.ms-excel", Content = StringToBase64String(str)
                    })
                                                  .CsrfToken(token);

                    Task <CreateFileUploadResponse> task = session.CallAsync(createFileUploadRequest.Build());
                    TestContext.Out.WriteLine("Take the second cup of coffee in meantime!");
                    CreateFileUploadResponse fieldData = task.Result;
                    Assert.That(fieldData, Is.Not.Null, "Response data is null!");
                    Assert.AreEqual(fieldData.FieldId, "Test Tester", "Wrong field name value!");
                }
        }
コード例 #2
0
ファイル: LoginHandler.cs プロジェクト: tranxuannam/bk_data
        private async Task <LoginResponse> DoLogin(PrivateSession session, Func <PrivateConnection> connectionFactory, SessionCsrfPair sessionCsrfPair, LoginRequest request, IAuthentication cred)
        {
            if (sessionCsrfPair == null)
            {
                return(new LoginResponse(null));
            }

            try
            {
                Func <string, string>      cookieExtractor;
                Func <HttpResponseHeaders> headerExtractor;
                using (var connection = connectionFactory())
                    using (var response = await connection
                                          .AppendHook(PrivateConnectionHooks.ForceQuery("index/Login?name=yt0"))
                                          .AppendHook(PrivateConnectionHooks.CookieExtractor(out cookieExtractor))
                                          .AppendHook(PrivateConnectionHooks.HeaderExtractor(out headerExtractor))
                                          .AppendHook(Hooks.DisableAutoRedirect)
                                          .Method(PrivateConnectionMethod.Form)
                                          .SuccessCondition(code => code != 301)
                                          .SendAsync(new Form.Login {
                        Crlf = sessionCsrfPair.Csrf, Auth = new Form.Auth {
                            Company = request.Company, Username = request.Username, Password = request.Password
                        }
                    }))
                    {
                        var locationHeader = headerExtractor().Location;

                        if (locationHeader != null && !locationHeader.AbsolutePath.EndsWith("/index/login"))
                        {
                            UpdateIdInfo(session, cookieExtractor);
                            return(new LoginResponse(cookieExtractor(SessionIdCookieName)));
                        }

                        throw new AuthenticationException(cred, $"Could not login to server (cookiekey:{SessionIdCookieName}, cslf:{sessionCsrfPair.Csrf}, company:{request.Company}, user:{request.Username}, password:{request.Password}, redirect: {((locationHeader == null) ? "null" : locationHeader.AbsolutePath)}, code: {response.Code})");
                    }
            }
            catch (ConnectionResponseException e)
            {
                throw new AuthenticationException(cred, "Could not login to server", e);
            }
            catch (AggregateException e)
            {
                e.Handle(ex =>
                {
                    if (ex is ConnectionResponseException)
                    {
                        throw new AuthenticationException(cred, "Could not login to server", ex);
                    }
                    return(false);
                });
                throw e;
            }
        }
コード例 #3
0
ファイル: LoginHandler.cs プロジェクト: tranxuannam/bk_data
        private async Task <SessionCsrfPair> GetSession(Func <PrivateConnection> connectionFactory)
        {
            Func <string, string> cookieExtractor;

            using (var connection = connectionFactory())
                using (var loginPage = await connection.AppendHook(PrivateConnectionHooks.ForceQuery("index/Login")).AppendHook(PrivateConnectionHooks.CookieExtractor(out cookieExtractor)).SendAsync())
                    using (var stream = new MemoryStream())
                        using (var reader = new StreamReader(stream))
                        {
                            var sessionId = cookieExtractor(SessionIdCookieName);

                            await loginPage.CopyToAsync(stream);

                            stream.Position = 0;
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                var m = RegexCsrf.Match(line);
                                if (m.Success)
                                {
                                    return(new SessionCsrfPair(sessionId, m.Groups[0].Value));
                                }
                            }
                            return(null);
                        }
        }
コード例 #4
0
        protected virtual string determineHrbcAccount(PrivateSession session, Func <PrivateConnection> connectionFactory)
        {
            if (session.Authentication().Account != null)
            {
                return(session.Authentication().Account);
            }
            Func <string, string> cookieExtractor;

            using (var connection = connectionFactory())
            {
                //get login page and use cookie
                var task = connection.AppendHook(PrivateConnectionHooks.ForceQuery("/index/Login")).AppendHook(PrivateConnectionHooks.CookieExtractor(out cookieExtractor)).SendAsync();
                task.Wait();
                using (var loginPage = task.Result)
                {
                    var hrbcAccount = cookieExtractor("HRBCACCOUNT");
                    session.Cache.Set("HrbcAccount", hrbcAccount, TimeSpan.FromHours(1));
                    return(hrbcAccount);
                }
            }
        }
コード例 #5
0
        public async Task <HrbcAccountInfoResponse> Handle(PrivateSession session, HrbcAccountInfoRequest request, Func <PrivateConnection> connectionFactory)
        {
            object cacheEntry;
            string hrbcAccount;

            if (session.Cache.TryGetValue("HrbcAccount", out cacheEntry))
            {
                hrbcAccount = (string)cacheEntry;
            }
            else
            {
                // Um.. no cookie cache? need to refresh. get login page and use cookies to refresh
                Func <string, string> cookieExtractor;
                using (var connection = connectionFactory())
                    using (var loginPage = await connection.AppendHook(PrivateConnectionHooks.ForceQuery("/index/Login")).AppendHook(PrivateConnectionHooks.CookieExtractor(out cookieExtractor)).SendAsync())
                    {
                        hrbcAccount = cookieExtractor("HRBCACCOUNT");
                        var entry = session.Cache.CreateEntry("HrbcAccount");
                        entry.SlidingExpiration = TimeSpan.FromHours(1);
                        entry.Value             = hrbcAccount;
                    }
            }
            var properties = hrbcAccount.Split('-');

            return(new HrbcAccountInfoResponse(new Dictionary <HrbcAccountInfoProperty, object> {
                { HrbcAccountInfoProperty.CompanyId, properties[0] }, { HrbcAccountInfoProperty.UserId, properties[1] }
            }));
        }