0% found this document useful (0 votes)
3 views

fogot

The ForgotPasswordController handles the password recovery process in a web application. It verifies the user's email, generates a reset token, and sends a verification email with a reset link. The email includes a security message and the link expires in 24 hours for security reasons.

Uploaded by

dayen55024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

fogot

The ForgotPasswordController handles the password recovery process in a web application. It verifies the user's email, generates a reset token, and sends a verification email with a reset link. The email includes a security message and the link expires in 24 hours for security reasons.

Uploaded by

dayen55024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using DNTCaptcha.

Core;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Text;
using System.Web;
using TicketCore.Common;
using TicketCore.Data.EmailVerification.Command;
using TicketCore.Data.Usermaster.Queries;
using TicketCore.Models.Usermaster;
using TicketCore.Models.Verification;
using TicketCore.Services.MailHelper;
using TicketCore.ViewModels.LoginVM;
using TicketCore.ViewModels.Usermaster;
using TicketCore.Web.Helpers;
using System.Net;
using System.Net.Mail;

namespace TicketCore.Web.Controllers
{
public class ForgotPasswordController : Controller
{
private readonly IUserMasterQueries _userMasterQueries;
private readonly IMailingService _mailingService;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IVerificationCommand _verificationCommand;
private readonly ILogger<ForgotPasswordController> _logger;

public ForgotPasswordController(
IUserMasterQueries userMasterQueries,
IMailingService mailingService,
IWebHostEnvironment webHostEnvironment, IVerificationCommand
verificationCommand, ILogger<ForgotPasswordController> logger)
{
_userMasterQueries = userMasterQueries;
_mailingService = mailingService;
_webHostEnvironment = webHostEnvironment;
_verificationCommand = verificationCommand;
_logger = logger;
}

[HttpGet]
public IActionResult Recover()
{
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
[ValidateDNTCaptcha(ErrorMessage = "Please enter valid security code",
CaptchaGeneratorLanguage = Language.English, CaptchaGeneratorDisplayMode =
DisplayMode.ShowDigits)]

public IActionResult Recover(ForgotPasswordViewModel


forgotPasswordViewModel)
{
if (!
_userMasterQueries.CheckEmailIdExists(forgotPasswordViewModel.EmailId))
{
TempData["ForgotPasswordErrorMessage"] = "Entered EmailId is
Invalid";
}
else
{

try

{
var verifyuser =
_userMasterQueries.CheckEmailIdExists(forgotPasswordViewModel.EmailId);
SmtpClient client = new SmtpClient();

if (verifyuser)
{

var userdetails =
_userMasterQueries.GetUserdetailsbyEmailId(forgotPasswordViewModel.EmailId);
var token =
HashHelper.CreateHashSHA256((GenerateRandomNumbers.GenerateRandomDigitCode(6)));
var commonDatetime = DateTime.Now;
var sendingresult = CreateVerificationEmail(userdetails,
token);

string toEmail = forgotPasswordViewModel.EmailId;


string fromEmail = "[email protected]";

// Creare mesaj
MailMessage message = new MailMessage(fromEmail, toEmail);
message.Subject = "Test Email";
// message.Body = "Acesta este un test de email.";
var emailBody = CreateVerificationEmail(userdetails,
token);
message.Body = emailBody;
client.UseDefaultCredentials = false;
client.Port = 587;
client.Host = "smtp.gmail.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new
NetworkCredential("[email protected]", "Parola!@#");
client.EnableSsl = true;
// Trimitere email
client.Send(message);
TempData["ForgotPasswordMessage"] = "Un email a fost trimis
la adresa respectiva!";

}
catch (Exception ex)
{
_logger.LogError(ex, "ForgotPasswordController:Recover");
}
return RedirectToAction("Recover", "ForgotPassword");
}

return View();
}

private string CreateVerificationEmail(UserMaster user, string token)


{

AesAlgorithm aesAlgorithm = new AesAlgorithm();


var key = string.Join(":", new string[]
{ DateTime.Now.Ticks.ToString(), user.UserId.ToString() });
var encrypt = aesAlgorithm.EncryptToBase64String(key);

string linktoverify = $"{this.Request.Scheme}://{this.Request.Host}


{this.Request.PathBase}/VerifyResetPassword/Verify?
key={HttpUtility.UrlEncode(encrypt)}&hashtoken={HttpUtility.UrlEncode(token)}";

var stringtemplate = new StringBuilder();


stringtemplate.AppendLine("Please click the following link to reset
your password.");
stringtemplate.AppendLine($"Reset password link: <a
href=\"{linktoverify}\">Link</a>");
stringtemplate.AppendLine();
stringtemplate.AppendLine("If the link does not work, copy and paste
the URL into a new browser window. The URL will expire in 24 hours for security
reasons.");

return stringtemplate.ToString();
}
}
}

You might also like