Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading.Tasks;
- using System.Web;
- using Collector.BL.EmailService;
- using Collector.BL.Entity.Authorization;
- using Collector.BL.Extentions;
- using Collector.DAO.Entities;
- using Collector.DAO.Repository;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Configuration;
- namespace Collector.BL.UserService
- {
- public class UserService : IUserService
- {
- private readonly IRepository<User> _userRepository;
- private readonly IConfiguration _configuration;
- private readonly IEmailService _emailService;
- public UserService(IRepository<User> userRepository, IConfiguration configuration,
- IEmailService emailService)
- {
- _userRepository = userRepository;
- _configuration = configuration;
- _emailService = emailService;
- }
- public async Task ResetPasswordAsync(string email)
- {
- var isExsist = await (await _userRepository.GetAllAsync(user => user.Email == email)).AnyAsync();
- if (!isExsist)
- throw new Exception("This email is not exist");
- var tokenToEncrypt = email + "|" + DateTime.Now.AddMinutes(10);
- var encryptedText = Enctypting.Encrypt(tokenToEncrypt, _configuration["EncryptionKey"], true);
- encryptedText = HttpUtility.UrlEncode(encryptedText);
- await _emailService.SendEmailAsync(email, "Password reset",
- $"To reset password press this link: <a href=http://localhost:3000/resetPassword/{encryptedText}>link</a>");
- }
- public async Task ResetPasswordTokenAsync(ResetPasswordDTO model)
- {
- var decryptedToken = await Task.Run(() =>
- Enctypting.Decrypt(HttpUtility.UrlDecode(model.Token), _configuration["EncryptionKey"], true));
- var email = decryptedToken.Split('|')[0];
- var expireTime = decryptedToken.Split('|')[1];
- if (DateTime.Compare(DateTime.Now, DateTime.Parse(expireTime)) == 1)
- throw new Exception("Token is expired");
- var oldUser = await (await _userRepository.GetAllAsync(user => user.Email == email)).FirstOrDefaultAsync();
- if (oldUser == null)
- throw new Exception("Token is not valid");
- oldUser.Password = model.Password.CreateMd5();
- await _userRepository.UpdateAsync(oldUser);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement