Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions eFormAPI/eFormAPI/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Security.Claims;
Expand Down Expand Up @@ -74,9 +75,8 @@ public async Task<OperationResult> ChangePassword(ChangePasswordModel model)
if (!ModelState.IsValid)
{
var allErrors = ModelState.Values.SelectMany(v => v.Errors);
return new OperationResult(false, string.Join(" ", allErrors.Select(x=>x.ErrorMessage)));
return new OperationResult(false, string.Join(" ", allErrors.Select(x => x.ErrorMessage)));
}

var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId<int>(),
model.OldPassword,
model.NewPassword);
Expand All @@ -85,10 +85,57 @@ public async Task<OperationResult> ChangePassword(ChangePasswordModel model)
{
return new OperationResult(false, string.Join(" ", result.Errors));
}

return new OperationResult(true);
}

// POST: /account/forgot-password
[HttpPost]
[Route("forgot-password")]
[AllowAnonymous]
public async Task<OperationResult> ForgotPassword(ForgotPasswordModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByEmailAsync(model.Email);
if (user == null)
{
return new OperationResult(false);
}
var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var link = ConfigurationManager.AppSettings["app:siteLink"];
link = $"{link}/login/restore-password?userId={user.Id}&code={code}";
await UserManager.SendEmailAsync(user.Id, "Reset Password",
"Please reset your password by clicking <a href=\"" + link + "\">here</a>");
return new OperationResult(true);
}
return new OperationResult(false);
}

// POST: /account/reset-password
[HttpPost]
[Route("reset-password")]
[AllowAnonymous]
public async Task<OperationResult> ResetPassword(ResetPasswordModel model)
{
if (!ModelState.IsValid)
{
var allErrors = ModelState.Values.SelectMany(v => v.Errors);
return new OperationResult(false, string.Join(" ", allErrors));
}
var user = await UserManager.FindByIdAsync(model.UserId);
if (user == null)
{
return new OperationResult(false);
}
var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
if (result.Succeeded)
{
return new OperationResult(true);
}
return new OperationResult(false, string.Join(" ", result));
}


#region Help Action

//// GET api/Account/ManageInfo?returnUrl=%2F&generateState=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ public ApplicationOAuthProvider(string publicClientId)

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{

var userManager = context.OwinContext.GetUserManager<EformUserManager>();

EformUser user = await userManager.FindAsync(context.UserName, context.Password);

if (user == null)
{
context.SetError("The user name or password is incorrect.", "The user name or password is incorrect.");
Expand Down
60 changes: 28 additions & 32 deletions eFormAPI/eFormAPI/Infrastructure/Services/EmailService.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
using System.Threading.Tasks;
using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;

namespace eFormAPI.Web.Infrastructure.Services
{
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
public Task SendAsync(IdentityMessage message)
{
await configSendGridasync(message);
}

// Use NuGet to install SendGrid (Basic C# client lib)
private async Task configSendGridasync(IdentityMessage message)
{
//var myMessage = new SendGridMessage();

//myMessage.AddTo(message.Destination);
//myMessage.From = new System.Net.Mail.MailAddress("taiseer@bitoftech.net", "Taiseer Joudeh");
//myMessage.Subject = message.Subject;
//myMessage.Text = message.Body;
//myMessage.Html = message.Body;

//var credentials = new NetworkCredential(ConfigurationManager.AppSettings["emailService:Account"],
// ConfigurationManager.AppSettings["emailService:Password"]);

//// Create a Web transport for sending email.
//var transportWeb = new Web(credentials);

// Send the email.
//if (transportWeb != null)
//{
// await transportWeb.DeliverAsync(myMessage);
//}
//else
//{
//Trace.TraceError("Failed to create Web transport.");
await Task.FromResult(0);
//}
int.TryParse(ConfigurationManager.AppSettings["email:smtpPort"], out int port);
var userName = ConfigurationManager.AppSettings["email:login"];
var password = ConfigurationManager.AppSettings["email:password"];
var smtp = new SmtpClient
{
Host = ConfigurationManager.AppSettings["email:smtpHost"],
Port = port,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(userName, password)
};
using (var mailMessage = new MailMessage(userName, message.Destination))
{
mailMessage.Subject = message.Subject;
mailMessage.Body = message.Body;
mailMessage.IsBodyHtml = true;
smtp.Send(mailMessage);
}
return Task.FromResult(0);
}
}
}
5 changes: 5 additions & 0 deletions eFormAPI/eFormAPI/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="app:siteLink" value="" />
<add key="email:smtpHost" value="smtp.gmail.com" />
<add key="email:smtpPort" value="587" />
<add key="email:login" value="" />
<add key="email:password" value="" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6" />
Expand Down
Loading