Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Email confirmation on user registration #2344


User avatar
0
donpedrodelamuerte created

Hi there, I am trying to implement an account confirmation feature when user register in the system. I am using the ABP template with asp.net Core and Module Zero.

I am trying to adapt this tutorial : <a class="postlink" href="https://docs.microsoft.com/en-us/aspnet/core/security/authentication/accconfirm">https://docs.microsoft.com/en-us/aspnet ... accconfirm</a>

But as you guessed it is not working...

Does boilerplate currently have built-in support for this feature? If not, how should I do it?


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Current template does not have it but you can do it simply like this,

    • There is a field called IsEmailConfirmed. Set this to false when a new user is registered and generate a password reset code and save it to user's PasswordResetCode field.
    • Then, create a link (You also need to create a Action in your AccountController) for example <a class="postlink" href="http://yoursite.com/Account/ResetPassword?userId=xxxxxxxxxxxx&tenantId=yyyyyyyyyyy">http://yoursite.com/Account/ResetPasswo ... yyyyyyyyyy</a>. (use encrypted values here) .
    • Send this link to user's email address and include password reset code in the email.
    • When user clicks to this link, he/she will be redirected to your ResetPassword action.
    • Decrypt userId and tenantId, find the matching user and compare the password reset code with the one user enters.
    • If everyting goes well, activate user :).

    I hope this helps.

  • User Avatar
    0
    donpedrodelamuerte created

    Thanks for the reply! It helped a lot!

    The only issue I have is I can't update the field IsActive and IsEmailConfirm for the user. Somehow, even if the user was created in the DB.

    What I'm trying to do is this: Once the user clicked the confirmation link he received by email this method is called:

    [UnitOfWork]
     public async Task<ActionResult> ConfirmEmail(int userID)
     {
            //find the user by id
            User userToUpdate = await _userManager.FindByIdAsync(userID);    //--> always return null
            //User userToUpdate = await _userManager.GetUserByIdAsync(userID);   //--> throw exception AbpException: There is no user with id: 7
    
             //activate user & confirm email - this is supposed to auto update the db...
             userToUpdate.IsActive = true;
             userToUpdate.IsEmailConfirmed = true;
    
             return RedirectToAction("Login");
    }
    

    Problem is, the user is never found. Would this be the right way to update a user ? It seems so when I read this page: [http://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work])...

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You also need to include tenantId of user in the email confirmation link and get it in your ConfirmEmail action.

    public async Task<ActionResult> ConfirmEmail(int? tenantId, int userID)
    

    Then before finding user, you need to switch the user's tenant.

    CurrentUnitOfWork.SetTenantId(tenantId);