using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using System.Diagnostics; using System.Text; using VirtualTask.Models; namespace VirtualTask.Controllers { public class LoginController : Controller { string apiUrl=string.Empty; HttpClient client; SessionHelper helper; string errMes=string.Empty; private readonly IConfiguration _configuration; public LoginController(IConfiguration configuration) { client = new HttpClient(); helper = new SessionHelper(this); _configuration = configuration; var key = _configuration["ApplicationInsights:rootUrlApi"]; apiUrl = key; } public IActionResult Index() { return View(); } [HttpPost] public IActionResult Login(Login model) { string url = apiUrl + "loginTechnicalVT"; Uri baseAddress = new Uri(url); client.BaseAddress = baseAddress; ViewBag.Error =string.Empty; ViewBag.Admin = string.Empty; LoginOut loginOut = new LoginOut(); string data = JsonConvert.SerializeObject(model); StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); HttpResponseMessage response = client.PostAsync(baseAddress, content).Result; if (response.IsSuccessStatusCode) { string risultato = response.Content.ReadAsStringAsync().Result; loginOut = JsonConvert.DeserializeObject(risultato); helper.SetStringValue("tok", loginOut.Tok); helper.SetStringValue("apiUrl", apiUrl); helper.SetStringValue("tenant", model.Tenant); helper.SetStringValue("tecnico", model.Username); helper.SetStringValue("admin", loginOut.Tcsuper != null ? loginOut.Tcsuper : "N"); return RedirectToAction("Index","Home"); } else { errMes = response.Content.ReadAsStringAsync().Result; loginOut = JsonConvert.DeserializeObject(errMes); helper.SetStringValue("errMsg", loginOut.err_detail); ViewBag.Error = loginOut.err_detail; return View(); } } public IActionResult Login2() { return View(); } } }