Beispiel #1
0
        /// <summary> Залогиниться в Creatio </summary>
        public static HttpClientOfCreatio LogIn(String baseAddress, String userName, String userPassword)
        {
            AuthResponse      loginStatus;
            HttpClientHandler handler = new HttpClientHandler()
            {
                CookieContainer = new CookieContainer()
            };
            HttpClientOfCreatio creatioClient = GetConfiguredClient(baseAddress, handler);
            HttpClient          client        = creatioClient.CreatioClient;
            String              authData      = JsonConvert.SerializeObject(new AuthRequest(userName, userPassword));
            StringContent       content       = new StringContent(authData, Encoding.UTF8, "application/json");
            HttpResponseMessage response      = client.PostAsync(creatioClient.AuthServiceUri, content).Result;

            try { response.EnsureSuccessStatusCode(); }
            catch (HttpRequestException ex)
            {
                String error = "\n\n" + JToken.Parse(response.Content.ReadAsStringAsync().Result).ToString(Formatting.Indented) + "\n";
                throw new Exception(error, ex);
            }
            String result = response.Content.ReadAsStringAsync().Result;

#if DEBUG
            result = JToken.Parse(result).ToString(Formatting.Indented);
#endif
            loginStatus = JsonConvert.DeserializeObject <AuthResponse>(result);
            // TODO Throw exception if not authorized
            if (loginStatus.Code != 0)
            {
                throw new InvalidOperationException("Не удалось авторизоваться");
            }
            Dictionary <String, Cookie> cookies = handler.CookieContainer.GetCookies(creatioClient.AuthServiceUri).Cast <Cookie>().ToDictionary(k => k.Name);
            creatioClient.CreatioClient.DefaultRequestHeaders.Add("ForceUseSession", "true");
            creatioClient.CreatioClient.DefaultRequestHeaders.Add("BPMCSRF", cookies["BPMCSRF"].Value);
            return(creatioClient);
        }
Beispiel #2
0
        private static HttpClientOfCreatio GetConfiguredClient(String baseAddress, HttpClientHandler handler = null)
        {
            HttpClientOfCreatio client     = new HttpClientOfCreatio(baseAddress);
            HttpClient          httpClient = handler is null ? new HttpClient() : new HttpClient(handler);

            httpClient.BaseAddress = client.BaseUri;
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.CreatioClient = httpClient;
            return(client);
        }