VirtualTask/Controllers/LoginController.cs
2025-01-31 15:12:16 +01:00

247 lines
9.0 KiB
C#

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<LoginOut>(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<LoginOut>(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
/// <summary></summary>
public static class Clienti
{
/// <summary></summary>
public const string Marrocco = "MARRO";
/// <summary></summary>
public const string Ferrari = "FERRA";
/// <summary>Zucchetti Sicilia</summary>
public const string Sicilia = "LABSE";
/// <summary>Discovery</summary>
public const string Discovery = "DISCO";
/// <summary>Sarom</summary>
public const string Sarom = "SAROM";
/// <summary>Sinergo</summary>
public const string Sinergo = "SINER";
/// <summary>Gitoga</summary>
public const string Gitoga = "GITSR";
/// <summary>Lifta</summary>
public const string Lifta = "LIFTA";
/// <summary>Siet</summary>
public const string Siet = "SIET2";
/// <summary>PMS</summary>
public const string PMS = "PMS00";
/// <summary>VT app</summary>
public const string VT = "VIRTU";
/// <summary>Lift-web</summary>
public const string LW = "DEMO";
/// <summary>Tedesco Impianti</summary>
public const string Tedesco = "TEDES";
/// <summary>Syscom</summary>
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<LoginOut>(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<LoginOut>(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");
}
}
}