public async Task <JsonResult> ExecuteBatchStepByObserveId(string ObserveId) { ObserverDbContext _db = new ObserverDbContext(); ObservesModel model = _db.Observes .Where(e => e.Id == ObserveId) .FirstOrDefault(); if (model.Users.Id == User.Identity.GetUserId()) { List <StepsModel> listModel = _db.Observes.Where(e => e.Id == ObserveId).FirstOrDefault().List_Steps.Where(e => e.Deleted == false).OrderBy(e => e.Order).ToList(); List <StepExecutionInputModel> inputList = new List <StepExecutionInputModel>(); PCMethodsModel pcMethodsModel = null; foreach (StepsModel item in listModel) { string PC1 = null; string PC2 = null; int PCMethod = 0; if (item.PC_Method != null) { pcMethodsModel = PCMethodServiceManager.GetPCMethodById(item.PC_Method.Id); PC1 = pcMethodsModel.PC1; PC2 = pcMethodsModel.PC2; PCMethod = int.Parse(item.PC_Method.Id); } StepExecutionInputModel input = new StepExecutionInputModel() { Order = item.Order.GetValueOrDefault(), Uri = item.Url, Method = item.Method.GetValueOrDefault(), PC_Method = PCMethod, PC1 = item.PC1, PC2 = item.PC2, PC2Secret = item.PC2Secret, CustomHeader = item.PredefinedHeader, UseHeader = item.SetHeader.GetValueOrDefault(), LabelPC1 = PC1, LabelPC2 = PC2 }; inputList.Add(input); } return(Json(await StepServiceManager.ExecuteBatchStepReturningResult(inputList))); } else { StepExecutionOutputModel input = new StepExecutionOutputModel() { Status = "Unauthorized" }; return(Json(input)); } }
public async Task <JsonResult> ExecuteStep(string StepId) { ObserverDbContext _db = new ObserverDbContext(); StepsModel stepsModel = _db.Steps .Where(e => e.Id == StepId) .FirstOrDefault(); ObservesModel observesModel = stepsModel.Observes; if (observesModel.Users.Id == User.Identity.GetUserId()) { string pcMethodsModelId = null; string LabelPC1 = null; string LabelPC2 = null; int pcMethodsModelIdInt = 0; if (stepsModel.PC_Method != null) { pcMethodsModelId = stepsModel.PC_Method.Id; LabelPC1 = stepsModel.PC_Method.PC1; LabelPC2 = stepsModel.PC_Method.PC2; pcMethodsModelIdInt = int.Parse(pcMethodsModelId); } StepExecutionInputModel input = new StepExecutionInputModel() { Order = stepsModel.Order.GetValueOrDefault(), Uri = stepsModel.Url, Method = stepsModel.Method.GetValueOrDefault(), PC_Method = pcMethodsModelIdInt, PC1 = stepsModel.PC1, PC2 = stepsModel.PC2, CustomHeader = stepsModel.PredefinedHeader, PredefinedHeader = stepsModel.SetHeader.GetValueOrDefault(), LabelPC1 = LabelPC1, LabelPC2 = LabelPC2, PC2Secret = stepsModel.PC2Secret }; return(Json(await StepServiceManager.ExecuteStepReturningResult(input))); } else { StepExecutionOutputModel input = new StepExecutionOutputModel() { Status = "Unauthorized" }; return(Json(input)); } }
public static async Task <StepExecutionOutputModel> ExecuteBatchStepReturningResult(List <Observer.API.ViewModels.StepExecutionInputModel> Input) { ObserverDbContext _db = new ObserverDbContext(); try { Uri uri = null; HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = new CookieContainer(); StringContent queryString = null; HttpClient client = new HttpClient(handler); string respond = null; HttpResponseMessage response = null; string decrypt = null; foreach (Observer.API.ViewModels.StepExecutionInputModel item in Input) { if (item.PC2 != null) { decrypt = EncryptionAlgorithmServiceManager.DecryptStringFromBytes_Aes(Convert.FromBase64String(item.PC2), Convert.FromBase64String(_db._AESSecretKeyS), Convert.FromBase64String(item.PC2Secret)); } if (item.UseHeader == 0) { handler = new HttpClientHandler(); client = new HttpClient(handler); } uri = new Uri(item.Uri); if (item.UseHeader == 2) { handler.CookieContainer.SetCookies(uri, item.CustomHeader); } if (item.Method == 2) { if (item.PC_Method == 1) { email_password model = new email_password() { email = item.PC1, password = decrypt }; queryString = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"); } else if (item.PC_Method >= 3) { var query = _db.PCMethods .Where(e => e.Id == item.PC_Method.ToString()) .FirstOrDefault(); var list = query.List_PCMethodDatas; if (query.ContentType == 1) { string s = "{ "; int count = 0; foreach (var items in list) { if (count == 0) { s = s + "\"" + items.Key + "\": " + "\"" + items.Value + "\" "; } else { s = s + ",\"" + items.Key + "\": " + "\"" + items.Value + "\" "; } count = count + 1; } s = s + "}"; } else if (query.ContentType == 2) { string content = null; foreach (var items in list) { content = content + items.Value; } if (item.PC1 != null) { content = content + "&" + item.LabelPC1 + "=" + item.PC1; } if (item.PC2 != null) { content = content + "&" + item.LabelPC2 + "=" + decrypt; } queryString = new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded"); } //FOR UCSI ELE else if (query.ContentType == 3) { string content = null; foreach (var items in list) { content = content + items.Value; } if (item.PC1 != null) { content = content + "&" + item.LabelPC1 + "=" + item.PC1; } if (item.PC2 != null) { content = content + "&" + item.LabelPC2 + "=" + decrypt; } queryString = new StringContent(content, Encoding.UTF8, "multipart/form-data"); queryString.Headers.Remove("Content-Type"); queryString.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=---------------------------114782935826962"); } } response = await client.PostAsync(uri, queryString); respond = await response.Content.ReadAsStringAsync(); } else if (item.Method == 1) { response = await client.GetAsync(uri).ConfigureAwait(false); respond = await response.Content.ReadAsStringAsync(); } } StepExecutionOutputModel output = new StepExecutionOutputModel() { ResponseContent = respond, Status = "Authorized" }; return(output); } catch (UriFormatException e) { e.ToString(); return(null); } }