title: Ocelot Integration with Azure Active Directory Authentication .Net Core 3.1
body: <p><strong>Context:</strong> My company is moving to an API Gateway to manage all our or services in our network. Each service is currently authenticated using azure AD. Everything is single tenant and just allows company users.
  <strong>Question:</strong> Am I understanding this correctly? 
  <strong>Problem:</strong> When I call http:localhost:5000/authResource/get I get a 401 response back from Ocelot. After tracing through the error, I see that I get null back when the Ocelot Authentication middle ware attempts to authenticate using the AzureADJwtBearer scheme.</p>
  
  <p>I followed the advice from questions related to Ocelot and azure ad but even following that I am unable to get anything to work. (Mostly from this answer: <a href="https://stackoverflow.com/questions/56004750/how-set-up-ocelot-api-gateway-with-azure-active-directory?answertab=active#tab-top">How set up Ocelot Api Gateway with Azure Active Directory</a>) 
  I think I may be misunderstanding how the authentication is supposed to work. My current understanding is that I tell Ocelot to authenticate against one of my Apis using the AzureADJwtBearer scheme. In my configuration I have the information for that specific api set up (ClientId, TenantId, etc). </p>
  
  <p>Once I call the route, I am expecting Ocelot to make a call to Microsoft to start authentication. At this point, I expect the same flow as the Apis provide, i.e. I make the call and get the Microsoft login page which then redirects me back to the application once I enter my username and password.</p>
  
  <p>After redirecting me back to Ocelot I guess (this is the part I am fuzzy on), I expect ocelot to store the access token Microsoft sends back FOR THE SPECIFIC RESOURCE I JUST REQUESTED. Then I expect ocelot to attach the token to an Auth header and then send the request for the resource I initially asked for. </p>
  
  <p>To clarify this, I will include the code I have for my startup files, and my ocelot.json file.
  From Startup.cs</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
          {
  
              services.AddProtectedWebApi(Configuration)
                  .AddProtectedApiCallsWebApis(Configuration)
                  .AddInMemoryTokenCaches();
              services.AddOcelot(Configuration);
              services.AddControllers();
          }
  </code></pre>
  
  <p>Also from Startup.cs</p>
  
  <pre><code> public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
  
              app.UseHttpsRedirection();
  
              app.UseRouting();
  
              app.UseAuthorization();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapControllers();
              });
  
              app.UseOcelot().Wait();
  
  
              app.UseAuthentication();
          }
  </code></pre>
  
  <p>My ocelot.json file for the authenticated resource is as follows (names changed for security reasons):</p>
  
  <pre><code>{
        "DownstreamPathTemplate": "/api/Controller/Get",
        "DownstreamScheme": "http",
        "DownstreamHostAndPorts": [
          {
            "Host": "localhost",
            "Port": 5003
          }
        ],
        "UpstreamPathTemplate": "/authResource/get",
        "UpstreamHttpMethod": [ "GET" ],
  
        "AuthenticationOptions": {
          "AuthenticationProviderKey": "AzureADJwtBearer",
          "AllowedScopes": []
        }
      }
  </code></pre>
  
  <p>To solidify how I understand this, I will go through an example using the sample api shown in the ocelot configuration.</p>
  
  <p>I wish to access resource <a href="http://localhost:5003/api/Controller/Get" rel="nofollow noreferrer">http://localhost:5003/api/Controller/Get</a> which is a protected API meaning I can only get a response from this if I provide an authorization header with my get request. 
  I make a request through my ocelot gateway for the url <a href="http://localhost:5000/authResource/get" rel="nofollow noreferrer">http://localhost:5000/authResource/get</a> (I am hosting Ocelot on localhost:5000). 
  Ocelot sees it needs to authenticate to access this resource, so it makes a request using the AzureADJwtBearer scheme. 
  I am redirected to microsoft to login. Once done, I am sent back to the Ocelot application with an access token in tow.
  Ocelot takes this taken, creates the Auth header, and finally calls <a href="http://localhost:5003/api/Controller/Get" rel="nofollow noreferrer">http://localhost:5003/api/Controller/Get</a> and returns the result.</p>