107 lines
3.6 KiB
C#
107 lines
3.6 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;
|
|
public LoginController()
|
|
{
|
|
apiUrl = "http://10.0.0.187:8000/api/Polo/";
|
|
client = new HttpClient();
|
|
helper = new SessionHelper(this);
|
|
}
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
public IActionResult Login()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
public IActionResult Login(Login model)
|
|
{
|
|
string url = apiUrl + "loginTechnicalVT";
|
|
Uri baseAddress = new Uri(url);
|
|
client.BaseAddress = baseAddress;
|
|
ViewBag.Error =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);
|
|
|
|
helper.SetStringValue("tok", loginOut.Tok);
|
|
helper.SetStringValue("apiUrl", apiUrl);
|
|
helper.SetStringValue("tenant", model.Tenant);
|
|
helper.SetStringValue("tecnico", model.Username);
|
|
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();
|
|
}
|
|
}
|
|
|
|
|
|
public IActionResult Login2()
|
|
{
|
|
return View();
|
|
}
|
|
[HttpPost]
|
|
public IActionResult Login2(Login model)
|
|
{
|
|
string url = apiUrl + "loginTechnicalVT";
|
|
Uri baseAddress = new Uri(url);
|
|
client.BaseAddress = baseAddress;
|
|
ViewBag.Error = 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);
|
|
|
|
helper.SetStringValue("tok", loginOut.Tok);
|
|
helper.SetStringValue("apiUrl", apiUrl);
|
|
helper.SetStringValue("tenant", model.Tenant);
|
|
helper.SetStringValue("tecnico", model.Username);
|
|
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();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|