Skip to content

Commit 9304dd2

Browse files
jvanderleiraman-m
andauthored
#1876 Replace JwtSecurityTokenHandler with JsonWebTokenHandler in the Authentication docs according to breaking changes in .NET 8 Auth (#2238)
* doc: changed namespace * doc: added footnote for JsonWebToken Breaking change * Update docs/features/authentication.rst * Review Authentication doc --------- Co-authored-by: Raman Maksimchuk <[email protected]>
1 parent 47d279d commit 9304dd2

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

docs/features/authentication.rst

+49-31
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ users must register authentication services in their **Startup.cs** as usual but
1010
public void ConfigureServices(IServiceCollection services)
1111
{
1212
const string AuthenticationProviderKey = "MyKey";
13-
services
14-
.AddAuthentication()
13+
services.AddAuthentication()
1514
.AddJwtBearer(AuthenticationProviderKey, options =>
1615
{
1716
// Custom Authentication setup via options initialization
@@ -21,10 +20,10 @@ users must register authentication services in their **Startup.cs** as usual but
2120
In this example ``MyKey`` is `the scheme <https://learn.microsoft.com/en-us/aspnet/core/security/authentication/#authentication-scheme>`_ that this provider has been registered with.
2221
We then map this to a Route in the configuration using the following `AuthenticationOptions <https://github.com/search?q=repo%3AThreeMammals%2FOcelot%20AuthenticationOptions&type=code>`_ properties:
2322

24-
* ``AuthenticationProviderKey`` is a string object, obsolete [#f1]_. This is legacy definition when you define :ref:`authentication-single`.
23+
* ``AuthenticationProviderKey`` is a string object, obsolete [#f1]_. This is legacy definition when you define :ref:`authentication-scheme`.
2524
* ``AuthenticationProviderKeys`` is an array of strings, the recommended definition of :ref:`authentication-multiple` feature.
2625

27-
.. _authentication-single:
26+
.. _authentication-scheme:
2827

2928
Single Key aka Authentication Scheme [#f1]_
3029
-------------------------------------------
@@ -54,8 +53,7 @@ Multiple Authentication Schemes [#f2]_
5453
| Property: ``AuthenticationOptions.AuthenticationProviderKeys``
5554
5655
In real world of ASP.NET, apps may need to support multiple types of authentication by single Ocelot app instance.
57-
To register `multiple authentication schemes <https://learn.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme#use-multiple-authentication-schemes>`_
58-
(`authentication provider keys <https://github.com/search?q=repo%3AThreeMammals%2FOcelot%20AuthenticationProviderKey&type=code>`_) for each appropriate authentication provider, use and develop this abstract configuration of two or more schemes:
56+
To register `multiple authentication schemes`_ (`authentication provider keys <https://github.com/search?q=repo%3AThreeMammals%2FOcelot%20AuthenticationProviderKey&type=code>`_) for each appropriate authentication provider, use and develop this abstract configuration of two or more schemes:
5957

6058
.. code-block:: csharp
6159
@@ -78,7 +76,7 @@ We then map these schemes to a Route in the configuration as shown below.
7876
"AllowedScopes": []
7977
}
8078
81-
Afterward, Ocelot applies all steps that are specified for ``AuthenticationProviderKey`` as :ref:`authentication-single`.
79+
Afterward, Ocelot applies all steps that are specified for ``AuthenticationProviderKey`` as :ref:`authentication-scheme`.
8280

8381
**Note** that the order of the keys in an array definition does matter! We use a "First One Wins" authentication strategy.
8482

@@ -95,10 +93,9 @@ If you want to authenticate using JWT tokens maybe from a provider like `Auth0 <
9593
9694
public void ConfigureServices(IServiceCollection services)
9795
{
98-
var authenticationProviderKey = "MyKey";
99-
services
100-
.AddAuthentication()
101-
.AddJwtBearer(authenticationProviderKey, options =>
96+
const string AuthenticationProviderKey = "MyKey";
97+
services.AddAuthentication()
98+
.AddJwtBearer(AuthenticationProviderKey, options =>
10299
{
103100
options.Authority = "test";
104101
options.Audience = "test";
@@ -115,11 +112,10 @@ Then map the authentication provider key to a Route in your configuration e.g.
115112
"AllowedScopes": []
116113
}
117114
118-
Docs
119-
^^^^
115+
**JWT Tokens Docs**
120116

121-
* Microsoft Learn: `Authentication and authorization in minimal APIs <https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/security>`_
122-
* Andrew Lock | .NET Escapades: `A look behind the JWT bearer authentication middleware in ASP.NET Core <https://andrewlock.net/a-look-behind-the-jwt-bearer-authentication-middleware-in-asp-net-core/>`_
117+
* Microsoft Learn: `Authentication and authorization in minimal APIs <https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/security>`_
118+
* Andrew Lock | .NET Escapades: `A look behind the JWT bearer authentication middleware in ASP.NET Core <https://andrewlock.net/a-look-behind-the-jwt-bearer-authentication-middleware-in-asp-net-core/>`_
123119

124120
Identity Server Bearer Tokens
125121
-----------------------------
@@ -131,15 +127,14 @@ If you don't understand how to do this, please consult the IdentityServer `docum
131127
132128
public void ConfigureServices(IServiceCollection services)
133129
{
134-
var authenticationProviderKey = "MyKey";
130+
const string AuthenticationProviderKey = "MyKey";
135131
Action<JwtBearerOptions> options = (opt) =>
136132
{
137133
opt.Authority = "https://whereyouridentityserverlives.com";
138134
// ...
139135
};
140-
services
141-
.AddAuthentication()
142-
.AddJwtBearer(authenticationProviderKey, options);
136+
services.AddAuthentication()
137+
.AddJwtBearer(AuthenticationProviderKey, options);
143138
services.AddOcelot();
144139
}
145140
@@ -154,37 +149,42 @@ Then map the authentication provider key to a Route in your configuration e.g.
154149
155150
Auth0 by Okta
156151
-------------
152+
157153
Yet another identity provider by `Okta <https://www.okta.com/>`_, see `Auth0 Developer Resources <https://developer.auth0.com/>`_.
158154

159155
Add the following to your startup ``Configure`` method:
160156

161157
.. code-block:: csharp
162158
163-
app.UseAuthentication()
164-
.UseOcelot().Wait();
159+
app.UseAuthentication();
160+
await UseOcelot();
165161
166162
Add the following, at minimum, to your startup ``ConfigureServices`` method:
167163

168164
.. code-block:: csharp
169165
170-
services
171-
.AddAuthentication()
172-
.AddJwtBearer(oktaProviderKey, options =>
166+
const string OktaProviderKey = "MyKey";
167+
services.AddAuthentication()
168+
.AddJwtBearer(OktaProviderKey, options =>
173169
{
174170
options.Audience = configuration["Authentication:Okta:Audience"]; // Okta Authorization server Audience
175171
options.Authority = configuration["Authentication:Okta:Server"]; // Okta Authorization Issuer URI URL e.g. https://{subdomain}.okta.com/oauth2/{authidentifier}
176172
});
177173
services.AddOcelot(configuration);
178174
179-
**Note** In order to get Ocelot to view the scope claim from Okta properly, you have to add the following to map the default Okta ``"scp"`` claim to ``"scope"``:
175+
In order to get Ocelot to view the scope claim from Okta properly, you have to add the following to map the default Okta ``scp`` claim to ``scope``:
180176

181177
.. code-block:: csharp
182178
183179
// Map Okta "scp" to "scope" claims instead of http://schemas.microsoft.com/identity/claims/scope to allow Ocelot to read/verify them
184-
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("scp");
185-
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Add("scp", "scope");
180+
JsonWebTokenHandler.DefaultInboundClaimTypeMap.Remove("scp");
181+
JsonWebTokenHandler.DefaultInboundClaimTypeMap.Add("scp", "scope");
182+
183+
**Okta Notes**
186184

187-
`Issue 446 <https://github.com/ThreeMammals/Ocelot/issues/446>`_ contains some code and examples that might help with Okta integration.
185+
1. Issue `446`_ contains some code and examples that might help with Okta integration.
186+
2. Here is documentation for better clarity on claims mapping: `Mapping, customizing, and transforming claims in ASP.NET Core`_.
187+
3. It is highly advisable to read and understand the :ref:`authentication-warning` related to the critical changes in authentication when utilizing .NET 8. [#f4]_
188188

189189
Allowed Scopes
190190
--------------
@@ -193,12 +193,22 @@ If you add scopes to **AllowedScopes**, Ocelot will get all the user claims (fro
193193

194194
This is a way to restrict access to a Route on a per scope basis.
195195

196+
.. _authentication-warning:
197+
198+
Warning
199+
-------
200+
201+
.NET 8 introduced a breaking change [#f4]_ where ``JwtSecurityToken`` was replaced with ``JsonWebToken`` to enhance performance and reliability.
202+
Consequently, their handlers were changed ``JwtSecurityTokenHandler`` to ``JsonWebTokenHandler``.
203+
For versions prior to .NET 8, use the previous classes.
204+
196205
Links
197206
-----
198207

199208
* Microsoft Learn: `Overview of ASP.NET Core authentication <https://learn.microsoft.com/en-us/aspnet/core/security/authentication/>`_
200209
* Microsoft Learn: `Authorize with a specific scheme in ASP.NET Core <https://learn.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme>`_
201210
* Microsoft Learn: `Policy schemes in ASP.NET Core <https://learn.microsoft.com/en-us/aspnet/core/security/authentication/policyschemes>`_
211+
* Microsoft Learn: `Mapping, customizing, and transforming claims in ASP.NET Core`_
202212
* Microsoft .NET Blog: `ASP.NET Core Authentication with IdentityServer4 <https://devblogs.microsoft.com/dotnet/asp-net-core-authentication-with-identityserver4/>`_
203213

204214
Future
@@ -209,6 +219,14 @@ Please, open `Show and tell <https://github.com/ThreeMammals/Ocelot/discussions/
209219

210220
""""
211221

212-
.. [#f1] Use the ``AuthenticationProviderKeys`` property instead of ``AuthenticationProviderKey`` one. We support this ``[Obsolete]`` property for backward compatibility and migration reasons. In future releases, the property may be removed as a breaking change.
213-
.. [#f2] "`Multiple authentication schemes <https://learn.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme#use-multiple-authentication-schemes>`__" feature was requested in issues `740 <https://github.com/ThreeMammals/Ocelot/issues/740>`_, `1580 <https://github.com/ThreeMammals/Ocelot/issues/1580>`_ and delivered as a part of `23.0 <https://github.com/ThreeMammals/Ocelot/releases/tag/23.0.0>`_ release.
214-
.. [#f3] We would appreciate any new PRs to add extra acceptance tests for your custom scenarios with `multiple authentication schemes <https://learn.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme#use-multiple-authentication-schemes>`__.
222+
.. [#f1] ":ref:`authentication-scheme`" feature has been an Ocelot artifact for ages. Use the ``AuthenticationProviderKeys`` property instead of ``AuthenticationProviderKey`` one. We support this ``[Obsolete]`` property for backward compatibility and migration reasons. In future releases, the property may be removed as a breaking change.
223+
.. [#f2] ":ref:`authentication-multiple`" feature was requested in issues `740`_, `1580`_ and delivered as a part of `23.0`_ release.
224+
.. [#f3] We would appreciate any new pull requests to add extra acceptance tests for your custom scenarios with `multiple authentication schemes`_.
225+
.. [#f4] For a complete understanding of .NET 8 breaking change related to JWT tokens, please refer to the Microsoft Learn documentation: "`Security token events return a JsonWebToken <https://learn.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/8.0/securitytoken-events>`__".
226+
227+
.. _446: https://github.com/ThreeMammals/Ocelot/issues/446
228+
.. _740: https://github.com/ThreeMammals/Ocelot/issues/740
229+
.. _1580: https://github.com/ThreeMammals/Ocelot/issues/1580
230+
.. _23.0: https://github.com/ThreeMammals/Ocelot/releases/tag/23.0.0
231+
.. _Mapping, customizing, and transforming claims in ASP.NET Core: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/claims?view=aspnetcore-9.0
232+
.. _multiple authentication schemes: https://learn.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme#use-multiple-authentication-schemes

0 commit comments

Comments
 (0)