title: ASP.NET Core 3.1 Azure AD Authentication throws OptionsValidationException
body: <p>I'm trying to use Azure Active Directory to handle authentication on a web app. However, when I try to hit an action with the <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authorization.authorizeattribute?view=aspnetcore-3.1" rel="noreferrer"><code>AuthorizeAttribute</code></a>, the app throws a <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.options.optionsvalidationexception?view=dotnet-plat-ext-3.1" rel="noreferrer"><code>OptionsValidationException</code></a>. With the following error:</p>
  
  <pre><code>Microsoft.Extensions.Options.OptionsValidationException: The 'Instance' option must be provided.
     at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
     at Microsoft.Extensions.Options.OptionsMonitor`1.&lt;&gt;c__DisplayClass11_0.&lt;Get&gt;b__0()
     at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
     at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
     at System.Lazy`1.CreateValue()
     at System.Lazy`1.get_Value()
     at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions)
     at Microsoft.Extensions.Options.OptionsMonitor`1.Get(String name)
     at Microsoft.AspNetCore.Authentication.AzureAD.UI.AzureADOpenIdConnectOptionsConfiguration.Configure(String name, OpenIdConnectOptions options)
     at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
     at Microsoft.Extensions.Options.OptionsMonitor`1.&lt;&gt;c__DisplayClass11_0.&lt;Get&gt;b__0()
     at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
     at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
     at System.Lazy`1.CreateValue()
     at System.Lazy`1.get_Value()
     at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions)
     at Microsoft.Extensions.Options.OptionsMonitor`1.Get(String name)
     at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.InitializeAsync(AuthenticationScheme scheme, HttpContext context)
     at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
     at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties)
     at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.ChallengeAsync(AuthenticationProperties properties)
     at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties)
     at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
  </code></pre>
  
  <p>I can't figure out what is causing this. Here's the code:</p>
  
  <p><strong>Add a package reference to <a href="https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.AzureAD.UI/" rel="noreferrer"><code>Microsoft.AspNetCore.Authentication.AzureAD.UI</code></a> version 3.1.1.</strong></p>
  
  <h3>Startup Class</h3>
  
  <pre class="lang-cs prettyprint-override"><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddAuthentication(defaultScheme: AzureADDefaults.AuthenticationScheme)
          .AddAzureAD(options =&gt;
          {
              options.ClientId = "&lt;client_id_goes_here&gt;";
              options.TenantId = "&lt;tenant_id_goes_here&gt;";
          });
  
      services.AddControllers();
  }
  
  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
      app.UseDeveloperExceptionPage();
      app.UseRouting();
      app.UseAuthorization();
      app.UseEndpoints(endpoints =&gt; endpoints.MapControllers());
  }
  </code></pre>
  
  <h3>Home Controller</h3>
  
  <p>Using only one controller.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class HomeController : Controller
  {
      [Route("")]
      [AllowAnonymous]
      public string Index() =&gt; "Hello Anonymous User!";
  
      [Route("restricted")]
      [Authorize]
      public string Restricted() =&gt; $"Hello, {User.Identity.Name}.";
  }
  </code></pre>
  
  <p>When you run the app and hit the Index action, you get the excepted output:</p>
  
  <pre><code>Hello Anonymous User!
  </code></pre>
  
  <p>When you hit the <code>/restricted</code> endpoint, then the exception is thrown.</p>