conferma mail

This commit is contained in:
Marco Audiffredi 2023-12-01 16:42:33 +01:00
parent 3d5a6b0cc3
commit b6fb7c18f3
6 changed files with 120 additions and 11 deletions

View File

@ -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<ActionResult> 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<Registrazione> modelList = new List<Registrazione>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Registrazione>>(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. <b>{0}</b><br> Grazie per essersi registrato. Per completare la registrazione fare click al link: <a href =\"{1}\" title =\"Conferma registrazione\">Conferma</a>", nome, url) ;
//txt = "<b>titolo contenuto</b><br>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;
}
}
}

View File

@ -55,5 +55,8 @@ namespace VirtualTask.Models
[StringLength(1)]
public string? attivato { get; set;}
[StringLength(10)]
public string? token { get; set; }
}
}

View File

@ -11,6 +11,7 @@ builder.Services.AddSession(options =>
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
builder.Services.AddHttpContextAccessor();
var app = builder.Build();

View File

@ -15,6 +15,7 @@
<input type="hidden" id="id" value="0" name="id" />
<input type="hidden" id="attivato" value="N" name="attivato" />
<input type="hidden" id="token" value="-" name="token" />
<div class="row">

View File

@ -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";
}
<div class="col-lg-8 col-md-12 col-sm-12 col-12">
<div class="agy-contact-form">
<h4 class="agy-sub-heading">@ViewData["Title"]</h4>
<div class="row">Registrazione Conclusa positivamente. Il nostro staff le invierà le indicazioni per la prova gratuita.</div>
</div>
</div>

View File

@ -7,7 +7,7 @@
<div class="col-lg-8 col-md-12 col-sm-12 col-12">
<div class="agy-contact-form">
<h4 class="agy-sub-heading">@ViewData["Title"]</h4>
<div class="row">Grazie per esserti registrato</div>
<div class="row">Grazie per esserti registrato. A breve riceverà una mail per confermare la sua registrazione.</div>
</div>
</div>