This forum is not used anymore. Please ask new questions to Stack Overflow: https://stackoverflow.com/questions/tag ... ort=newest (use aspnetboilerplate tag).
By NancyPhan
#18339 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:
Code: Select all    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"));
            }
        }

Code: Select allpublic 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>();
        }

Code: Select allpublic 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.