|
| 1 | +--- |
| 2 | +title: Configuring OAuth 2.0 token introspection |
| 3 | +description: Learn more about the various options when adding the ASP.NET Core authentication handler for OAuth 2.0 token introspection. |
| 4 | +sidebar: |
| 5 | + label: Options |
| 6 | + order: 2 |
| 7 | +--- |
| 8 | + |
| 9 | +#### OAuth2IntrospectionOptions |
| 10 | + |
| 11 | +`OAuth2IntrospectionOptions` is the options class to configure the ASP.NET Core authentication handler for OAuth 2.0 token introspection. |
| 12 | + |
| 13 | +You set the options when registering the authentication handler at startup time, using a lambda expression in the `AddOAuth2Introspection` method: |
| 14 | + |
| 15 | +```csharp |
| 16 | +// Program.cs |
| 17 | +builder.Services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme) |
| 18 | + .AddOAuth2Introspection(options => |
| 19 | + { |
| 20 | + // configure options here.. |
| 21 | + }); |
| 22 | +``` |
| 23 | + |
| 24 | +## Main |
| 25 | + |
| 26 | +Top-level settings. Available directly on the `OAuth2IntrospectionOptions` class. |
| 27 | + |
| 28 | +* **`Authority`** |
| 29 | + |
| 30 | +The URL of the token server. When configured, the handler will use this URI to discover the introspection endpoint. |
| 31 | + |
| 32 | +* **`IntrospectionEndpoint`** |
| 33 | + |
| 34 | +Sets the URL of the introspection endpoint. If this is set, the `Authority` will not be used to discover the introspection endpoint. |
| 35 | + |
| 36 | +* **`ClientId`** |
| 37 | + |
| 38 | +Specifies the ID of the introspection client. This setting is required. |
| 39 | + |
| 40 | +* **`ClientSecret`** |
| 41 | + |
| 42 | +Specifies the shared secret of the introspection client. |
| 43 | + |
| 44 | +* **`ClientCredentialStyle`** |
| 45 | + |
| 46 | +Specifies how the client credentials are sent to the introspection endpoint. The default is `ClientCredentialStyle.PostBody`, which sends the credentials in the body of the request. |
| 47 | +You can also set this to `ClientCredentialStyle.AuthorizationHeader` to send the credentials in the `Authorization` header as a Basic authentication scheme. |
| 48 | + |
| 49 | +* **`ClientCredentialStyle `** |
| 50 | + |
| 51 | +Specifies how the authorization header is formatted when used. The default is `BasicAuthenticationHeaderStyle.Rfc2617`, which formats the header according to the [original basic authentication spec](https://tools.ietf.org/html/rfc2617#section-2). |
| 52 | +You can also set this to `BasicAuthenticationHeaderStyle.Rfc6749`, which formats the header according to [RFC 6749](https://tools.ietf.org/html/rfc6749#section-2.3.1). |
| 53 | + |
| 54 | +* **`TokenTypeHint`** |
| 55 | + |
| 56 | +Specifies the token type hint of the introspection client. Defaults to `"access_token"`. |
| 57 | + |
| 58 | +* **`NameClaimType`** |
| 59 | + |
| 60 | +Specifies the claim type to use for the name claim. Defaults to `"name"`. |
| 61 | + |
| 62 | +* **`RoleClaimType`** |
| 63 | + |
| 64 | +Specifies the claim type to use for the role claim. Defaults to `"role"`. |
| 65 | + |
| 66 | +* **`AuthenticationType`** |
| 67 | + |
| 68 | +Specifies the authentication type to use for the authenticated identity. If not set, the authentication scheme name is used as the authentication type. |
| 69 | +Defaults to `null`. |
| 70 | + |
| 71 | +* **`DiscoveryPolicy`** |
| 72 | + |
| 73 | +Specifies the policy used for the discovery client. |
| 74 | + |
| 75 | +* **`SkipTokensWithDots`** |
| 76 | + |
| 77 | +Specifies whether to skip tokens that contain dots (`.`) in the introspection request. Defaults to `false`. |
| 78 | + |
| 79 | +* **`SaveToken`** |
| 80 | + |
| 81 | +Specifies whether the token should be stored in the context, so it is available for the duration of the HTTP request. Defaults to `true`. |
| 82 | + |
| 83 | +* **`EnableCaching`** |
| 84 | + |
| 85 | +Specifies whether the outcome of the token validation should be cached. |
| 86 | + |
| 87 | +This reduces the number of requests to the introspection endpoint, improving performance and reducing load on the authorization server. |
| 88 | +Defaults to `false`. |
| 89 | + |
| 90 | +* **`CacheDuration`** |
| 91 | + |
| 92 | +Specifies for how long the outcome of the token validation should be cached. Defaults to `TimeSpan.FromMinutes(5)`. |
| 93 | + |
| 94 | +* **`CacheKeyPrefix`** |
| 95 | + |
| 96 | +Specifies the prefix to use for the cache key. Defaults to `string.Empty`. |
| 97 | + |
| 98 | +* **`CacheKeyGenerator`** |
| 99 | + |
| 100 | +Specifies the method to use for generating the cache key. |
| 101 | +Defaults to `CacheUtils.CacheKeyFromToken`, which generates a cache key using the configured `CacheKeyPrefix` combined with the SHA-256 hash from the token. |
| 102 | + |
| 103 | +* **`TokenRetriever`** |
| 104 | + |
| 105 | +Specifies the method to use for retrieving the token from the HTTP request. |
| 106 | + |
| 107 | +Defaults to `TokenRetrieval.FromAuthorizationHeader`, which retrieves the token from the `Authorization` header. |
| 108 | +You can also set this to `TokenRetrieval.FromQueryString`, which retrieves the token from the query string, or use a custom method. |
| 109 | + |
| 110 | +* **`Events`** |
| 111 | + |
| 112 | +Gets or sets the `OAuth2IntrospectionEvents` instance used to handle authentication events. |
| 113 | + |
| 114 | +## Events |
| 115 | + |
| 116 | +The `OAuth2IntrospectionEvents` class allows you to handle various events during the authentication process. |
| 117 | +You can override methods to customize the behavior of the authentication handler, or set custom logic for specific events like `OnTokenValidated`, `OnAuthenticationFailed`, etc. |
| 118 | + |
| 119 | +* **`OnTokenValidated`** |
| 120 | + |
| 121 | +This event is triggered when the token has been successfully validated. You can use this to add additional claims or perform custom logic after the token validation. |
| 122 | + |
| 123 | +* **`OnAuthenticationFailed`** |
| 124 | + |
| 125 | +This event is triggered when exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. |
| 126 | + |
| 127 | +* **`OnTokenValidated`** |
| 128 | + |
| 129 | +This event is triggered after the security token has passed validation and a `ClaimsIdentity` has been generated. |
| 130 | + |
| 131 | +* **`OnUpdateClientAssertion`** |
| 132 | + |
| 133 | +This event is triggered when client assertion need to be updated. |
| 134 | + |
| 135 | +* **`OnSendingRequest`** |
| 136 | + |
| 137 | +This event is triggered when sending the token introspection request. |
| 138 | + |
| 139 | +* **`AuthenticationFailed`** |
| 140 | + |
| 141 | +Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. |
| 142 | + |
| 143 | +* **`TokenValidated`** |
| 144 | + |
| 145 | +Invoked after the security token has passed validation and a ClaimsIdentity has been generated. |
| 146 | + |
| 147 | +* **`UpdateClientAssertion`** |
| 148 | + |
| 149 | +Invoked when client assertion need to be updated. |
| 150 | + |
| 151 | +* **`SendingRequest`** |
| 152 | + |
| 153 | +Invoked when sending the token introspection request. |
0 commit comments