public async Task <IActionResult> Edit(int id, [Bind("Id,Key,Name,Description")] SaMiKeyModel saMiKeyModel) { if (id != saMiKeyModel.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var protector = protectionProvider.CreateProtector(purposes); saMiKeyModel.Key = protector.Protect(saMiKeyModel.Key); context.Update(saMiKeyModel); await context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SaMiKeyModelExists(saMiKeyModel.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(saMiKeyModel)); }
public async Task <IActionResult> Create([Bind("Key,Name,Description")] SaMiKeyModel saMiKeyModel) { if (!ModelState.IsValid) { return(View(saMiKeyModel)); } // Query SaMi for key validity var uri = configuration.GetSection("Urls").GetSection("Sensors").Value + saMiKeyModel.Key; var request = new HttpRequestMessage(HttpMethod.Get, uri); var client = clientFactory.CreateClient(); var response = await client.SendAsync(request); if (!response.IsSuccessStatusCode) { ModelState.AddModelError("Key", "Invalid key."); return(View(saMiKeyModel)); } var protector = protectionProvider.CreateProtector(purposes); saMiKeyModel.Key = protector.Protect(saMiKeyModel.Key); saMiKeyModel.ApplicationUser = await userManager.GetUserAsync(HttpContext.User); context.Add(saMiKeyModel); await context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }