diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/UsersController.cs b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/UsersController.cs index eb26fdc..6a6cab9 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/UsersController.cs +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/UsersController.cs @@ -12,21 +12,21 @@ namespace Know_Your_Nation_Speedy.Controllers [ApiController] public class UsersController : ControllerBase { - private readonly MyDbContext _db; + + EmailService emailService = new EmailService(); + private MyDbContext _db; readonly IConfiguration _config; public UsersController(MyDbContext context, IConfiguration config) { _db = context; _config = config; } - // GET api/values [HttpGet] public async Task>> Get() { return await _db.UserEntries.ToListAsync(); } - [HttpGet("{id}")] public async Task GetEntry([FromRoute] int id) { @@ -38,7 +38,6 @@ public async Task GetEntry([FromRoute] int id) await _db.SaveChangesAsync(); return Ok(entry); } - [HttpPost("login")] public ActionResult Login([FromBody] User User) { @@ -56,14 +55,12 @@ public ActionResult Login([FromBody] User User) return BadRequest(); } } - [HttpPost] public async Task Post([FromBody] User User) { await _db.UserEntries.AddAsync(User); await _db.SaveChangesAsync(); } - [HttpPut("{id}")] public async Task Put(int id, [FromBody] User User) { @@ -71,7 +68,6 @@ public async Task Put(int id, [FromBody] User User) entry = User; await _db.SaveChangesAsync(); } - [HttpDelete("{id}")] public async Task DeleteEntry([FromRoute]int id) { @@ -84,5 +80,27 @@ public async Task DeleteEntry([FromRoute]int id) await _db.SaveChangesAsync(); return Ok(entry); } + [HttpPut()] + [Route("ForgotPassword/{mail}")] + public async Task getCodes(string mail) + { + string code = emailService.generateCode(); + var entry = await _db.UserEntries.FindAsync(mail); + + if (entry != null) + { + emailService.SendMail(mail, "testing", code); + } + } + // PUT api/values/5 + [HttpPut()] + [Route("ResetPassword/{password} + {mail}")] + public async Task ResetPassword(string mail,string password) + { + var entry = await _db.UserEntries.SingleOrDefaultAsync(m => m.Email == mail); + entry.Password = password; + _db.UserEntries.Update(entry); + await _db.SaveChangesAsync(); + } } } diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs new file mode 100644 index 0000000..2fe41fe --- /dev/null +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Net; +using System.Net.Mail; +using System.Linq; +using Know_Your_Nation_Speedy.Models; + + +namespace Know_Your_Nation_Speedy.Models{ + public class EmailService + { + string smtpAddress = "smtp.gmail.com"; + int portNumber = 587; + bool enableSSL = true; + string emailFromAddress = "**********"; //Sender Email Address + string password = "***********"; //Sender Password + + public bool SendMail(string To, string Subject) + { + try + { + using (MailMessage mail = new MailMessage()) + { + MailAssignment(mail, emailFromAddress, To, Subject, "Reset Password"); + SmtpSend(mail); + } + } + catch(Exception e) + { + Console.WriteLine(e.Message); + return false; + } + return true; + } + + public void MailAssignment(MailMessage mailMessage, string From, string To, string Subject, string Body) + { + mailMessage.From = new MailAddress(From); + mailMessage.To.Add(To); + mailMessage.Subject = Subject; + mailMessage.IsBodyHtml = true; + mailMessage.Body = Body; + } + + public void SmtpSend(MailMessage mail) + { + SmtpClient smtp = new SmtpClient(smtpAddress, portNumber); + smtp.Credentials = new NetworkCredential(emailFromAddress, password); + smtp.EnableSsl = enableSSL; + smtp.Send(mail); + } + } +} diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy.csproj b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy.csproj index fe51a5a..a8e475a 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy.csproj +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy.csproj @@ -10,6 +10,7 @@ + diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/appsettings.json b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/appsettings.json index c800919..6750b9d 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/appsettings.json +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/appsettings.json @@ -4,7 +4,7 @@ "Default": "Warning" } }, - "ConnectionStrings": { - "connection": "Server=dev.retrotest.co.za;Database=ereader;User Id=group4;Password=3bHNuE8&rvG+99U2;" - } -} \ No newline at end of file + "ConnectionStrings": { + "connection": "Server=dev.retrotest.co.za;Database=ereader;User Id=group4;Password=*********;" + } +}