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 Login2(Login model) { if (ModelState.IsValid) { helper = new SessionHelper(this); 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); string azienda = loginOut != null && !string.IsNullOrEmpty(loginOut.Tenant) ? loginOut.Tenant : string.Empty; string ten = model != null && !string.IsNullOrEmpty(model.Tenant) ? model.Tenant : string.Empty; string tok = loginOut != null && !string.IsNullOrEmpty(loginOut.Tok) ? loginOut.Tok : string.Empty; string usr = model != null && !string.IsNullOrEmpty(model.Username) ? model.Username : string.Empty; helper.SetStringValue("tok", tok); helper.SetStringValue("apiUrl", apiUrl); helper.SetStringValue("tenant", ten); helper.SetStringValue("tenant2", azienda); helper.SetStringValue("tecnico", usr); helper.SetStringValue("admin", (loginOut!=null && loginOut.Tcsuper != null) ? loginOut.Tcsuper : "N"); helper.SetStringValue("time_sheet", loginOut != null && loginOut.Config!=null && loginOut.Config.time_sheet != null && loginOut.Config.time_sheet ==true? "S" : "N"); if (!string.IsNullOrEmpty(azienda) && azienda.Equals(Clienti.Marrocco)) { string err = "Utente non abilitato all'area riservata."; helper.SetStringValue("errMsg", err); ViewBag.Error = err; return View(); } else { 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(); } } else { foreach (var Elemento in ModelState.Values) { foreach (var Errore in Elemento.Errors) { string ErroreRilevato = Errore.ErrorMessage; } } return View(); } } public IActionResult Login2() { return View(); } #region Login per download apk /// public static class Clienti { /// public const string Marrocco = "MARRO"; /// public const string Ferrari = "FERRA"; /// Zucchetti Sicilia public const string Sicilia = "LABSE"; /// Discovery public const string Discovery = "DISCO"; /// Sarom public const string Sarom = "SAROM"; /// Sinergo public const string Sinergo = "SINER"; /// Gitoga public const string Gitoga = "GITSR"; /// Lifta public const string Lifta = "LIFTA"; /// Siet public const string Siet = "SIET2"; /// PMS public const string PMS = "PMS00"; /// VT app public const string VT = "VIRTU"; /// Lift-web public const string LW = "DEMO"; /// Tedesco Impianti public const string Tedesco = "TEDES"; /// Syscom public const string Syscom = "A0001"; } public IActionResult LoginDownload() { return View(); } [HttpPost] public IActionResult LoginDownload(Login model) { if (ModelState.IsValid) { helper = new SessionHelper(this); 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); string azienda = loginOut != null && !string.IsNullOrEmpty(loginOut.Tenant) ? loginOut.Tenant : string.Empty; helper.SetStringValue("tok", loginOut.Tok); helper.SetStringValue("apiUrl", apiUrl); helper.SetStringValue("tenant", azienda); helper.SetStringValue("tenant2", loginOut.Tenant); helper.SetStringValue("tecnico", model.Username); helper.SetStringValue("admin", loginOut.Tcsuper != null ? loginOut.Tcsuper : "N"); helper.SetStringValue("time_sheet", loginOut.Config != null && loginOut.Config.time_sheet != null && loginOut.Config.time_sheet == true ? "S" : "N"); if(!string.IsNullOrEmpty(azienda)&& azienda.Equals(Clienti.Marrocco)) { helper.SetStringValue("aziendaDownload", azienda); return RedirectToAction("Index", "Download"); } else { string err = "Utente non abilitato al download."; helper.SetStringValue("errMsg", err); ViewBag.Error = err; return View(); } } else { errMes = response.Content.ReadAsStringAsync().Result; loginOut = JsonConvert.DeserializeObject(errMes); helper.SetStringValue("errMsg", loginOut.err_detail); ViewBag.Error = loginOut.err_detail; return View(); } } else { foreach (var Elemento in ModelState.Values) { foreach (var Errore in Elemento.Errors) { string ErroreRilevato = Errore.ErrorMessage; } } return View(); } } #endregion public IActionResult Logout() { helper = new SessionHelper(this); helper.ClearFormatedKey("tok"); helper.ClearFormatedKey("apiUrl"); helper.ClearFormatedKey("tenant"); helper.ClearFormatedKey("tecnico"); helper.ClearFormatedKey("admin"); return RedirectToAction("Login2", "Login"); } } }