title: How do I fix too many redirection errors in an ASP.NET Core application with Windows authentication?
body: <p>I have a ASP.NET Core 2.2 application with an <a href="http://en.wikipedia.org/wiki/ADO.NET_Entity_Framework" rel="nofollow noreferrer">Entity Framework</a> core which uses Windows authentication.</p>
  
  <p>When I run the application locally using the IIS Express edition or locally using IIS server, it runs fine. The same goes when I deploy it to our <a href="http://en.wikipedia.org/wiki/Windows_Server_2008" rel="nofollow noreferrer">Windows Server 2008</a> R2 IIS servers and when I run it by surfing to the servers, either by the server name or by using the site host header.</p>
  
  <p>The problem starts when I asked other users to access the application and they receive the error too many redirections.</p>
  
  <p>I tried enabling the anonymous authentication in IIS.</p>
  
  <p>I tried to disable all the HTTPS enforcements in my startup.cs file like
  <code>app.UseHsts()</code> and <code>app.UseHttpsRedirection()</code>.</p>
  
  <p>Here is my startup.cs class:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Globalization;
  using System.Linq;
  using System.Threading.Tasks;
  using GatePassSystem.Models;
  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.AspNetCore.Http;
  using Microsoft.AspNetCore.HttpsPolicy;
  using Microsoft.AspNetCore.Localization;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.AspNetCore.Mvc.Razor;
  using Microsoft.AspNetCore.Routing;
  using Microsoft.AspNetCore.Server.IISIntegration;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Options;
  using Newtonsoft.Json.Serialization;
  
  
  namespace GatePassSystem
  {
      public class RouteDataRequestCultureProvider : RequestCultureProvider
      {
          public int IndexOfCulture;
          public int IndexofUICulture;
  
          public override Task&lt;ProviderCultureResult&gt; DetermineProviderCultureResult(HttpContext httpContext)
          {
              if (httpContext == null)
                  throw new ArgumentNullException(nameof(httpContext));
  
              string culture = null;
              string uiCulture = null;
  
              var twoLetterCultureName = httpContext.Request.Path.Value.Split('/')[IndexOfCulture]?.ToString();
              var twoLetterUICultureName = httpContext.Request.Path.Value.Split('/')[IndexofUICulture]?.ToString();
  
              if (twoLetterCultureName == "he")
                  culture = "he-IL";
              else if (twoLetterCultureName == "en")
                  culture = uiCulture = "en-US";
  
              if (twoLetterUICultureName == "he")
                  culture = "he-IL";
              else if (twoLetterUICultureName == "en")
                  culture = uiCulture = "en-US";
  
              if (culture == null &amp;&amp; uiCulture == null)
                  return NullProviderCultureResult;
  
              if (culture != null &amp;&amp; uiCulture == null)
                  uiCulture = culture;
  
              if (culture == null &amp;&amp; uiCulture != null)
                  culture = uiCulture;
  
              var providerResultCulture = new ProviderCultureResult(culture, uiCulture);
  
              return Task.FromResult(providerResultCulture);
          }
      }
  
  
      public class LanguageRouteConstraint : IRouteConstraint
      {
          public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
          {
              if (!values.ContainsKey("culture"))
                  return false;
  
              var culture = values["culture"].ToString();
              return culture == "en" || culture == "he";
          }
      }
  
  
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              //services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
              //{
              //    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
              //    options.CheckConsentNeeded = context =&gt; true;
              //    options.MinimumSameSitePolicy = SameSiteMode.None;
              //});
  
              var connection = Configuration["ConnectionStrings:ConnStr"];
              //@"Server=(localdb)\mssqllocaldb;Database=IDC.GetPassSystem.DB;Trusted_Connection=True;ConnectRetryCount=0";
              services.AddDbContext&lt;CarEnteranceContext&gt;
                  (options =&gt; options.UseSqlServer(connection));
  
              services.AddLocalization(options =&gt; options.ResourcesPath = "Resources");
  
              services.AddMvc().AddViewLocalization(
                  LanguageViewLocationExpanderFormat.Suffix,
                  opts =&gt; { opts.ResourcesPath = "Resources"; })
                      .AddDataAnnotationsLocalization().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  
              services.Configure&lt;RequestLocalizationOptions&gt;(
                  opts =&gt;
                  {
                      var supportedCultures = new List&lt;CultureInfo&gt;
                      {
                              new CultureInfo("en-US"),
                              new CultureInfo("he-IL"),
                      };
  
                      opts.DefaultRequestCulture = new RequestCulture("he-IL");
                      // Formatting numbers, dates, etc.
                      opts.SupportedCultures = supportedCultures;
                      // UI strings that we have localized.
                      opts.SupportedUICultures = supportedCultures;
  
                      opts.RequestCultureProviders = new[]{ new RouteDataRequestCultureProvider{
                                  IndexOfCulture=1,
                                  IndexofUICulture=1
                              }};
                      //opts.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =&gt;
                      //{
                      //    // My custom request culture logic
                      //    return new ProviderCultureResult("he-IL");
                      //}));
  
                  });
  
              services.Configure&lt;RouteOptions&gt;(options =&gt;
              {
                  options.ConstraintMap.Add("culture", typeof(LanguageRouteConstraint));
              });
  
              services.AddAuthentication(IISDefaults.AuthenticationScheme);
              services.Configure&lt;IISServerOptions&gt;(options =&gt;
              {
                  options.AutomaticAuthentication = true;
              });
  
              services.AddSingleton&lt;IEmailConfiguration&gt;(Configuration.GetSection("EmailConfiguration").Get&lt;EmailConfiguration&gt;());
              services.AddSingleton(Configuration);
              //services.AddTransient&lt;IEmailService, EmailService&gt;();
              services.AddKendo();
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
              else
              {
                  app.UseExceptionHandler("/Home/Error");
                  // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                  //app.UseHsts();
              }
  
              //app.UseHttpsRedirection();
              app.UseStaticFiles();
              app.UseCookiePolicy();
  
              var options = app.ApplicationServices.GetService&lt;IOptions&lt;RequestLocalizationOptions&gt;&gt;();
              app.UseRequestLocalization(options.Value);
  
              app.UseMvc(routes =&gt;
              {
                  //routes.MapRoute(
                  //        name: "default",
                  //        template: "{controller=CarEnteranceRequests}/{action=Index}/{id?}");
                  routes.MapRoute(
                          name: "LocalizedDefault",
                          template: "{culture:culture}/{controller=CarEnteranceRequests}/{action=Index}/{id?}");
                  routes.MapRoute(
                      name: "CatchAll",
                       //template: "{culture:culture}/{controller=CarEnteranceRequests}/{action=Index}/{id?}");
                       template: "{*catchall}",
                        defaults: new { controller = "CarEnteranceRequests", action = "RedirectToDefaultCulture", culture = "he" });
              });
          }
      }
  }
  </code></pre>
  
  <p>And my <code>web.config</code> file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;location path="." inheritInChildApplications="false"&gt;
      &lt;system.webServer&gt;
        &lt;handlers&gt;
          &lt;add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /&gt;
        &lt;/handlers&gt;
        &lt;aspNetCore processPath="dotnet" arguments=".\GatePassSystem.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" /&gt;
      &lt;/system.webServer&gt;
    &lt;/location&gt;
  &lt;/configuration&gt;
  &lt;!--ProjectGuid: aa6e8352-12f8-4e41-8d73-bcb66aefada8--&gt;
  </code></pre>