Exemple #1
0
        public override async Task <ResponseResult <TResponseParser> > Execute()
        {
            var webRequestBuilder = new ASWebRequestBuilder();

            string xml = this.Parameters.BuildXml(this.CommandName);

            NetworkCredential credential = new NetworkCredential(this.Settings.Login, this.Settings.Password);

            string url = string.Format("{0}{1}?Cmd={2}&User={3}&DeviceId={4}&DeviceType={5}",
                                       this.Settings.HostName,
                                       ServerUrlDirectory,
                                       this.CommandName,
                                       this.Settings.Login,
                                       this.Settings.DeviceId,
                                       this.Settings.DeviceType);

            var headers = new Dictionary <string, string>
            {
                { "MS-ASProtocolVersion", this.Settings.ProtocolVersion },
                { "X-MS-PolicyKey", this.Settings.PolicyKey.ToString() },
            };

            WebRequestResponse response = null;

            try
            {
                response = await webRequestBuilder.SendRequestAsync(
                    url,
                    HttpMethod.Post,
                    ContentTypeWbXml,
                    xml,
                    headers,
                    credential);

                // settings must be updated if a redirection occured
                string requestUri = response.Request.RequestUri.GetSchemeAndHost();
                if (requestUri.TrimEnd('/') != this.Settings.HostName.TrimEnd('/'))
                {
                    string oldHostName = this.Settings.HostName;
                    this.Settings.HostName = requestUri;
                    LogService.Log("ASCommandBase", $"Successfull redirection from {oldHostName} to {requestUri}");
                }

                if (response.Response.IsSuccessStatusCode)
                {
                    var parser = new TResponseParser();
                    parser.ParseResponse(this.CommandName, response);

                    return(ResponseResult <TResponseParser> .Create(parser, requestUri));
                }
                else
                {
                    return(this.CreateResponseResult(null, response, requestUri));
                }
            }
            catch (Exception ex)
            {
                return(this.CreateResponseResult(ex, response, url));
            }
        }
Exemple #2
0
        private async Task <ResponseResult <TResponseParser> > SendRequestAsync(string xml, string username, string password, bool secondTry = false)
        {
            WebRequestResponse response = null;

            try
            {
                response = await this.webRequestBuilder.SendRequestAsync(
                    this.Settings.ServerUri,
                    HttpMethod.Post,
                    ContentTypeTextXml,
                    xml,
                    new Dictionary <string, string>(),
                    new NetworkCredential(username, password));

                if (response.Response.IsSuccessStatusCode)
                {
                    var parser = new TResponseParser();
                    parser.ParseResponse(this.CommandName, response);

                    return(ResponseResult <TResponseParser> .Create(parser, this.Settings.ServerUri));
                }
                else if (!secondTry && response.Response.StatusCode == HttpStatusCode.Unauthorized && this.Settings.Email != null && this.Settings.Username != this.Settings.Email)
                {
                    // one more try using email for authentication
                    return(await this.SendRequestAsync(xml, this.Settings.Email, this.Settings.Password, true));
                }
                else
                {
                    return(this.CreateResponseResult(null, response, this.Settings.ServerUri));
                }
            }
            catch (Exception ex)
            {
                return(this.CreateResponseResult(ex, response, this.Settings.ServerUri));
            }
        }