Redirect to Verify email page if Email is not verified

Prathamesh Shende 376 Reputation points
2022-07-17T13:04:07.21+00:00

After registering the user it redirect to verify email so It will send email verification link to user's email. So if user don't want to verify now or user missed out, Next time user going to add correct credential so It will show error - "Invalid login attempt" which is gone into else part please check the code.

So How I check if IsEmailVerified is true or false. If false then it will move to Send Email Verification Page.

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl ??= Url.Content("~/");

        ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();  

        if (ModelState.IsValid)  
        {  
            // This doesn't count login failures towards account lockout  
            // To enable password failures to trigger account lockout, set lockoutOnFailure: true  
            var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);  
            if (result.Succeeded)  
            {  
                _logger.LogInformation("User logged in.");  
                return LocalRedirect(returnUrl);  
            }  
            if (result.RequiresTwoFactor)  
            {  
                return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });  
            }  
            if (result.IsLockedOut)  
            {  
                _logger.LogWarning("User account locked out.");  
                return RedirectToPage("./Lockout");  
            }  
            else  
            {                                   
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");  
                return Page();  
            }  
        }  

        // If we got this far, something failed, redisplay form  
        return Page();  
    }  

asp.net core 6 Identity Project

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sreeju Nair 12,176 Reputation points
    2022-07-17T13:52:11.04+00:00

    You may use IsEmailConfirmed method of user manager to verify whether the user's email ID is confirmed.

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.usermanager-1.isemailconfirmedasync?view=aspnetcore-6.0

    In your sign in page, before calling the sign in code, you may verify whether the email is confirmed.

    var user = await _userManager.FindByNameAsync("username");  
            if (user != null)  
            {  
                if (!await _userManager.IsEmailConfirmedAsync(user))  
                {  
                    //Redirect user to confirm email flow....  
                }  
            }  
      
    // Here continue to the login flow...for e.g. PasswordSignInAsync  
    

    Hope this helps

    0 comments No comments

0 additional answers

Sort by: Most helpful