Skip to content

Commit 05f5692

Browse files
committed
2 parents da460a0 + 076e791 commit 05f5692

File tree

20 files changed

+1267
-60
lines changed

20 files changed

+1267
-60
lines changed

eFormAPI/eFormAPI/Controllers/AccountController.cs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Configuration;
34
using System.Linq;
45
using System.Net.Http;
56
using System.Security.Claims;
@@ -74,9 +75,8 @@ public async Task<OperationResult> ChangePassword(ChangePasswordModel model)
7475
if (!ModelState.IsValid)
7576
{
7677
var allErrors = ModelState.Values.SelectMany(v => v.Errors);
77-
return new OperationResult(false, string.Join(" ", allErrors.Select(x=>x.ErrorMessage)));
78+
return new OperationResult(false, string.Join(" ", allErrors.Select(x => x.ErrorMessage)));
7879
}
79-
8080
var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId<int>(),
8181
model.OldPassword,
8282
model.NewPassword);
@@ -85,10 +85,57 @@ public async Task<OperationResult> ChangePassword(ChangePasswordModel model)
8585
{
8686
return new OperationResult(false, string.Join(" ", result.Errors));
8787
}
88-
8988
return new OperationResult(true);
9089
}
9190

91+
// POST: /account/forgot-password
92+
[HttpPost]
93+
[Route("forgot-password")]
94+
[AllowAnonymous]
95+
public async Task<OperationResult> ForgotPassword(ForgotPasswordModel model)
96+
{
97+
if (ModelState.IsValid)
98+
{
99+
var user = await UserManager.FindByEmailAsync(model.Email);
100+
if (user == null)
101+
{
102+
return new OperationResult(false);
103+
}
104+
var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
105+
var link = ConfigurationManager.AppSettings["app:siteLink"];
106+
link = $"{link}/login/restore-password?userId={user.Id}&code={code}";
107+
await UserManager.SendEmailAsync(user.Id, "Reset Password",
108+
"Please reset your password by clicking <a href=\"" + link + "\">here</a>");
109+
return new OperationResult(true);
110+
}
111+
return new OperationResult(false);
112+
}
113+
114+
// POST: /account/reset-password
115+
[HttpPost]
116+
[Route("reset-password")]
117+
[AllowAnonymous]
118+
public async Task<OperationResult> ResetPassword(ResetPasswordModel model)
119+
{
120+
if (!ModelState.IsValid)
121+
{
122+
var allErrors = ModelState.Values.SelectMany(v => v.Errors);
123+
return new OperationResult(false, string.Join(" ", allErrors));
124+
}
125+
var user = await UserManager.FindByIdAsync(model.UserId);
126+
if (user == null)
127+
{
128+
return new OperationResult(false);
129+
}
130+
var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
131+
if (result.Succeeded)
132+
{
133+
return new OperationResult(true);
134+
}
135+
return new OperationResult(false, string.Join(" ", result));
136+
}
137+
138+
92139
#region Help Action
93140

94141
//// GET api/Account/ManageInfo?returnUrl=%2F&generateState=true

eFormAPI/eFormAPI/Infrastructure/Security/ApplicationOAuthProvider.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ public ApplicationOAuthProvider(string publicClientId)
2222

2323
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
2424
{
25-
2625
var userManager = context.OwinContext.GetUserManager<EformUserManager>();
27-
2826
EformUser user = await userManager.FindAsync(context.UserName, context.Password);
29-
3027
if (user == null)
3128
{
3229
context.SetError("The user name or password is incorrect.", "The user name or password is incorrect.");
Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Configuration;
3+
using System.IO;
4+
using System.Net;
5+
using System.Net.Mail;
6+
using System.Net.Sockets;
7+
using System.Threading.Tasks;
28
using Microsoft.AspNet.Identity;
39

410
namespace eFormAPI.Web.Infrastructure.Services
511
{
612
public class EmailService : IIdentityMessageService
713
{
8-
public async Task SendAsync(IdentityMessage message)
14+
public Task SendAsync(IdentityMessage message)
915
{
10-
await configSendGridasync(message);
11-
}
12-
13-
// Use NuGet to install SendGrid (Basic C# client lib)
14-
private async Task configSendGridasync(IdentityMessage message)
15-
{
16-
//var myMessage = new SendGridMessage();
17-
18-
//myMessage.AddTo(message.Destination);
19-
//myMessage.From = new System.Net.Mail.MailAddress("[email protected]", "Taiseer Joudeh");
20-
//myMessage.Subject = message.Subject;
21-
//myMessage.Text = message.Body;
22-
//myMessage.Html = message.Body;
23-
24-
//var credentials = new NetworkCredential(ConfigurationManager.AppSettings["emailService:Account"],
25-
// ConfigurationManager.AppSettings["emailService:Password"]);
26-
27-
//// Create a Web transport for sending email.
28-
//var transportWeb = new Web(credentials);
29-
30-
// Send the email.
31-
//if (transportWeb != null)
32-
//{
33-
// await transportWeb.DeliverAsync(myMessage);
34-
//}
35-
//else
36-
//{
37-
//Trace.TraceError("Failed to create Web transport.");
38-
await Task.FromResult(0);
39-
//}
16+
int.TryParse(ConfigurationManager.AppSettings["email:smtpPort"], out int port);
17+
var userName = ConfigurationManager.AppSettings["email:login"];
18+
var password = ConfigurationManager.AppSettings["email:password"];
19+
var smtp = new SmtpClient
20+
{
21+
Host = ConfigurationManager.AppSettings["email:smtpHost"],
22+
Port = port,
23+
EnableSsl = true,
24+
DeliveryMethod = SmtpDeliveryMethod.Network,
25+
UseDefaultCredentials = false,
26+
Credentials = new NetworkCredential(userName, password)
27+
};
28+
using (var mailMessage = new MailMessage(userName, message.Destination))
29+
{
30+
mailMessage.Subject = message.Subject;
31+
mailMessage.Body = message.Body;
32+
mailMessage.IsBodyHtml = true;
33+
smtp.Send(mailMessage);
34+
}
35+
return Task.FromResult(0);
4036
}
4137
}
4238
}

eFormAPI/eFormAPI/Web.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
<add key="webpages:Enabled" value="false" />
1616
<add key="ClientValidationEnabled" value="true" />
1717
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
18+
<add key="app:siteLink" value="" />
19+
<add key="email:smtpHost" value="smtp.gmail.com" />
20+
<add key="email:smtpPort" value="587" />
21+
<add key="email:login" value="" />
22+
<add key="email:password" value="" />
1823
</appSettings>
1924
<system.web>
2025
<compilation debug="true" targetFramework="4.6" />

0 commit comments

Comments
 (0)