Exemplo n.º 1
0
        async static public Task <string> RefreshTokenAsync(string serviceUrl, string user, string password, string currentToken = "")
        {
            string currentParameter = currentToken;

            //lock (_refreshTokenLocker)
            {
                string dictKey = serviceUrl + "/" + user;

                if (_tokenParams.ContainsKey(dictKey) && _tokenParams[dictKey] != currentParameter)
                {
                    return(_tokenParams[dictKey]);
                }
                else
                {
                    int    pos             = serviceUrl.ToLower().IndexOf("/rest/");
                    string tokenServiceUrl = serviceUrl.Substring(0, pos) + "/tokens/generateToken";

                    //string tokenParams = $"request=gettoken&username={ user }&password={ password.UrlEncodePassword() }&expiration={ RequestTokenCache.TicketExpiration }&f=json";
                    string tokenParams = $"request=gettoken&username={ user }&password={ password.UrlEncodePassword() }&f=json";

                    string tokenResponse = String.Empty;
                    while (true)
                    {
                        try
                        {
                            tokenResponse = WebFunctions.HttpSendRequest($"{ tokenServiceUrl }?{ tokenParams }");
                            break;
                        }
                        catch (WebException we)
                        {
                            if (we.Message.Contains("(502)") && tokenServiceUrl.StartsWith("http://"))
                            {
                                tokenServiceUrl = "https:" + tokenServiceUrl.Substring(5);
                                continue;
                            }
                            throw we;
                        }
                    }
                    if (tokenResponse.Contains("\"error\":"))
                    {
                        JsonError error = JsonConvert.DeserializeObject <JsonError>(tokenResponse);
                        throw new Exception($"GetToken-Error:{ error.Error?.Code }\n{ error.Error?.Message }\n{ error.Error?.Details?.ToString() }");
                    }
                    else
                    {
                        JsonSecurityToken jsonToken = JsonConvert.DeserializeObject <JsonSecurityToken>(tokenResponse);
                        if (jsonToken.token != null)
                        {
                            _tokenParams.TryAdd(dictKey, jsonToken.token);
                            return(jsonToken.token);
                        }
                    }
                }
            }

            return(String.Empty);
        }
Exemplo n.º 2
0
        async public Task Run(string[] args)
        {
            string inFile = String.Empty;
            string server = String.Empty;
            string service = String.Empty;
            string folder = String.Empty;
            string client = String.Empty, secret = String.Empty, accessToken = String.Empty;

            for (int i = 1; i < args.Length - 1; i++)
            {
                switch (args[i].ToLower())
                {
                case "-mxl":
                    inFile = args[++i];
                    break;

                case "-server":
                    server = args[++i];
                    break;

                case "-service":
                    service = args[++i];
                    break;

                case "-client":
                    client = args[++i];
                    break;

                case "-secret":
                    secret = args[++i];
                    break;
                }
            }

            if (String.IsNullOrEmpty(inFile) ||
                String.IsNullOrEmpty(server) ||
                String.IsNullOrEmpty(service))
            {
                throw new IncompleteArgumentsException();
            }

            if (service.Contains("/"))
            {
                folder  = service.Substring(0, service.IndexOf("/"));
                service = service.Substring(service.IndexOf("/") + 1);
            }

            try
            {
                #region Get Access Token

                if (!String.IsNullOrEmpty(client) && !String.IsNullOrEmpty(secret))
                {
                    var tokenUrl    = $"{ server }/geoservices/tokens/generateToken";
                    var tokenParams = $"request=gettoken&username={ client }&password={ secret }&expiration=60&f=json";

                    using (var httpClient = new HttpClient())
                        using (var tokenRequest = new HttpRequestMessage(HttpMethod.Post, tokenUrl)
                        {
                            Content = new StringContent(tokenParams, Encoding.UTF8, "application/x-www-form-urlencoded")
                        })
                        {
                            var tokenResponse = await httpClient.SendAsync(tokenRequest);

                            if (tokenResponse.IsSuccessStatusCode)
                            {
                                var tokenResponseString = await tokenResponse.Content.ReadAsStringAsync();

                                if (tokenResponseString.Contains("\"error\":"))
                                {
                                    JsonError error = JsonConvert.DeserializeObject <JsonError>(tokenResponseString);
                                    throw new Exception($"GetToken-Error: { error.Error?.Code }\n{ error.Error?.Message }\n{ error.Error?.Details }");
                                }
                                else
                                {
                                    JsonSecurityToken jsonToken = JsonConvert.DeserializeObject <JsonSecurityToken>(tokenResponseString);
                                    if (jsonToken.token != null)
                                    {
                                        accessToken = jsonToken.token;
                                        Console.WriteLine($"Successfull requested access token ({ client }): { accessToken.Substring(0, 5) }........");
                                    }
                                }
                            }
                            else
                            {
                                throw new Exception($"Token request returned Statuscode { tokenResponse.StatusCode }");
                            }
                        }
                }

                #endregion

                var url = $"{ server }/BrowseServices/AddService?service={ service }&folder={ folder }&token={ accessToken }&f=json";
                using (var httpClient = new HttpClient())
                {
                    var requestContent = new MultipartFormDataContent();
                    var mxlContent     = new ByteArrayContent(await File.ReadAllBytesAsync(inFile));
                    mxlContent.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml");

                    requestContent.Add(mxlContent, "file", new FileInfo(inFile).Name);

                    var response = await httpClient.PostAsync(url, requestContent);

                    var mapServerResponse = JsonConvert.DeserializeObject <AdminMapServerResponse>(await response.Content.ReadAsStringAsync());
                    if (mapServerResponse.Success == false)
                    {
                        throw new Exception($"Error on publishing service:{ Environment.NewLine }{ mapServerResponse.Message }");
                    }

                    Console.WriteLine($"Service { folder }/{ service } successfully published...");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception:");
                Console.WriteLine(ex.Message);
            }
        }