102 lines
3.2 KiB
C#
102 lines
3.2 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using NuGet.Common;
|
|
using SoftwayWeb.Models;
|
|
using System.Text;
|
|
|
|
namespace SoftwayWeb.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)
|
|
{
|
|
_configuration = configuration;
|
|
client = new HttpClient();
|
|
var key = _configuration["ApplicationInsights:rootUrlApi"];
|
|
apiUrl = key;
|
|
}
|
|
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
public IActionResult Login()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult Login(Login model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
helper.ClearFormatedKey("errMsg");
|
|
string url = apiUrl + "Login/loginMagazzino";
|
|
Uri baseAddress = new Uri(url);
|
|
client.BaseAddress = baseAddress;
|
|
//ViewBag.Error = string.Empty;
|
|
//ViewBag.Admin = string.Empty;
|
|
|
|
Login_Out loginOut = new Login_Out();
|
|
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<Login_Out>(risultato);
|
|
|
|
helper.SetStringValue("tok", loginOut.Tok);
|
|
helper.SetStringValue("apiUrl", apiUrl);
|
|
|
|
return RedirectToAction("Index", "Giri"); // 25/07/2024 dalla login vado sulla pagina con l'elenco dei giri senza passare per "Portale"
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
loginOut = JsonConvert.DeserializeObject<Login_Out>(errMes);
|
|
|
|
helper.SetStringValue("errMsg", loginOut.err_detail);
|
|
ViewBag.Error = loginOut.err_detail;
|
|
return View();
|
|
}
|
|
//return View(model);
|
|
}
|
|
else
|
|
{
|
|
foreach (var Elemento in ModelState.Values)
|
|
{
|
|
foreach (var Errore in Elemento.Errors)
|
|
{
|
|
string ErroreRilevato = Errore.ErrorMessage;
|
|
}
|
|
|
|
}
|
|
return View();
|
|
}
|
|
}
|
|
|
|
#region Logout
|
|
|
|
public IActionResult Logout()
|
|
{
|
|
helper = new SessionHelper(this);
|
|
helper.ClearFormatedKey("tok");
|
|
helper.ClearFormatedKey("errMsg");
|
|
return RedirectToAction("Login","Login");
|
|
}
|
|
|
|
|
|
#endregion Logout
|
|
}
|
|
}
|