Blazor WASM PWA app - email account confirmation. Please help

Posted 172 days ago by Francois
Edited 172 days ago
0

Hi All

I have created a blazor WASM PWA .Net6 application that allows a user to register with some basic details.

Once the user registers the application sends a email confirmation link with a link and token to validate the email address.

This is all working fine in production I can register and confirm email via the link provided. 

When I host the application on my local machine IIS the application works but when I register a new account and use the email confirmation link I keep getting a message saying "Sorry, there's nothing at this address."

this is happening on all the pages that uses "return View("xyz"). Even when I use the passwords reset function that also returns a view I get the same message  "Sorry, there's nothing at this address.". This is only happening on controllers that returns a view and it is only happening when the app is hosted. I don't see any errors in DEV tools in chrome. When I click on the email confirmation link it takes me to the correct page using this link:

https://localhost/confirmemail?token=CfDJ8KzzpED9Bh1EllkgzIX2Irm9CPTd%2FsBR2lNzn2kYtjOAiFsoeJ44Crm8%2B4HVudPtOipeshkSB5cNLKXNtUNnPzqS%2FbNeOtCqRJ4QfRWaPR1YD8Nrss6MxRrUotCWgefDhTjUnTOUnJ19A3QiiB%2Fx4BZaUjBnMCnq609ixEwnoZ%2BqWpQOsE2ptyczBLpHtkV4OSIbVoc6YbmB52hzAULOEWbC8Y4%2Bc5N6ENVso69%2BemCn%2FHSjOigHvKIUBMcViq68hg%3D%3D&email=JJK.Something@gmail.com

 

but displayed:   "Sorry, there's nothing at this address.".  if I do a ctrl+F5 it completes the email confirmation process and all is fine. But I cannot understand why I have to do a refresh "CTRL+F5" to complete the email confirmation or password reset this is only when the app is hosted and its only on Return View all other razor pages are working as expected.

 

My account controller to generate the link on login uses the following code:

var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

var confirmationLink = Url.Link("Confirmation", new { token, email = user.Email });

await _emailSender.SendEmailAsync(user.Email, "Account Confirmation Link - CrimeLogger",

 $"Please confirm your account by clicking <a href=\"" + confirmationLink + "\">here</a>");

 

The email is sent and I use the email link to confirm registration:

The link points to the following controller:

 

[Route("ConfirmEmail", Name = "Confirmation")]

   [ApiController]

   [Authorize]

   public class EmailController : Controller

   {

       private readonly UserManager<ApplicationUser> _userManager;

 

       public EmailController(UserManager<ApplicationUser> userManager)

       {

           _userManager = userManager;

       }

       [HttpGet]

       [AllowAnonymous]

       public async Task<IActionResult> ConfirmEmail(string token, string email)

       {

           var user = await _userManager.FindByNameAsync(email);

           if (user == null)

               return View("Error");

           var result = await _userManager.ConfirmEmailAsync(user, token);

           if (result.Succeeded)

           {

               return View("ConfirmedEmail");

           }

           else

           {

               return View("emailconfirmerror");

           }

 

   it should load the view below: but it does not, only after CTRL+F5 does it load the below view. Any suggestions on where I can look to try and resolve this error. Remember this only happens when the app is hosted and only on Views I have cleared the browser cash and tried again but still same situation

 

@using Common

@using Microsoft.AspNetCore.Components

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{

   ViewData["Title"] = "ConfirmedEmail";

}

@inject NavigationManager NavigationManager

 

<!DOCTYPE html>

<html lang="en">

 

<head>

   <meta charset="utf-8" />

   <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

   <title>ConfirmEmail</title>

   <link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" />

   <link href="~/css/app.css" rel="stylesheet" asp-append-version="true" />

</head>

<body>

 

   <div class="row col-md-12" style="place-content:center;text-align:center">

 

   <div class="card border">

       <div class="card-body">

           <h3 class="col-12 text-black text-center py-2">

               Welcome to XYZ 

           </h3>

           <div>

               <p>Thank you for confirming your email. Please log in <a href="@SD.TempLink">here</a>.

               </p>

           </div>

       </div>

   </div>

</div>

</body>

</html>        

 

Thank you

 

  • 0

    I'm not sure why it works differently locally than on the server, but when you refresh, does the URL change at all?  Is it maybe causing the encoded string to decode, then processing it okay?  

    Try adding the following just above the line that has _userManager.ConfirmEmailAsync (user, token);

    token = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(token));

    This would url decode the string before it tries to confirm it, in case that's the issue.

    Posted 172 days ago by selliott
  • 0

    Hi 

     

    I tried adding the line suggested, if I do that it renders the -  return View("emailconfirmerror"); so the error view returns correctly. I then changed it back to inspect the url again and to compare the failed one with the refreshed version after ctrl+F5 and the 2 are identical

    failed/ before ctrl+f5

    https://localhost/confirmemail?token=CfDJ8KzzpED9Bh1EllkgzIX2Irm23ewD5H3SU584Exbl8z3KrYuKmy0rZ2rd0pWo6LGtmrzjFnn9hRumctJ1EzQpfjX4DvLYqqCatmsPyR01pC7l5BcCFFQweLcB%2FRTcae2yHNj9Bw9ZJ%2FgL1m9j9Qqiysdg%2Bwe%2FrOMhNYH0IrGqgCbB4%2F2naDof5oeGb533YQpYQkE7kuiW%2Fk39C85%2BJt%2BAyw3197ihGdaXfb7EPAc3lMaOnBQNTs4v7GXH%2Ffymtw06dg%3D%3D&email=xxxx@gmail.com

    success after ctrl+F5

    https://localhost/confirmemail?token=CfDJ8KzzpED9Bh1EllkgzIX2Irm23ewD5H3SU584Exbl8z3KrYuKmy0rZ2rd0pWo6LGtmrzjFnn9hRumctJ1EzQpfjX4DvLYqqCatmsPyR01pC7l5BcCFFQweLcB%2FRTcae2yHNj9Bw9ZJ%2FgL1m9j9Qqiysdg%2Bwe%2FrOMhNYH0IrGqgCbB4%2F2naDof5oeGb533YQpYQkE7kuiW%2Fk39C85%2BJt%2BAyw3197ihGdaXfb7EPAc3lMaOnBQNTs4v7GXH%2Ffymtw06dg%3D%3D&email=xxxx@gmail.com

     

    the fact that it returned the error view page stunned me, as both the emailconfirmerror view page and the confirmedemail view pages are identical except for the message body <p> tag. I am stunned I just cant seem to find the issue here. Both pages are located in the same subfolder under Views folder in my app. I even tried different browsers now and still the same issues.

    do you know if there is any other way I might be able to debug this issue??, of all the complexity in this app this is thing that's not working it killing my brain.. lol

     

     

    Posted 172 days ago by Francois
  • 0

    If your issue is on the live website and not the local version, why does the URL have https://localhost in it?

    Edit: Nevermind, I just re-read and see that the issue is local and not on the production server.

    Posted 172 days ago by selliott Edited 172 days ago
  • 0

    I am hosting it locally on my pc using IIS.

     If I run it in debug mode in Visual Studio 2022 I don't have any issues.

    Posted 172 days ago by Francois
  • 0

    Perhaps it's related to IIS then.  I could be wrong, but I believe debug mode may run in Kestrel.

    Posted 172 days ago by selliott
  • 0

    I think it is. I'm going to register the domain and setup the hosting etc. and see if  I run into the same issues. I hope not. Difficult to find sites to host for testing purposes, do you know of any? 

    Posted 172 days ago by Francois
  • 0

    If you launch the app by going to your Server directory in command line and launch it with "dotnet watch" (should open the browser automatically) or "dotnet run" and open the browser manually, I suspect it will run fine then too.

    Azure offers free tiers for testing purposes.  You can add an App Service to host an app and just choose the free tier.  It has limited resources, but is fine for testing.

    Posted 172 days ago by selliott
  • 0

    Perfect thank you. I will let you know if the issue is with IIS if the hosted version works on azure. Thanks again for the assistance 

    Posted 172 days ago by Francois
Someone is typing...

Post a Reply

You must be logged in to add a new post.
Number of online users: 1
An error has occurred. This application may no longer respond until reloaded. Reload 🗙