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) { //apiUrl = "http://10.0.0.187:8000/api/Polo/"; //apiUrl = "http://testapi.poloinformatico.it:9001/api/Polo/"; client = new HttpClient(); helper = new SessionHelper(this); _configuration = configuration; var key = _configuration["ApplicationInsights:rootUrlApi"]; apiUrl = key; } 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(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(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(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(errMes); // helper.SetStringValue("errMsg", loginOut.err_detail); // ViewBag.Error = loginOut.err_detail; // return View(); // } //} } }