From ef0afb72458a97ea01911f1377e9547696cf65de Mon Sep 17 00:00:00 2001 From: kyle-manganyi Date: Thu, 14 Feb 2019 17:13:04 +0200 Subject: [PATCH 1/7] forgot password --- .../Controllers/RattingsController.cs | 16 +++++ .../Controllers/UsersController.cs | 30 ++++++++- .../Know-Your-Nation-Speedy/EmailService.cs | 66 +++++++++++++++++++ .../Know-Your-Nation-Speedy.csproj | 1 + 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/RattingsController.cs create mode 100644 Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/RattingsController.cs b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/RattingsController.cs new file mode 100644 index 0000000..d7663f0 --- /dev/null +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/RattingsController.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace Know_Your_Nation_Speedy.Controllers +{ + public class RattingsController : Controller + { + public IActionResult Index() + { + return View(); + } + } +} \ No newline at end of file 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 a89afd2..67da8b4 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,14 +12,16 @@ namespace Know_Your_Nation_Speedy.Controllers [ApiController] public class UsersController : ControllerBase { - private readonly MyDbContext _db; + + private Users _model; + EmailService mailS = 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() @@ -84,5 +86,29 @@ public async Task DeleteEntry([FromRoute]int id) await _db.SaveChangesAsync(); return Ok(entry); } + + [HttpPut()] + [Route("ForgotPassword/{mail}")] + public ActionResult getCodes(string mail) + { + string code = mailS.generateCode(); + + mailS.SendMail(mail, "testing", code); + _db.SaveChanges(); + return Ok(mail); + } + + // PUT api/values/5 + [HttpPut()] + [Route("ResetPassword/{password} + {mail}")] + public async Task ResetPassword(string mail,string password) + { + var entry = await _db.UsersEntries.SingleOrDefaultAsync(m => m.Email == mail); + entry.Password = password; + _db.UsersEntries.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..d0bc283 --- /dev/null +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs @@ -0,0 +1,66 @@ +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 = "manganyikyle@gmail.com"; //Sender Email Address + string password = "tiyisela97"; //Sender Password + + + + public bool SendMail(string To, string Subject, string Body) + { + 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); + } + + public string generateCode() + { + + const string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + Random rand = new Random(); + + string code = new string(Enumerable.Repeat(characters, 10).Select(s => s[rand.Next(s.Length)]).ToArray()); + return code; + } + } +} 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 @@ + From 9124e9a3b42fe0cb75280b14284094563720e33d Mon Sep 17 00:00:00 2001 From: kyle-manganyi Date: Thu, 14 Feb 2019 19:12:45 +0200 Subject: [PATCH 2/7] forgot password --- .../Controllers/UsersController.cs | 18 +++++++++++------- .../Know-Your-Nation-Speedy/EmailService.cs | 7 +++---- .../Know-Your-Nation-Speedy/appsettings.json | 6 +++--- 3 files changed, 17 insertions(+), 14 deletions(-) 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 eef4731..b617fe2 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 @@ -13,7 +13,7 @@ namespace Know_Your_Nation_Speedy.Controllers public class UsersController : ControllerBase { - private Users _model; + private User _model; EmailService mailS = new EmailService(); private MyDbContext _db; readonly IConfiguration _config; @@ -89,13 +89,17 @@ public async Task DeleteEntry([FromRoute]int id) [HttpPut()] [Route("ForgotPassword/{mail}")] - public ActionResult getCodes(string mail) + public async Task getCodes(string mail) { string code = mailS.generateCode(); + var entry = await _db.UserEntries.FindAsync(mail); - mailS.SendMail(mail, "testing", code); - _db.SaveChanges(); - return Ok(mail); + if (entry != null) + { + mailS.SendMail(mail, "testing", code); + _db.SaveChanges(); + + } } // PUT api/values/5 @@ -103,9 +107,9 @@ public ActionResult getCodes(string mail) [Route("ResetPassword/{password} + {mail}")] public async Task ResetPassword(string mail,string password) { - var entry = await _db.UsersEntries.SingleOrDefaultAsync(m => m.Email == mail); + var entry = await _db.UserEntries.SingleOrDefaultAsync(m => m.Email == mail); entry.Password = password; - _db.UsersEntries.Update(entry); + _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 index d0bc283..8b55245 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs @@ -13,9 +13,8 @@ public class EmailService string smtpAddress = "smtp.gmail.com"; int portNumber = 587; bool enableSSL = true; - string emailFromAddress = "manganyikyle@gmail.com"; //Sender Email Address - string password = "tiyisela97"; //Sender Password - + string emailFromAddress = "**********"; //Sender Email Address + string password = "***********"; //Sender Password public bool SendMail(string To, string Subject, string Body) @@ -24,7 +23,7 @@ public bool SendMail(string To, string Subject, string Body) { using (MailMessage mail = new MailMessage()) { - MailAssignment(mail, emailFromAddress, To, Subject, "Reset Password"); + MailAssignment(mail, emailFromAddress, To, Subject, "Reset Password"); SmtpSend(mail); } } 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..cec7f5d 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;" - } + "ConnectionStrings": { + "connection": "Server=dev.retrotest.co.za;Database=ereader;User Id=group4;Password=P,A+m8JDHx{!L4_#;" + } } \ No newline at end of file From 89107e77d0f82066be528c5e7c087b44d428e8d5 Mon Sep 17 00:00:00 2001 From: kyle-manganyi <46721650+kyle-manganyi@users.noreply.github.com> Date: Thu, 14 Feb 2019 19:21:30 +0200 Subject: [PATCH 3/7] Update appsettings.json --- .../Know-Your-Nation-Speedy/appsettings.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 cec7f5d..6750b9d 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/appsettings.json +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/appsettings.json @@ -5,6 +5,6 @@ } }, "ConnectionStrings": { - "connection": "Server=dev.retrotest.co.za;Database=ereader;User Id=group4;Password=P,A+m8JDHx{!L4_#;" + "connection": "Server=dev.retrotest.co.za;Database=ereader;User Id=group4;Password=*********;" } -} \ No newline at end of file +} From bb7e5c304ce96efae908e82bccb1e9df8c191033 Mon Sep 17 00:00:00 2001 From: kyle-manganyi Date: Fri, 15 Feb 2019 10:06:40 +0200 Subject: [PATCH 4/7] reset password with minor errors fixed --- .../Know-Your-Nation-Speedy/Controllers/UsersController.cs | 7 +++---- .../Know-Your-Nation-Speedy/EmailService.cs | 3 +-- 2 files changed, 4 insertions(+), 6 deletions(-) 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 b617fe2..c137725 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 @@ -13,8 +13,7 @@ namespace Know_Your_Nation_Speedy.Controllers public class UsersController : ControllerBase { - private User _model; - EmailService mailS = new EmailService(); + EmailService emailService = new EmailService(); private MyDbContext _db; readonly IConfiguration _config; public UsersController(MyDbContext context, IConfiguration config) @@ -91,12 +90,12 @@ public async Task DeleteEntry([FromRoute]int id) [Route("ForgotPassword/{mail}")] public async Task getCodes(string mail) { - string code = mailS.generateCode(); + string code = emailService.generateCode(); var entry = await _db.UserEntries.FindAsync(mail); if (entry != null) { - mailS.SendMail(mail, "testing", code); + emailService.SendMail(mail, "testing", code); _db.SaveChanges(); } diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs index 8b55245..a69d79b 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs @@ -14,8 +14,7 @@ public class EmailService int portNumber = 587; bool enableSSL = true; string emailFromAddress = "**********"; //Sender Email Address - string password = "***********"; //Sender Password - + string password = "***********"; //Sender Password public bool SendMail(string To, string Subject, string Body) { From ebf8621791871a4f23b6ca1bb671c2b2e8d0b11d Mon Sep 17 00:00:00 2001 From: kyle-manganyi <46721650+kyle-manganyi@users.noreply.github.com> Date: Sun, 17 Feb 2019 18:42:40 +0200 Subject: [PATCH 5/7] Delete RattingsController.cs --- .../Controllers/RattingsController.cs | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/RattingsController.cs diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/RattingsController.cs b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/RattingsController.cs deleted file mode 100644 index d7663f0..0000000 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/RattingsController.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; - -namespace Know_Your_Nation_Speedy.Controllers -{ - public class RattingsController : Controller - { - public IActionResult Index() - { - return View(); - } - } -} \ No newline at end of file From 33e6d796ab1da558df7c0de18b3803ce559794c7 Mon Sep 17 00:00:00 2001 From: kyle-manganyi <46721650+kyle-manganyi@users.noreply.github.com> Date: Sun, 17 Feb 2019 18:43:43 +0200 Subject: [PATCH 6/7] Update UsersController.cs --- .../Controllers/UsersController.cs | 11 ----------- 1 file changed, 11 deletions(-) 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 c137725..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 @@ -27,7 +27,6 @@ public async Task>> Get() { return await _db.UserEntries.ToListAsync(); } - [HttpGet("{id}")] public async Task GetEntry([FromRoute] int id) { @@ -39,7 +38,6 @@ public async Task GetEntry([FromRoute] int id) await _db.SaveChangesAsync(); return Ok(entry); } - [HttpPost("login")] public ActionResult Login([FromBody] User User) { @@ -57,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) { @@ -72,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) { @@ -85,7 +80,6 @@ public async Task DeleteEntry([FromRoute]int id) await _db.SaveChangesAsync(); return Ok(entry); } - [HttpPut()] [Route("ForgotPassword/{mail}")] public async Task getCodes(string mail) @@ -96,11 +90,8 @@ public async Task getCodes(string mail) if (entry != null) { emailService.SendMail(mail, "testing", code); - _db.SaveChanges(); - } } - // PUT api/values/5 [HttpPut()] [Route("ResetPassword/{password} + {mail}")] @@ -111,7 +102,5 @@ public async Task ResetPassword(string mail,string password) _db.UserEntries.Update(entry); await _db.SaveChangesAsync(); } - - } } From 4b5b126dd5b7db88eaf85e122f8dd4142a23cc0d Mon Sep 17 00:00:00 2001 From: kyle-manganyi <46721650+kyle-manganyi@users.noreply.github.com> Date: Mon, 18 Feb 2019 09:00:14 +0200 Subject: [PATCH 7/7] Update EmailService.cs --- .../Know-Your-Nation-Speedy/EmailService.cs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs index a69d79b..2fe41fe 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs @@ -16,7 +16,7 @@ public class EmailService string emailFromAddress = "**********"; //Sender Email Address string password = "***********"; //Sender Password - public bool SendMail(string To, string Subject, string Body) + public bool SendMail(string To, string Subject) { try { @@ -50,15 +50,5 @@ public void SmtpSend(MailMessage mail) smtp.EnableSsl = enableSSL; smtp.Send(mail); } - - public string generateCode() - { - - const string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - Random rand = new Random(); - - string code = new string(Enumerable.Repeat(characters, 10).Select(s => s[rand.Next(s.Length)]).ToArray()); - return code; - } } }