title: Azure AD and Redirect Uri with infinite login loop
body: <p>Good morning,
  I created a Blazor Server Side application in .NET5 with the standard VS2019 template and I want to authenticate via Azure OpenId.</p>
  <p>The app must be deployed under IIS in HTTP mode and a reverse proxy will give users an HTTPS url: the final url given by the reverse proxy is <a href="https://myapp-test.domain.it" rel="nofollow noreferrer">https://myapp-test.domain.it</a>.</p>
  <p>My problem is that both when I debug the application in VS both when I publish it under IIS the application reload in loops the authentication page and the the login fails.</p>
  <p>My Startup.cs:</p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
          {
  services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                  .AddMicrosoftIdentityWebApp(Configuration.GetSection(&quot;AzureAd&quot;))
                  .EnableTokenAcquisitionToCallDownstreamApi()
                  .AddInMemoryTokenCaches(); 
  
              services.AddControllersWithViews()
                  .AddMicrosoftIdentityUI();
  
              services.Configure&lt;ForwardedHeadersOptions&gt;(options =&gt;
              {
                  options.ForwardedHeaders =
                      ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost;
              });
  
              string redirectUri = Configuration.GetSection(&quot;AzureAd:RedirectUri&quot;).Value;
              string clientSecret = Configuration.GetSection(&quot;AzureAd:ClientSecret&quot;).Value;
              if (!string.IsNullOrWhiteSpace(redirectUri))
              {
                  services.Configure&lt;OpenIdConnectOptions&gt;(OpenIdConnectDefaults.AuthenticationScheme, options =&gt;
                  {
                      options.SaveTokens = true;
                      var redirectToIdpHandler = options.Events.OnRedirectToIdentityProvider;
                      options.Events.OnRedirectToIdentityProvider = async context =&gt;
                      {
                          await redirectToIdpHandler(context);
                          context.ProtocolMessage.RedirectUri = redirectUri;
                      };
                  });
              }
  //...
  }
  
  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              app.UseForwardedHeaders();
  //...
  }
  </code></pre>
  <p>My appsettings.json:</p>
  <pre><code>{  
    &quot;AzureAd&quot;: {
      &quot;Instance&quot;: &quot;https://login.microsoftonline.com/&quot;,
      &quot;Domain&quot;: &quot;XXXXXXX&quot;,
      &quot;TenantId&quot;: &quot;XXXXXXX&quot;,
      &quot;ClientId&quot;: &quot;XXXXXXX&quot;,
      &quot;ClientSecret&quot;: &quot;XXXXXXX&quot;,
      &quot;RedirectUri&quot;: &quot;https://myapp-test.domain.it&quot;,
      &quot;CallbackPath&quot;: &quot;/signin-oidc&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>In the redirect URIs on Azure I setted:</p>
  <pre><code>https://myapp-test.domain.it/
  https://myapp-test.domain.it/signin-oidc
  </code></pre>
  <p>I run the application (or I hosted it under IIS) with the following url: http://localhost:39146; but as I said calling the app with http://localhost:39146 or with <a href="https://myapp-test.domain.it" rel="nofollow noreferrer">https://myapp-test.domain.it</a> produces and &quot;infinite&quot; login loop that fails at the end.</p>