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 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 = "" + @"" + @"
" + @"

" + "

TESO

" + "
" + "
" + @"

Hello " + firstname + ",


" + @"

Your verification code is
" + code + "


" + @"

" + "Enter this code in the TESO app to activate your [customer portal] account.
" + "You could also click the button below to confirm your email address:
" + @"" + @"
" + "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


" + @"

Regards,
" + "TESO TEAM

" + "
" + "
" + "" + ""; 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; } } } }