title: Asp.Net Core Scaffolded 'Register' and 'Login' redirect not working in Azure AppService
body: <p>Asp.Net Core 3.0 app. I am using Entity Framework Identity individual authentication and have scaffolded in a Login page and a Register page. I have an SQL database in Azure.</p>
  <p>When I run on localhost, the registration &amp; login work fine. I have confirmed the new user is stored in the DB Users table.</p>
  <p>But when I deploy to Azure AppService, <strong>I get a 404 page not found error when I click the register or login button.</strong></p>
  <p>I have tried:</p>
  <ul>
  <li><p>Scaled up to a paid plan and implemented CNAME Using GoDaddy &amp; Azure for my app service to remove the <code>.azurewebsites.net</code> from the url</p>
  </li>
  <li><p>Added <code>asp-area</code> and <code>asp-page</code> to the <code>&lt;button&gt;</code> elements in my cshtml.</p>
  </li>
  <li><p>Turned on Azure Authentication &amp; Authorization in App Service and added <code>http://www.myapp.com</code> as a redirect url.</p>
  </li>
  <li><p>Whitelisted all my AppService Outbound IPs in my Azure DB server firewall settings.</p>
  </li>
  <li><p>I have tried using the dev console in Chrome to see whats happening but I couldnt see anything useful.</p>
  </li>
  <li><p>Pulled some of my hair out...</p>
  </li>
  </ul>
  <p>Any ideas where I should look?</p>
  <p><strong>Additional info:</strong></p>
  <p>appsettings.json</p>
  <pre><code>{
    &quot;ConnectionStrings&quot;: {
      &quot;DefaultConnection&quot;: &quot;Server=tcp:**-*****.database.windows.net,1433;Initial Catalog=****;Persist Security Info=False;User ID=****;Password=****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;&quot;
    },
    &quot;Logging&quot;: {
      &quot;LogLevel&quot;: {
        &quot;Default&quot;: &quot;Information&quot;,
        &quot;Microsoft&quot;: &quot;Warning&quot;,
        &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Information&quot;
      }
    },
    &quot;AllowedHosts&quot;: &quot;*&quot;
  </code></pre>
  <p>DbContext:</p>
  <pre><code>using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  using Microsoft.EntityFrameworkCore;
  
  namespace MyApp.Data
  {
      public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options) : base(options)
          {
          }
      }
  }
  </code></pre>
  <p>Register cshtml:</p>
  <pre><code>&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; asp-area=&quot;Identity&quot; asp-page=&quot;Register&quot; id=&quot;RegisterButton&quot;&gt;@_loc[Model.RegisterButtonName]&lt;/button&gt;
  </code></pre>
  <p>Login cshtml:</p>
  <pre><code>&lt;div class=&quot;form-group&quot;&gt;
  &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; asp-area=&quot;Identity&quot; asp-page=&quot;Login&quot; id=&quot;LoginButton&quot;&gt;@_loc[Model.LoginButtonName]&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  <p>Error:
  If I navigate to <a href="https://www.myapp.com/en/Identity/Account/Login" rel="nofollow noreferrer">https://www.myapp.com/en/Identity/Account/Login</a> from my homepage, the page loads fine. But when I click the Login/Register button, the redirect fails and shows the error below.</p>
  <p><a href="https://i.stack.imgur.com/O1vq4.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O1vq4.jpg" alt="Error Screenshot" /></a></p>
  <p>Middleware:</p>
  <pre><code>app.UseRouting();
  app.UseAuthentication();
  app.UseAuthorization();
  app.UseRequestLocalization();
      
  app.UseEndpoints(endpoints =&gt;
  {
  endpoints.MapControllerRoute(
      name: &quot;default&quot;, pattern: &quot; 
      {culture=en}/{controller=Home}/{action=Index}/{id?}&quot;);
  endpoints.MapControllerRoute(
      name: &quot;Features&quot;, pattern: &quot; 
      {culture=en}/{controller=Features}/{action=Features}/{id?}&quot;);
  endpoints.MapControllerRoute(
      name: &quot;About&quot;, pattern: &quot; 
      {culture=en}/{controller=About}/{action=About}/{id?}&quot;);
  endpoints.MapControllerRoute(
      name: &quot;Help&quot;, pattern: &quot; 
      {culture=en}/{controller=Help}/{action=Help}/{id?}&quot;);
  endpoints.MapRazorPages();
  });
  </code></pre>
  <p>Database:</p>
  <p>There is no user record being created in the DB when register is clicked, just the error page</p>
  <p><strong>Update:</strong></p>
  <p>I searched my entire codebase for 'localhost' but did not find it anywhere other than the IIS settings.</p>
  <p><strong>Update:</strong></p>
  <p>I added the server and virtual inbound IP addresses from the nslookup to the DB server firewall whitelist.</p>
  <p><strong>Update:</strong></p>
  <p>On Firefox I do not get an error. Simply a blank white page.</p>
  <p>On Edge I get the same 404 error that the page cannot be found.</p>
  <p><strong>Update:</strong></p>
  <p>In my startup.cs services I tried both <code>true</code> and <code>false</code> for <code>RequireConfirmedAccount</code></p>
  <pre><code>services.AddDefaultIdentity&lt;ApplicationUser&gt;( options =&gt; { options.SignIn.RequireConfirmedAccount = true; }).AddEntityFrameworkStores&lt;ApplicationDbContext&gt;();
  </code></pre>
  <p><strong>Update:</strong></p>
  <p>Confirmed that Build Action is set to Content for my cshtml properties.</p>
  <p><strong>Update:</strong></p>
  <p>Added <strong>TXT</strong> | <strong>asuid</strong> | <strong>app verification id</strong> to my DNS settings in GoDaddy.</p>
  <p>Added <strong>TXT</strong> | <strong>@</strong> | <strong>mywebsite.azurewebsites.net</strong> to my DNS settings in GoDaddy.</p>
  <p>Added mywebsite.com as an assigned custom domain &amp; verified it in the azure portal.</p>
  <p><strong>Update:</strong></p>
  <p>I was told on the MSDN forums that this may be because the http POST for the submit button wont work and I should change it to a anchor <code>&lt;a&gt;</code> tag or <code>Input</code> type button.</p>
  <p>Changing the html is fine, but I am trying to figure out what this will mean for my Razor page redirect code? If anyone can point me in the right direction it would be great. Existing redirect code for the register page shown below.</p>
  <pre><code>public async Task&lt;IActionResult&gt; OnPostAsync(string returnUrl = null)
  {
      returnUrl ??= Url.Content(&quot;~/&quot;);
      ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
      if (ModelState.IsValid)
      {
          var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email };
          var result = await _userManager.CreateAsync(user, Input.Password);
          if (result.Succeeded)
          {
              _logger.LogInformation(&quot;User created a new account with password.&quot;);
  
              var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
              code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
              var callbackUrl = Url.Page(
                  &quot;/Account/ConfirmEmail&quot;,
                  pageHandler: null,
                  values: new { area = &quot;Identity&quot;, userId = user.Id, code },
                  protocol: Request.Scheme);
  
              await _emailSender.SendEmailAsync(Input.Email, &quot;Confirm your email&quot;,
                  $&quot;Please confirm your account by &lt;a href='{HtmlEncoder.Default.Encode(callbackUrl)}'&gt;clicking here&lt;/a&gt;.&quot;);
  
              if (_userManager.Options.SignIn.RequireConfirmedAccount)
              {
                  return RedirectToPage(&quot;RegisterConfirmation&quot;, new { email = Input.Email });
              }
              else
              {
                  await _signInManager.SignInAsync(user, isPersistent: false);
                  return LocalRedirect(returnUrl);
              }
          }
          foreach (var error in result.Errors)
          {
              ModelState.AddModelError(string.Empty, error.Description);
          }
      }
  
      // If we got this far, something failed, redisplay form
      return Page();
  }
  </code></pre>
  <p>I'm guessing from a little research that my new cshtml will be something like below:</p>
  <pre><code>&lt;input id=&quot;RegisterButton&quot; runat=&quot;server&quot; onserverclick=&quot;RegisterButtonClick&quot; type=&quot;submit&quot; class=&quot;btn btn-primary&quot; asp-area=&quot;Identity&quot; asp-page=&quot;Register&quot; &gt;@_loc[Model.RegisterButtonName]&lt;/input&gt;
  </code></pre>
  <p><strong>Update:</strong></p>
  <p>I commented out <code>Use.Hsts</code> for prod in the <code>Configure</code> method of my <code>startup.cs</code></p>
  <p><strong>Update:</strong></p>
  <p>I tried hardcoding a <code>returnUrl</code> in the <code>OnPostAsync</code> method of my <code>login.cshtml.cs</code> instead of using <code>returnUrl ??= Url.Content(&quot;~/&quot;);</code>Same 404 error...so I'm not sure where the redirect is actually being triggered by the identity service that redirects to the same login page. Obviously not in my OnPostAsync, because it should have tried to reach the url I hardcoded instead, which was <code>~/myapp.com/en/Features/Features</code>. Ugh. This is giving me serious headaches.</p>
  <p><strong>Update:</strong></p>
  <p>I tried replacing <code>return LocalRedirect(returnUrl);</code> with
  <code>return LocalRedirect(Url.Action(&quot;Login&quot;, &quot;Identity/Account&quot;));</code>but it is not working on localhost so I have not tried it in prod. It is saying</p>
  <pre><code>No webpage was found for the web address: https://localhost:44305/en/Identity%2FAccount/Login
  </code></pre>
  <p><strong>Update:</strong></p>
  <p>I replaced the null coalescing operators for <code>returnUrl ??= Url.Content(&quot;~/&quot;);</code> with the <code>=</code> operator for <code>returnUrl = Url.Content(&quot;~/&quot;);</code></p>
  <p><strong>Update:</strong></p>
  <p>Confirmed that my <code>AddControllersWithViews</code> and <code>AddRazorPages</code> do <strong>NOT</strong> implement authorization globally by default, and my Login and Register page models have     <code>[AllowAnonymous]</code> specified.</p>
  <p><strong>Update:</strong></p>
  <p>I have made changes to my <code>OnPostAsync</code> method and my <code>ConfigureServices&lt;IdentityOptions&gt;</code>. I think the default IdentityOptions password requirements were masking the true issue.</p>
  <p>I can still log in fine on localhost using the user I created prior to scaffolding, and I can <strong>not</strong> log in on App Service with that user. But now on AppService I am getting an, 'Invalid Login Attempt' error on the UI for that pre-scaffolding user. There is no error in the console.</p>
  <p>So basically I still cant register or login on AppService, but somehow after 2 weeks an error message on the UI that I can try to diagnose feels like progress!</p>
  <p>I will investigate this because it is happening with new users I register in localhost also, so it needs to be resolved first anyway. If it doesn't help, I will look into CORS as the next stop to find an AppService resolution. I also reached out to  Microsoft, fingers crossed. Thanks folks.</p>
  <p><strong>UPDATE:
  This is now resolved. I honestly have no idea how. I made so many changes over the past few weeks, to undo all my commits one by one to find out what fixed it would just be too painful. When the painful memory subsides in a few weeks I will come back and try to break it again to find out what it was. I'll post an update then.</strong></p>