Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Web;
  4. using Collector.BL.EmailService;
  5. using Collector.BL.Entity.Authorization;
  6. using Collector.BL.Extentions;
  7. using Collector.DAO.Entities;
  8. using Collector.DAO.Repository;
  9. using Microsoft.EntityFrameworkCore;
  10. using Microsoft.Extensions.Configuration;
  11.  
  12. namespace Collector.BL.UserService
  13. {
  14.     public class UserService : IUserService
  15.     {
  16.         private readonly IRepository<User> _userRepository;
  17.         private readonly IConfiguration _configuration;
  18.         private readonly IEmailService _emailService;
  19.  
  20.         public UserService(IRepository<User> userRepository, IConfiguration configuration,
  21.             IEmailService emailService)
  22.         {
  23.             _userRepository = userRepository;
  24.             _configuration = configuration;
  25.             _emailService = emailService;
  26.         }
  27.  
  28.         public async Task ResetPasswordAsync(string email)
  29.         {
  30.             var isExsist = await (await _userRepository.GetAllAsync(user => user.Email == email)).AnyAsync();
  31.             if (!isExsist)
  32.                 throw new Exception("This email is not exist");
  33.             var tokenToEncrypt = email + "|" + DateTime.Now.AddMinutes(10);
  34.             var encryptedText = Enctypting.Encrypt(tokenToEncrypt, _configuration["EncryptionKey"], true);
  35.             encryptedText = HttpUtility.UrlEncode(encryptedText);
  36.             await _emailService.SendEmailAsync(email, "Password reset",
  37.                 $"To reset password press this link: <a href=http://localhost:3000/resetPassword/{encryptedText}>link</a>");
  38.         }
  39.  
  40.         public async Task ResetPasswordTokenAsync(ResetPasswordDTO model)
  41.         {
  42.             var decryptedToken = await Task.Run(() =>
  43.                 Enctypting.Decrypt(HttpUtility.UrlDecode(model.Token), _configuration["EncryptionKey"], true));
  44.             var email = decryptedToken.Split('|')[0];
  45.             var expireTime = decryptedToken.Split('|')[1];
  46.             if (DateTime.Compare(DateTime.Now, DateTime.Parse(expireTime)) == 1)
  47.                 throw new Exception("Token is expired");
  48.             var oldUser = await (await _userRepository.GetAllAsync(user => user.Email == email)).FirstOrDefaultAsync();
  49.             if (oldUser == null)
  50.                 throw new Exception("Token is not valid");
  51.  
  52.             oldUser.Password = model.Password.CreateMd5();
  53.             await _userRepository.UpdateAsync(oldUser);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement