From b6fb7c18f3efe9f85459215915576549bc7ad4ec Mon Sep 17 00:00:00 2001 From: Marco Audiffredi Date: Fri, 1 Dec 2023 16:42:33 +0100 Subject: [PATCH] conferma mail --- Controllers/RegistrazioniController.cs | 111 ++++++++++++++++-- Models/Registrazione.cs | 3 + Program.cs | 1 + Views/Registrazioni/Create.cshtml | 1 + .../RegistrazioneFinished.cshtml | 13 ++ Views/Registrazioni/RegistrazioneOk.cshtml | 2 +- 6 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 Views/Registrazioni/RegistrazioneFinished.cshtml diff --git a/Controllers/RegistrazioniController.cs b/Controllers/RegistrazioniController.cs index a791a16..1a6830a 100644 --- a/Controllers/RegistrazioniController.cs +++ b/Controllers/RegistrazioniController.cs @@ -8,6 +8,13 @@ using System.Reflection; using System.Text; using VirtualTask.Models; using X.PagedList; +using Humanizer; +using System.Numerics; +using System.Reflection.Metadata; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc.RazorPages; +using NuGet.Common; namespace VirtualTask.Controllers { @@ -19,19 +26,23 @@ namespace VirtualTask.Controllers string tenant = string.Empty; string errMes = string.Empty; string admin = string.Empty; + string urlConfirm = string.Empty; HttpClient client; private readonly IConfiguration _configuration; + private readonly IHttpContextAccessor _context; - public RegistrazioniController(IConfiguration configuration) + public RegistrazioniController(IConfiguration configuration, IHttpContextAccessor context) { client = new HttpClient(); _configuration = configuration; var key = _configuration["ApplicationInsights:rootUrlApi"]; apiUrl = key; - + _context = context; + var request = _context.HttpContext.Request; + urlConfirm = string.Format("{0}://{1}", request.Scheme, request.Host.Value); } #region INDEX @@ -103,6 +114,8 @@ namespace VirtualTask.Controllers if (ModelState.IsValid) { + string tokenMail = RandomString(10); + model.token = tokenMail; bool bAziPres = false; bAziPres = checkAziendaPresente(model.azienda); if (bAziPres) @@ -117,7 +130,6 @@ namespace VirtualTask.Controllers if (!bAziPres && bEmail) { - urlBase = apiUrl + "registrazioni/add"; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); @@ -130,7 +142,7 @@ namespace VirtualTask.Controllers if (response.IsSuccessStatusCode) { //mando mail avvenuta richiesta - bool esito = MailSent(model.email); + bool esito = MailSent(model.email,model.cognome,model.token); return RedirectToAction("RegistrazioneOk"); } else @@ -161,11 +173,48 @@ namespace VirtualTask.Controllers } - public IActionResult RegistrazioneOk() { return View(); } + + public async Task ConfirmEmail(string Token, string Email) + { + SessionHelper helper = new SessionHelper(this); + urlBase = apiUrl + "RegistrazioniList"; + admin = helper.GetStringValue("admin"); + ViewBag.Admin = admin; + + Uri baseAddress = new Uri(urlBase); + client = new HttpClient(); + client.BaseAddress = baseAddress; + + List modelList = new List(); + HttpResponseMessage response = client.GetAsync(baseAddress).Result; + + if (response.IsSuccessStatusCode) + { + string data = response.Content.ReadAsStringAsync().Result; + modelList = JsonConvert.DeserializeObject>(data); + + var reg=modelList.Where(t=>t.email.Equals(Email)&& t.token.Equals(Token)).ToList(); + var trovato = reg.First(); + + UpdRegistrazione(trovato); + return RedirectToAction("RegistrazioneFinished"); + } + else + { + errMes = response.Content.ReadAsStringAsync().Result; + helper.SetStringValue("errMsg", errMes); + return RedirectToAction("Error"); + } + } + + public IActionResult RegistrazioneFinished() + { + return View(); + } #endregion CREATE #region DETAILS @@ -390,19 +439,20 @@ namespace VirtualTask.Controllers } return bAziPres; } - private bool MailSent(string receiver) + private bool MailSent(string receiver, string nome, string tokenMail) { //REMEMBER per una mail gmail bisogna andare nelle impostazioni //e impostare "Accesso app meno sicure" a ON bool sent = false; + string senderMail = _configuration["ApplicationInsights:mittenteMail"]; string senderName = _configuration["ApplicationInsights:nomeMail"]; string pwdMail = _configuration["ApplicationInsights:pwdMail"]; string subject = _configuration["ApplicationInsights:subjectMail"]; - string message = getMailText(); + string message = getMailText(nome, tokenMail, receiver); var senderEmail = new MailAddress(senderMail, senderName); var receiverEmail = new MailAddress(receiver, "Receiver"); @@ -422,7 +472,8 @@ namespace VirtualTask.Controllers using (var mess = new MailMessage(senderEmail, receiverEmail) { Subject = subject, - Body = body + Body = body, + IsBodyHtml = true }) { smtp.Send(mess); @@ -430,13 +481,53 @@ namespace VirtualTask.Controllers return sent; } - private string getMailText() + private string getMailText(string nome,string tokenEmail, string emailReg) { string txt = string.Empty; - txt = "contenuto mail di registrazione"; + StringBuilder sb=new StringBuilder(); + sb.Append(urlConfirm); + sb.Append("/Registrazioni/ConfirmEmail"); + //sb.Append("https://localhost:7140/Registrazioni/ConfirmEmail"); + sb.Append("?Token="); + sb.Append(tokenEmail); + sb.Append("&Email="); + sb.Append(emailReg); + + string url = sb.ToString(); + + txt = string.Format("Gentile sig. {0}
Grazie per essersi registrato. Per completare la registrazione fare click al link: Conferma", nome, url) ; + //txt = "titolo contenuto
contenuto mail di registrazione"; return txt; } + private static Random random = new Random(); + + public static string RandomString(int length) + { + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + return new string(Enumerable.Repeat(chars, length) + .Select(s => s[random.Next(s.Length)]).ToArray()); + } + + public bool UpdRegistrazione(Registrazione model) + { + bool upd=false; + model.attivato = "S"; + urlBase = apiUrl + "registrazioni/mod"; + Uri baseAddress = new Uri(urlBase); + client = new HttpClient(); + client.BaseAddress = baseAddress; + + string data = JsonConvert.SerializeObject(model); + StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); + HttpResponseMessage response = client.PostAsync(baseAddress, content).Result; + + if (response.IsSuccessStatusCode) + { + upd = true; + } + return upd; + } } } diff --git a/Models/Registrazione.cs b/Models/Registrazione.cs index 4cf3fed..a131e7e 100644 --- a/Models/Registrazione.cs +++ b/Models/Registrazione.cs @@ -55,5 +55,8 @@ namespace VirtualTask.Models [StringLength(1)] public string? attivato { get; set;} + + [StringLength(10)] + public string? token { get; set; } } } diff --git a/Program.cs b/Program.cs index a543536..a0b75ba 100644 --- a/Program.cs +++ b/Program.cs @@ -11,6 +11,7 @@ builder.Services.AddSession(options => options.Cookie.HttpOnly = true; options.Cookie.IsEssential = true; }); +builder.Services.AddHttpContextAccessor(); var app = builder.Build(); diff --git a/Views/Registrazioni/Create.cshtml b/Views/Registrazioni/Create.cshtml index 80d5276..aeee2bd 100644 --- a/Views/Registrazioni/Create.cshtml +++ b/Views/Registrazioni/Create.cshtml @@ -15,6 +15,7 @@ +
diff --git a/Views/Registrazioni/RegistrazioneFinished.cshtml b/Views/Registrazioni/RegistrazioneFinished.cshtml new file mode 100644 index 0000000..a3c264d --- /dev/null +++ b/Views/Registrazioni/RegistrazioneFinished.cshtml @@ -0,0 +1,13 @@ +@* + For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 +*@ +@{ + ViewData["Title"] = "Registrazione Conclusa"; + Layout = "~/Views/Shared/_Layout2.cshtml"; +} +
+
+

@ViewData["Title"]

+
Registrazione Conclusa positivamente. Il nostro staff le invierà le indicazioni per la prova gratuita.
+
+
\ No newline at end of file diff --git a/Views/Registrazioni/RegistrazioneOk.cshtml b/Views/Registrazioni/RegistrazioneOk.cshtml index 16bff1e..97d0ed9 100644 --- a/Views/Registrazioni/RegistrazioneOk.cshtml +++ b/Views/Registrazioni/RegistrazioneOk.cshtml @@ -7,7 +7,7 @@

@ViewData["Title"]

-
Grazie per esserti registrato
+
Grazie per esserti registrato. A breve riceverà una mail per confermare la sua registrazione.