Example #1
0
        public async Task <dynamic> CreateUser([FromBody] dynamic request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            string responseRecaptcha = request.responseRecaptcha;

            if (!Recaptcha.IsValid(responseRecaptcha, _env, _config))
            {
                var createResponse = new
                {
                    isValid = false,
                    error   = "Invalid captcha validation"
                };

                return(createResponse);
            }

            using (var httpClient = new HttpClient())
            {
                using (var content = new StringContent(JsonConvert.SerializeObject(request), System.Text.Encoding.UTF8, "application/json"))
                {
                    content.Headers.Clear();
                    content.Headers.Add("Content-Type", "application/json");
                    var response = await httpClient.PostAsync(this._config["AppApiDomain"] + "/api/user", content);

                    dynamic token = JsonConvert.DeserializeObject <dynamic>(await response.Content.ReadAsStringAsync());
                    return(token);
                }
            }
        }
Example #2
0
        public async Task <ClientToken> GetClientToken([FromBody] ClientLoginRequest request, Boolean?mock = false)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (!Recaptcha.IsValid(request.responseRecaptcha, _env, _config))
            {
                ClientToken ct = new ClientToken();
                ct.error             = "Invalid captcha validation";
                ct.error_description = "Invalid captcha validation";
                return(ct);
            }

            var BaseURL = this._config["AppApiDomain"] + "/api/user/authenticate";

            if (mock.HasValue && mock.Value)
            {
                BaseURL = "http://" + this.Request.Host.Value + ("/mocks/get-token.json");
                return(new ClientToken {
                    access_token = "token"
                });
            }

            var postData = new List <KeyValuePair <string, string> >();

            postData.Add(new KeyValuePair <string, string>("client_id", this._config["client_id"]));
            postData.Add(new KeyValuePair <string, string>("client_secret", this._config["client_secret"]));
            postData.Add(new KeyValuePair <string, string>("client_type", "webclient"));
            postData.Add(new KeyValuePair <string, string>("grant_type", "password"));
            postData.Add(new KeyValuePair <string, string>("username", request.username));
            postData.Add(new KeyValuePair <string, string>("password", request.password));
            postData.Add(new KeyValuePair <string, string>("TwoFactorAuthentication", request.twoFactorAuthentication));
            postData.Add(new KeyValuePair <string, string>("client_ip", this.Request.HttpContext.Connection.RemoteIpAddress.ToString()));


            using (var httpClient = new HttpClient())
            {
                using (var content = new FormUrlEncodedContent(postData))
                {
                    content.Headers.Clear();
                    content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                    var response = await httpClient.PostAsync(BaseURL, content);

                    var stringResponse = await response.Content.ReadAsStringAsync();

                    var token = JsonConvert.DeserializeObject <ClientToken>(stringResponse);
                    return(token);
                }
            }
        }