Backend for the Teso project written in 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
5.2 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Teso_API.Models;
namespace Teso_API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ActivationGeneratorController : ControllerBase
{
private readonly TESOContext _context;
private readonly IWebHostEnvironment webHostEnvironemt;
public ActivationGeneratorController(TESOContext context, IWebHostEnvironment _webHostEnvironment)
{
_context = context;
this.webHostEnvironemt = _webHostEnvironment;
}
// POST: api/ActivationGenerator
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult> PostActivationCodes(TesoUserDetail user)
{
user.UserGUID = await _context.UserAuths.AsQueryable().Where(a => a.Username == user.Username).Select(p => p.UserGUID).FirstOrDefaultAsync();
ActivationCodes activation = new ActivationCodes();
activation.UserGuid = user.UserGUID;
activation.CodeGuid = Guid.NewGuid().ToString();
activation.DateGenerated = DateTime.Now;
activation.Code = int.Parse(String.Format("{0:d6}", (DateTime.Now.Ticks / 10) % 100000));
_context.ActivationCodes.Add(activation);
try
{
int result = activationCode(user, activation.Code, activation.CodeGuid);
if (result == 1)
{
await _context.SaveChangesAsync();
}
else
{
return BadRequest();
}
}
catch
{
return Conflict();
}
return Ok();
}
private AlternateView GetEmbeddedImage(int code, string firstname, string guid)
{
string filePath = Path.Combine(this.webHostEnvironemt.ContentRootPath, "teso.png");
LinkedResource res = new LinkedResource(filePath, MediaTypeNames.Image.Jpeg);
res.ContentId = Guid.NewGuid().ToString();
string htmlBody = "<html>" +
@"<body style=""padding:50px;"">" +
@"<div style=""padding:20px;"">" +
@"<center><img src = 'cid:" + res.ContentId + @"' /> <br>" +
"<b><h1> TESO </h1><b>" +
"<hr/>" +
"</center>" +
@"<p style=""font-size:18px; color:#003445;""> Hello " + firstname + ",</p><br/>" +
@"<center> <h1 style=""font-size:24px; color:#003445;font-weight:bold;""> <b> Your verification code is <br/> " + code + "<b> </h1> <br/>" +
@"<p style=""font-size:18px; color:#003445;"">" +
"Enter this code in the TESO app to activate your [customer portal] account. <br/>" +
"You could also click the button below to confirm your email address: <br/>" +
@"<a href='" + ServerLocation.location + "api/activationhandler/" + guid + "'>" +
@"<button style=""height: 60px; color: white; font-size:18px; background-color:#fd0a35;font-weight:bold;padding:10px;cursor:pointer;"">Activate Account</button></a> <br/>" +
"If it wasn't you, someone must have mistakenly typed in your email. Keep this code to yourself; no other action is needed at this moment </p> <br/>" +
@"<h2 style=""font-size:18px; color:#003445;"">Regards, <br/>" +
"<b>TESO TEAM <b></h2>" +
"</center>" +
"</div>" +
"</body>" +
"</html>";
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(res);
return alternateView;
}
private int activationCode(TesoUserDetail tesoUser, int Code, string verificationCode)
{
try
{
MailMessage mail = new MailMessage();
string client = "mail.privateemail.com";
string clientPort = "587";
string username = "support@tesoapp.com";
string password = "Konstantinovich96";
SmtpClient smtpClient = new SmtpClient(client);
mail.From = new MailAddress(username);
mail.To.Add(tesoUser.Email);
mail.Subject = "Verification Code";
mail.AlternateViews.Add(GetEmbeddedImage(Code, tesoUser.Username, verificationCode));
mail.IsBodyHtml = true;
smtpClient.Port = int.Parse(clientPort);
smtpClient.Credentials = new System.Net.NetworkCredential(username, password);
smtpClient.EnableSsl = true;
smtpClient.Timeout = 1000000;
smtpClient.Send(mail);
return 1;
}
catch
{
return 0;
}
}
}
}