Base solution for your next web application
Open Closed

Confirm email after register user #3221


User avatar
0
nancyphan created

Hi I want to confirm email after register user. But when i confirm email by code and user id it return "invalid token" although i confirm the same code. Please help me to fix it. Thanks

This is my code:

public class UserAppService : DemoAPIAppServiceBase, IUserAppService
    {
        private readonly IRepository<User, long> _userRepository;
        private readonly UserManager _userManager;

        public static IDataProtectionProvider DataProtectionProvider { get; private set; }

        public UserAppService(IRepository<User, long> userRepository, UserManager userManager)
        {
            _userRepository = userRepository;
            _userManager = userManager;
            DataProtectionProvider = new DpapiDataProtectionProvider("LabourXchange");
            if (_userManager.UserTokenProvider == null)
            {
                _userManager.UserTokenProvider = new DataProtectorTokenProvider<User, long (DataProtectionProvider.Create("EmailConfirmation"));
            }
        }
public async Task<UserDto> CreateUser(CreateCandidateUserInput input)
        {

            var user = new User
            {
                UserName = input.EmailAddress,
                Name = input.Name,
                Surname = input.Surname,
                EmailAddress = input.EmailAddress,
                Password = new PasswordHasher().HashPassword(input.Password),
                IsActive = true
            };
            //Add default roles
            user.Roles = new List<UserRole> { new UserRole { RoleId = input.RoleId } };
            //Save user
            CheckErrors(await _userManager.CreateAsync(user));
            await CurrentUnitOfWork.SaveChangesAsync();   
            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user.Id);
            code = HttpUtility.UrlEncode(code);
            var callbackUrl = ConfigurationManager.AppSettings["LinkClient"];
            callbackUrl = "user/signup/confirmEmail/?id=" + user.Id + "&code=" + code;
            string path = HttpContext.Current.Server.MapPath("~/Templates/Signup/User/ConfirmationRegisterationEmail.html");

            var emailInput = new ConfirmationRegisterationEmailInput
            {
                EmailAddress = user.EmailAddress,
                Link = callbackUrl,
                Name = user.Name,
                Subject = "Confirmation register user",
                Path = path
            };
            SendConfirmationRegisterationEmail(emailInput);
            return user.MapTo<UserDto>();
        }
public async Task ConfirmEmail(int id, string code)
        {
            var user = await _userManager.GetUserByIdAsync(id);
            var decode = HttpUtility.UrlDecode(code);
            var result = await _userManager.ConfirmEmailAsync(id, decode);
            if (result .Success)
            {
                user.IsEmailConfirmed = true;

                await _userManager.UpdateAsync(user);
                await CurrentUnitOfWork.SaveChangesAsync();
            }
        }

==> result: Invalid token. I don't know why. Would you help me this problem, thanks you very much.


No answer yet!