Open Closed

Automatically Login a User after Verify Phone Number or Resetting Password #1532


User avatar
0
learnabp created

We would like to automatically login in the user when they Confirm their Phone Number or Reset their Password using the forgot password feature, having the user redirected to the login screen or a confirmation screen adds confusion.

In the below code there is a task //TODO: Try to automatically login! can we please have the solution so we can use it in our SaaS product for DesertFire?

public virtual async Task<IActionResult> OnPostAsync()
{

   try
   {
       ValidateModel();
       SetNormalizeReturnUrl();

       await AccountAppService.ResetPasswordAsync(
           new ResetPasswordDto
           {
               UserId = UserId,
               ResetToken = ResetToken,
               Password = Password
           }
       );
   }
   catch (Exception e)
   {
       if (e is AbpIdentityResultException && !string.IsNullOrWhiteSpace(e.Message))
       {
           Alerts.Warning(GetLocalizeExceptionMessage(e));
           return Page();
       }

       if (e is AbpValidationException)
       {
           return Page();
       }

       throw;
   }

   //TODO: Try to automatically login!
   return RedirectToPage("./ResetPasswordConfirmation", new
   {
       returnUrl = NormalizeReturnUrl
   });
}

2 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try this:

    var user = await UserManager.FindByIdAsync(UserId.ToString());
    if (user != null)
    {
        var isPersistent = (await HttpContext.AuthenticateAsync(IdentityConstants.ApplicationScheme))?.Properties?.IsPersistent ?? false;
        await SignInManager.SignOutAsync();
    
        var additionalClaims = new List<Claim>();
        await SignInManager.SignInWithClaimsAsync(user, new AuthenticationProperties
        {
            IsPersistent = isPersistent
        }, additionalClaims);
        
        //Or
        //await SignInManager.SignInAsync(user, isPersistent: true);
    }
    
  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35