conferma mail
This commit is contained in:
parent
3d5a6b0cc3
commit
b6fb7c18f3
@ -8,6 +8,13 @@ using System.Reflection;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using VirtualTask.Models;
|
using VirtualTask.Models;
|
||||||
using X.PagedList;
|
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
|
namespace VirtualTask.Controllers
|
||||||
{
|
{
|
||||||
@ -19,19 +26,23 @@ namespace VirtualTask.Controllers
|
|||||||
string tenant = string.Empty;
|
string tenant = string.Empty;
|
||||||
string errMes = string.Empty;
|
string errMes = string.Empty;
|
||||||
string admin = string.Empty;
|
string admin = string.Empty;
|
||||||
|
string urlConfirm = string.Empty;
|
||||||
|
|
||||||
|
|
||||||
HttpClient client;
|
HttpClient client;
|
||||||
private readonly IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
|
private readonly IHttpContextAccessor _context;
|
||||||
|
|
||||||
public RegistrazioniController(IConfiguration configuration)
|
public RegistrazioniController(IConfiguration configuration, IHttpContextAccessor context)
|
||||||
{
|
{
|
||||||
|
|
||||||
client = new HttpClient();
|
client = new HttpClient();
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
var key = _configuration["ApplicationInsights:rootUrlApi"];
|
var key = _configuration["ApplicationInsights:rootUrlApi"];
|
||||||
apiUrl = key;
|
apiUrl = key;
|
||||||
|
_context = context;
|
||||||
|
var request = _context.HttpContext.Request;
|
||||||
|
urlConfirm = string.Format("{0}://{1}", request.Scheme, request.Host.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region INDEX
|
#region INDEX
|
||||||
@ -103,6 +114,8 @@ namespace VirtualTask.Controllers
|
|||||||
|
|
||||||
if (ModelState.IsValid)
|
if (ModelState.IsValid)
|
||||||
{
|
{
|
||||||
|
string tokenMail = RandomString(10);
|
||||||
|
model.token = tokenMail;
|
||||||
bool bAziPres = false;
|
bool bAziPres = false;
|
||||||
bAziPres = checkAziendaPresente(model.azienda);
|
bAziPres = checkAziendaPresente(model.azienda);
|
||||||
if (bAziPres)
|
if (bAziPres)
|
||||||
@ -117,7 +130,6 @@ namespace VirtualTask.Controllers
|
|||||||
|
|
||||||
if (!bAziPres && bEmail)
|
if (!bAziPres && bEmail)
|
||||||
{
|
{
|
||||||
|
|
||||||
urlBase = apiUrl + "registrazioni/add";
|
urlBase = apiUrl + "registrazioni/add";
|
||||||
Uri baseAddress = new Uri(urlBase);
|
Uri baseAddress = new Uri(urlBase);
|
||||||
client = new HttpClient();
|
client = new HttpClient();
|
||||||
@ -130,7 +142,7 @@ namespace VirtualTask.Controllers
|
|||||||
if (response.IsSuccessStatusCode)
|
if (response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
//mando mail avvenuta richiesta
|
//mando mail avvenuta richiesta
|
||||||
bool esito = MailSent(model.email);
|
bool esito = MailSent(model.email,model.cognome,model.token);
|
||||||
return RedirectToAction("RegistrazioneOk");
|
return RedirectToAction("RegistrazioneOk");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -161,11 +173,48 @@ namespace VirtualTask.Controllers
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public IActionResult RegistrazioneOk()
|
public IActionResult RegistrazioneOk()
|
||||||
{
|
{
|
||||||
return View();
|
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
|
#endregion CREATE
|
||||||
|
|
||||||
#region DETAILS
|
#region DETAILS
|
||||||
@ -390,19 +439,20 @@ namespace VirtualTask.Controllers
|
|||||||
}
|
}
|
||||||
return bAziPres;
|
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
|
//REMEMBER per una mail gmail bisogna andare nelle impostazioni
|
||||||
//e impostare "Accesso app meno sicure" a ON
|
//e impostare "Accesso app meno sicure" a ON
|
||||||
bool sent = false;
|
bool sent = false;
|
||||||
|
|
||||||
|
|
||||||
string senderMail = _configuration["ApplicationInsights:mittenteMail"];
|
string senderMail = _configuration["ApplicationInsights:mittenteMail"];
|
||||||
string senderName = _configuration["ApplicationInsights:nomeMail"];
|
string senderName = _configuration["ApplicationInsights:nomeMail"];
|
||||||
string pwdMail = _configuration["ApplicationInsights:pwdMail"];
|
string pwdMail = _configuration["ApplicationInsights:pwdMail"];
|
||||||
|
|
||||||
|
|
||||||
string subject = _configuration["ApplicationInsights:subjectMail"];
|
string subject = _configuration["ApplicationInsights:subjectMail"];
|
||||||
string message = getMailText();
|
string message = getMailText(nome, tokenMail, receiver);
|
||||||
|
|
||||||
var senderEmail = new MailAddress(senderMail, senderName);
|
var senderEmail = new MailAddress(senderMail, senderName);
|
||||||
var receiverEmail = new MailAddress(receiver, "Receiver");
|
var receiverEmail = new MailAddress(receiver, "Receiver");
|
||||||
@ -422,7 +472,8 @@ namespace VirtualTask.Controllers
|
|||||||
using (var mess = new MailMessage(senderEmail, receiverEmail)
|
using (var mess = new MailMessage(senderEmail, receiverEmail)
|
||||||
{
|
{
|
||||||
Subject = subject,
|
Subject = subject,
|
||||||
Body = body
|
Body = body,
|
||||||
|
IsBodyHtml = true
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
smtp.Send(mess);
|
smtp.Send(mess);
|
||||||
@ -430,13 +481,53 @@ namespace VirtualTask.Controllers
|
|||||||
return sent;
|
return sent;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string getMailText()
|
private string getMailText(string nome,string tokenEmail, string emailReg)
|
||||||
{
|
{
|
||||||
string txt = string.Empty;
|
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;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -55,5 +55,8 @@ namespace VirtualTask.Models
|
|||||||
|
|
||||||
[StringLength(1)]
|
[StringLength(1)]
|
||||||
public string? attivato { get; set;}
|
public string? attivato { get; set;}
|
||||||
|
|
||||||
|
[StringLength(10)]
|
||||||
|
public string? token { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,7 @@ builder.Services.AddSession(options =>
|
|||||||
options.Cookie.HttpOnly = true;
|
options.Cookie.HttpOnly = true;
|
||||||
options.Cookie.IsEssential = true;
|
options.Cookie.IsEssential = true;
|
||||||
});
|
});
|
||||||
|
builder.Services.AddHttpContextAccessor();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
<input type="hidden" id="id" value="0" name="id" />
|
<input type="hidden" id="id" value="0" name="id" />
|
||||||
<input type="hidden" id="attivato" value="N" name="attivato" />
|
<input type="hidden" id="attivato" value="N" name="attivato" />
|
||||||
|
<input type="hidden" id="token" value="-" name="token" />
|
||||||
|
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
|
|||||||
13
Views/Registrazioni/RegistrazioneFinished.cshtml
Normal file
13
Views/Registrazioni/RegistrazioneFinished.cshtml
Normal 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>
|
||||||
@ -7,7 +7,7 @@
|
|||||||
<div class="col-lg-8 col-md-12 col-sm-12 col-12">
|
<div class="col-lg-8 col-md-12 col-sm-12 col-12">
|
||||||
<div class="agy-contact-form">
|
<div class="agy-contact-form">
|
||||||
<h4 class="agy-sub-heading">@ViewData["Title"]</h4>
|
<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>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user