Ejemplo n.º 1
0
        public async Task <IActionResult> Index(LoginViewModel vModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(vModel));
            }

            var user = await _userModel.Login(vModel.Id, vModel.Password);

            if (user != null)
            {
                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Name, user.Id.ToString()),
                    new Claim(ClaimTypes.Role, user.Type)
                };

                var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                var authProperties = new AuthenticationProperties();
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                              new ClaimsPrincipal(claimsIdentity), authProperties);

                await _systemLogModel.Write(Define.LogType.LoginSuccess, vModel.Id);

                return(Redirect("~/stack/"));
            }
            else
            {
                await _systemLogModel.Write(Define.LogType.LoginFailure, vModel.Id);

                ModelState.AddModelError("Id", "AuthId");
            }
            return(View(vModel));
        }
Ejemplo n.º 2
0
        public async Task <ApiResponse> Index(string token)
        {
            var headers = HttpContext.Request.Headers.Keys.ToDictionary <string, string, string>(key => key, key => HttpContext.Request.Headers[key]);
            var body    = "";

            var contentType = HttpContext.Request.ContentType.ToLower();

            if (contentType.Contains("application/json"))
            {
                using (var stream = new StreamReader(HttpContext.Request.Body))
                {
                    body = stream.ReadToEnd();
                }
            }
            else if (contentType.Contains("application/x-www-form-urlencoded"))
            {
                var data = HttpContext.Request.Form.ToDictionary <KeyValuePair <string, StringValues>, string, object>(form => form.Key, form =>
                {
                    try
                    {
                        return(JsonConvert.DeserializeObject(form.Value));
                    }
                    catch (Exception e)
                    {
                        return(form.Value.Count > 1 ? (object)form.Value : form.Value.First());
                    }
                });
                body = JsonConvert.SerializeObject(data);
            }

            var result = new ApiResponse();

            try
            {
                var stack = await _stackModel.GetByToken(token);

                if (stack == null)
                {
                    result.Error = "Invalid token";
                }
                else
                {
                    await _logModel.WriteReceivedLog(new ReceivedLog
                    {
                        StackId = stack.Id,
                        Header  = JsonConvert.SerializeObject(headers),
                        Body    = body
                    });

                    var post = JObject.Parse(body);

                    var webHook = new WebHook(post, _httpClientFactory);

                    foreach (var task in stack.Tasks.Where(t => t.Enabled))
                    {
                        var taskContent = JsonConvert.DeserializeObject <TaskContent>(task.Content);
                        var(sentBody, response) = await webHook.Send(taskContent);

                        if (response != null)
                        {
                            await _logModel.WriteSentLog(new SentLog()
                            {
                                TaskId   = task.Id,
                                Url      = taskContent.Url,
                                Content  = sentBody,
                                Response = await response.Content.ReadAsStringAsync()
                            });
                        }
                    }

                    result.Result = "OK";
                }
            }
            catch (Exception ex)
            {
            }

            var message = new
            {
                Token  = token,
                Header = headers,
                Body   = body,
                Result = result.Result
            };
            await _systemLogModel.Write(Define.LogType.WebHook, JsonConvert.SerializeObject(message));

            return(result);
        }