title: No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization
body: <p>I have a .NET Core 2.0 app and have a problem with authorization. I want to use custom authorization with special requests. Header and standard default authentication.
  First, I add configuration in <code>Startup.cs</code>:</p>
  
  <pre><code>public IServiceProvider ConfigureServices(IServiceCollection services)
  {
      // ...
      services.AddAuthorization(options =&gt;
      {
          options.AddPolicy(DefaultAuthorizedPolicy, policy =&gt;
          {
              policy.Requirements.Add(new TokenAuthRequirement());
          });
      });
      services.AddSingleton&lt;IAuthorizationHandler, AuthTokenPolicy&gt;();
      // ...
  }
  </code></pre>
  
  <p><code>AuthTokenPolicy.cs</code>:</p>
  
  <pre><code>public class AuthTokenPolicy : AuthorizationHandler&lt;TokenAuthRequirement&gt;
  {   
      protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TokenAuthRequirement requirement)
      {
          var filterContext = context.Resource as AuthorizationFilterContext;
          var response = filterContext.HttpContext.Response;
          try
          {
              // some validation code
  
              var isValidToken = isValidTokenTask.Result;
              if (!isValidToken)
              {
                  response.StatusCode = 401;
                  return Task.CompletedTask;
              }
  
              response.StatusCode = 200;
              context.Succeed(requirement);
          }
          catch (Exception)
          {
              return Task.CompletedTask;
          }
          return Task.CompletedTask;
      }
  }
  </code></pre>
  
  <p>and in <code>HomeController.cs</code>:</p>
  
  <pre><code>[Authorize(Policy = Startup.DefaultAuthorizedPolicy)]
  public async Task&lt;IActionResult&gt; IsVisible()
  </code></pre>
  
  <p>If I use the wrong request.header in <code>AuthTokenPolicy</code> I see it, but in the logs I see this error:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found.\r\n   at Microsoft.AspNetCore.Authentication.AuthenticationService.d__11.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Mvc.ChallengeResult.d__14.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__19.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__17.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__15.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at React.AspNet.BabelFileMiddleware.d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.d__6.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at core.common.Middleware.LoggingMiddleware.d__3.MoveNext() in D:\Dev\microservicePDP\Template\core.common\Middleware\LoggingMiddleware.cs:line 72</p>
  </blockquote>
  
  <p>After reading <a href="https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x" rel="noreferrer">Migrating Authentication and Identity to ASP.NET Core 2.0</a> I've added this code in startup.cs</p>
  
  <p>Quotation from the article : </p>
  
  <pre><code>services.AddAuthentication(options =&gt; 
  {
      options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
      options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
  });
  </code></pre>
  
  <blockquote>
    <p>Define a default scheme in 2.0 if one of the following conditions is true:
    You want the user to be automatically signed in
    You use the [Authorize] attribute or authorization policies without specifying schemes</p>
  </blockquote>
  
  <p>I added <code>AuthenticationScheme</code> and <code>DefaultChallengeScheme</code> in <code>ConfigureServices()</code>. It didn't help, the same error here. I've tried to use <code>app.UseAuthentication();</code> in the <code>Startup.Configure()</code> method, with no results.</p>
  
  <p>How can I use a custom authorization without authentication?</p>