Controlli unicità login tecnici

This commit is contained in:
Marco Audiffredi 2023-12-06 16:56:48 +01:00
parent a90d7ff2e8
commit e9fe869a2f
4 changed files with 104 additions and 26 deletions

View File

@ -1,7 +1,9 @@
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Text;
using System.Xml.Linq;
using VirtualTask.Models;
using X.PagedList;
using static System.Net.Mime.MediaTypeNames;
@ -104,39 +106,71 @@ namespace VirtualTask.Controllers
[HttpPost]
public IActionResult Create(Tecnici model)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
tenant = helper.GetStringValue("tenant");
if (ModelState.IsValid)
{
token = helper.GetStringValue("tok");
tenant = helper.GetStringValue("tenant");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login2", "Login");
}
model.tccodazi = tenant;
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login2", "Login");
}
//step 1 prima controllo che le credenziali di login non siano già presenti.
//per fare questo chiamo il metodo api tecniciListAll
Controllo c = isLoginPresente(model.tcuser, model.tcpwd, helper);
if (c.isPresente)
{
ModelState.AddModelError("tcuser", "Login già utilizzata. Scegliere un nome utente differente");
}
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "tecnici/add";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
if(!c.isPresente)
{
model.tccodazi = tenant;
string data = JsonConvert.SerializeObject(model);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(baseAddress, content).Result;
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "tecnici/add";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
if (response.IsSuccessStatusCode)
{
return RedirectToAction("Index");
string data = JsonConvert.SerializeObject(model);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(baseAddress, content).Result;
if (response.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("ErrMsg", errMes);
return RedirectToAction("Index");
}
}
else
{
return View("Create", model);
}
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("ErrMsg", errMes);
return RedirectToAction("Index");
//foreach (var Elemento in ModelState.Values)
//{
// foreach (var Errore in Elemento.Errors)
// {
// string ErroreRilevato = Errore.ErrorMessage;
// }
//}
//return RedirectToAction("Create");
return View("Create", model);
}
}
#endregion CREATE
@ -347,5 +381,47 @@ namespace VirtualTask.Controllers
string e = helper.GetStringValue("errMsg");
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier, ErrMsg = e });
}
public Controllo isLoginPresente(string user, string pwd, SessionHelper helper)
{
Controllo c=new Controllo();
c.isPresente = false;
urlBase = apiUrl + "tecniciListAll";
urlBase = urlBase + "?usr=" + user;
urlBase = urlBase + "&pwd=" + pwd;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Tecnici> modelList = new List<Tecnici>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Tecnici>>(data);
if(modelList!=null && modelList.Count>0)
c.isPresente=true;
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
c.errMsg = errMes;
}
return c;
}
}
public class Controllo
{
/// <summary></summary>
public string? errMsg { get; set; }
/// <summary></summary>
public bool isPresente { get; set; }
}
}

View File

@ -74,6 +74,8 @@
<form asp-action="DeleteConfirmed">
<input type="submit" value="Elimina" class="agy-btn submitForm" />
<input type="hidden" id="id" value=@Html.DisplayFor(model => model.ancodice) name="id" />
<input type="hidden" id="antipcon" value="C" name="antipcon" />
<input type="hidden" id="ancodice" value=@Html.DisplayFor(model => model.ancodice) name="ancodice" />
<div>
<a asp-action="Index">Torna alla lista</a>
</div>

View File

@ -6,8 +6,8 @@
}
},
"ApplicationInsights": {
//"rootUrlApi": "https://api.poloinformatico.it:9000/api/Polo/",
"rootUrlApi": "http://testapi.poloinformatico.it:9001/api/Polo/",
"rootUrlApi": "https://api.poloinformatico.it:9000/api/Polo/",
//"rootUrlApi": "http://testapi.poloinformatico.it:9001/api/Polo/",
"mittenteMail": "audiffredi@poloinformatico.it",
"nomeMail": "Supporto Virtual Task",
"pwdMail": "audicruz70",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 564 KiB

After

Width:  |  Height:  |  Size: 556 KiB