Open Closed

Email confirmation token error v7.2.2 #6403


User avatar
0
abpnewtonvisionco created
  • ABP Framework version: v7.2.2
  • UI Type: Angular
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): Auth Server separated

Hey Team,

Currently we are facing an issue when the user uses the registration and the confirmation email setting is on, the user gets the email for him to confirm hisaccount but he gets an "Invalid Token" error in the EmailConfirmation page.

I've seen in the forum that this is an issue in this current version that we are using (7.2.2) and that was fixed somewhere in 7.3. Is there a way for us to fix this issue in the current version? Is there a way to override the EmailConfirmation page? I tried adding it in the /Pages/Account/EmailConfirmation but throws an error saying that it already exist in the project.

How can we fix this issue?

I've seen that this is kind of the fix but not entirely sure how to implement it: https://support.abp.io/QA/Questions/5422/Customize-email-confirmation-token-after-successfully-confirm-email

Regards


11 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can override the AccountAppService and EmailConfirmation

    
    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(AccountAppService))]
    public class MyAccountAppService : AccountAppService
    {
       ....
       
       public override async Task ConfirmEmailAsync(ConfirmEmailInput input)
        {
            var user = await UserManager.GetByIdAsync(input.UserId);
            if (user.EmailConfirmed)
            {
                return;
            }
    
            (await UserManager.ConfirmEmailAsync(user, input.Token)).CheckErrors();
            (await UserManager.UpdateSecurityStampAsync(user)).CheckErrors();
    
            await IdentitySecurityLogManager.SaveAsync(new IdentitySecurityLogContext
            {
                Identity = IdentitySecurityLogIdentityConsts.Identity,
                Action = IdentitySecurityLogActionConsts.ChangeEmail
            });
        }
    }
    
    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(EmailConfirmationModel))]
    public class MyEmailConfirmationModel : EmailConfirmationModel
    {
        ...
        
        public override async Task<IActionResult> OnGetAsync()
        {
            ReturnUrl = GetRedirectUrl(ReturnUrl, ReturnUrlHash);
    
            try
            {
                var user = await UserManager.GetByIdAsync(UserId);
                if (user.EmailConfirmed)
                {
                    EmailConfirmed = true;
                    return Page();
                }
    
                ValidateModel();
                InvalidToken = !await AccountAppService.VerifyEmailConfirmationTokenAsync(
                    new VerifyEmailConfirmationTokenInput()
                    {
                        UserId = UserId,
                        Token = ConfirmationToken
                    }
                );
    
                if (!InvalidToken)
                {
                    await _accountAppService.ConfirmEmailAsync(new ConfirmEmailInput
                    {
                        UserId = UserId,
                        Token = ConfirmationToken
                    });
    
                    EmailConfirmed = true;
                }
            }
            catch (Exception e)
            {
                if (e is AbpIdentityResultException && !string.IsNullOrWhiteSpace(e.Message))
                {
                    Alerts.Warning(GetLocalizeExceptionMessage(e));
                    return Page();
                }
    
                if (e is AbpValidationException)
                {
                    return Page();
                }
    
                throw;
            }
    
            return Page();
        }
    }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    You can also consider upgrading to 7.3,

  • User Avatar
    0
    abpnewtonvisionco created

    Hi,

    You can override the AccountAppService and EmailConfirmation

     
    [Dependency(ReplaceServices = true)] 
    [ExposeServices(typeof(AccountAppService))] 
    public class MyAccountAppService : AccountAppService 
    { 
       .... 
        
       public override async Task ConfirmEmailAsync(ConfirmEmailInput input) 
        { 
            var user = await UserManager.GetByIdAsync(input.UserId); 
            if (user.EmailConfirmed) 
            { 
                return; 
            } 
     
            (await UserManager.ConfirmEmailAsync(user, input.Token)).CheckErrors(); 
            (await UserManager.UpdateSecurityStampAsync(user)).CheckErrors(); 
     
            await IdentitySecurityLogManager.SaveAsync(new IdentitySecurityLogContext 
            { 
                Identity = IdentitySecurityLogIdentityConsts.Identity, 
                Action = IdentitySecurityLogActionConsts.ChangeEmail 
            }); 
        } 
    } 
    
    [Dependency(ReplaceServices = true)] 
    [ExposeServices(typeof(EmailConfirmationModel))] 
    public class MyEmailConfirmationModel : EmailConfirmationModel 
    { 
        ... 
         
        public override async Task<IActionResult> OnGetAsync() 
        { 
            ReturnUrl = GetRedirectUrl(ReturnUrl, ReturnUrlHash); 
     
            try 
            { 
                var user = await UserManager.GetByIdAsync(UserId); 
                if (user.EmailConfirmed) 
                { 
                    EmailConfirmed = true; 
                    return Page(); 
                } 
     
                ValidateModel(); 
                InvalidToken = !await AccountAppService.VerifyEmailConfirmationTokenAsync( 
                    new VerifyEmailConfirmationTokenInput() 
                    { 
                        UserId = UserId, 
                        Token = ConfirmationToken 
                    } 
                ); 
     
                if (!InvalidToken) 
                { 
                    await _accountAppService.ConfirmEmailAsync(new ConfirmEmailInput 
                    { 
                        UserId = UserId, 
                        Token = ConfirmationToken 
                    }); 
     
                    EmailConfirmed = true; 
                } 
            } 
            catch (Exception e) 
            { 
                if (e is AbpIdentityResultException && !string.IsNullOrWhiteSpace(e.Message)) 
                { 
                    Alerts.Warning(GetLocalizeExceptionMessage(e)); 
                    return Page(); 
                } 
     
                if (e is AbpValidationException) 
                { 
                    return Page(); 
                } 
     
                throw; 
            } 
     
            return Page(); 
        } 
    } 
    

    Hi Liang, thanks for the answer. I replaced the ConfirmEmail by adding a "CustomEmailConfirmation" Page under /Pages/Account/CustomEmailConfirmationModel.html and CustomEmailConfirmationModel.cs

    I am not sure for the "AppService". I added it under the same /Pages/Account and just a class with the code you sent.

    I added these changes and the Token that I get on the email still shows as invalid

    Am I adding in the wrong section the override for the "AppService"?

    Here it is how I added it:

    Regards,

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    The position looks like no problem, have you added this attrubute?

    [Dependency(ReplaceServices = true)] 
    [ExposeServices(typeof(AccountAppService))]
    
    [Dependency(ReplaceServices = true)] 
    [ExposeServices(typeof(EmailConfirmationModel))] 
    
  • User Avatar
    0
    abpnewtonvisionco created

    Hi,

    The position looks like no problem, have you added this attrubute?

    [Dependency(ReplaceServices = true)]  
    [ExposeServices(typeof(AccountAppService))] 
    
    [Dependency(ReplaceServices = true)]  
    [ExposeServices(typeof(EmailConfirmationModel))]  
    

    Hi Liang, Yes. I added that on top of both classes:

  • User Avatar
    0
    abpnewtonvisionco created

    So, after debugging I see that the code gets here when the user clicks on the link:

    However, the "EmailConfirmed" is always false, maybe we are missing something there?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Could you share the full logs?

  • User Avatar
    0
    abpnewtonvisionco created

    Hi,

    Sure, I have them with the app insight telemetry, can I send you those or you want them in a specific way?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I guess the user's email address was not confirmed, but there was a problem when confirming the email address.

    Please share it with email: shiwei.liang@volosoft.com

    Thanks.

  • User Avatar
    0
    abpnewtonvisionco created

    Hi,

    I guess the user's email address was not confirmed, but there was a problem when confirming the email address.

    Please share it with email: shiwei.liang@volosoft.com

    Thanks.

    Hi Liang,

    Done. I sent them to you.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I have checked but didn't find anything.

    I could not reproduce the problem in my local.

    Could you provide the full steps to reproduce? I will check it. thanks

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11