Administration¶
NuGet package:IdentityServer4 byIdentityServer org (archived on March 6, 2025)Ocelot extension package:Ocelot.Administration with version 23.4.3
Ocelot supports changing configuration during runtime via an authenticated HTTP API.This can be authenticated in two ways either using Ocelot’s internalIdentityServer (for authenticating requests to theAdministration API only) or hooking theAdministration API authentication into your ownIdentityServer.
The first thing you need to do if you want to use theAdministration API is bring in the relevantOcelot.Administration package:
Install-PackageOcelot.Administration
This will bring down everything needed by theAdministration API.
Warning! Currently, theAdministration feature relies solely on theIdentityServer4 package, whoserepository was archived by its owner on July 31, 2024 (for the first time) and again on March 6, 2025.The Ocelot team will deprecate the upcomingOcelot.Administration.IdentityServer4 extension package after the release of Ocelot version24.0.However,the repository will remain available, allowing for potential future patches.
Your Own IdentityServer[1]¶
All you need to do to hook into your ownIdentityServer is add the following configuration options with authentication to yourProgram.After that, we must pass these options to theAddAdministration() extension of theOcelotBuilder being returned byAddOcelot()[2], as shown below:
Action<JwtBearerOptions>options=o=>{o.Authority="https://identity-server-host:3333";o.RequireHttpsMetadata=true;// false in development environmento.TokenValidationParameters=new(){ValidateAudience=false,};//...};builder.Services.AddOcelot(builder.Configuration).AddAdministration("/administration",options);
You now need to get a token from yourIdentityServer and use in subsequent requests to Ocelot’sAdministration API.
Note: This feature is useful because theIdentityServer authentication middleware needs the URL of the server.If you are using theInternal IdentityServer, it might not always be possible to have the Ocelot URL.
Internal IdentityServer¶
The API is authenticated using Bearer tokens that you request from Ocelot itself.This is provided by the amazingIdentityServer project that the .NET community has been using for several years.Check it out.
In order to enable the administration section, you need to do a few things. First of all, add this to your initialProgram.
The path can be anything you want and it is obviously recommended don’t use a URL you would like to route through with Ocelot as this will not work.The administration uses theMapWhen functionality of ASP.NET Core and all requests to{root}/administration will be sent there not to the Ocelot middleware.
The secret is the client secret that Ocelot’s internalIdentityServer will use to authenticate requests to theAdministration API.This can be whatever you want it to be!In order to pass this secret string as parameter, we must call theAddAdministration() extension of theOcelotBuilder being returned byAddOcelot()[2], as shown below:
builder.Services.AddOcelot(builder.Configuration).AddAdministration("/administration","secret");
In order for theAdministration API to work, Ocelot andIdentityServer must be able to call themselves for validation.This means that you need to add the base URL of Ocelot to the global configuration if it is not the defaulthttp://localhost:5000.
Please note, if you are using something like Docker to host Ocelot, it might not be able to call back tolocalhost, etc., and you need to know what you are doing with Docker networking in this scenario.Anyway, this can be done as follows:
If you want to run on a different host and port locally:
"GlobalConfiguration":{"BaseUrl":"http://localhost:5580"}
or if Ocelot is exposed via DNS:
"GlobalConfiguration":{"BaseUrl":"http://mydns.com"}
Now, if you went with the configuration options above and want to access the API, you can use the Postman scripts calledocelot.postman_collection.json in the solution to change the Ocelot configuration.Obviously these will need to be changed if you are running Ocelot on a different URL tohttp://localhost:5000.
The scripts show you how to request a Bearer token from Ocelot and then use it to GET the existing configuration and POST a configuration.
If you are running multiple Ocelot instances in a cluster then you need to use a certificate to sign the Bearer tokens used to access theAdministration API.
In order to do this, you need to add two more environmental variables for each Ocelot in the cluster:
OCELOT_CERTIFICATE: The path to a certificate that can be used to sign the tokens. The certificate needs to be of the type X509 and obviously Ocelot needs to be able to access it.OCELOT_CERTIFICATE_PASSWORD: The password for the certificate.
Normally Ocelot just uses temporary signing credentials but if you set these environmental variables then it will use the certificate.If all the other Ocelot instances in the cluster have the same certificate then you are good!
Administration API¶
POST
{adminPath}/connect/tokenThis gets a token for use with the admin area using the client credentials we talk about setting above.Under the hood this calls into anIdentityServer hosted within Ocelot.
The body of the request is form-data as follows:
client_idset as adminclient_secretset as whatever you used when setting up the administration services.scopeset as admingrant_typeset as client_credentials
GET
{adminPath}/configurationThis gets the current Ocelot configuration. It is exactly the same JSON we use to set Ocelot up with in the first place.
POST
{adminPath}/configurationThis overwrites the existing configuration (should probably be a PUT!).We recommend getting your config from the GET endpoint, making any changes and posting it back… simples.
The body of the request is JSON and it is the same format as theFileConfigurationthat we use to set up Ocelot on a file system.
Please note, if you want to use this API then the process running Ocelot must have permission to write to the disk where your
ocelot.jsonorocelot.{environment}.jsonis located.This is because Ocelot will overwrite them on save.DELETE
{adminPath}/outputcache/{region}This clears a region of the cache. If you are using a backplane, it will clear all instances of the cache!Giving your the ability to run a cluster of Ocelots and cache over all of them in memory and clear them all at the same time, so just use a distributed cache.
The region is whatever you set against the
Regionfield in theFileCacheOptions section of the Ocelot configuration.
[1]
The “Your Own IdentityServer 1” feature was implemented for issue228.
[2](1,2)TheAddOcelot method adds default ASP.NET services to the DI container. You can call another extendedAddOcelotUsingBuilder method while configuring services to develop your ownCustom Builder. See more instructions in the “AddOcelotUsingBuilder method” section of theDependency Injection feature.