Skip to content

Commit ef15079

Browse files
committed
Added sample code for removing a claim using the OnTicketReceived event
1 parent 6d180f0 commit ef15079

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

  • astro/src/content/docs/identityserver/troubleshooting

astro/src/content/docs/identityserver/troubleshooting/index.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,30 @@ services.AddAuthentication()
301301

302302
When dealing with external authentication, you may want to implement `OnTicketReceived` to reduce the size of the cookie. This is a callback that is invoked after the external authentication process is complete. You can use this callback to remove any claims that are not needed by your solution.
303303

304+
```csharp
305+
// Program.cs
306+
services.AddAuthentication()
307+
.AddOpenIdConnect("oidc", options =>
308+
{
309+
...
310+
311+
options.Events = new OpenIdConnectEvents()
312+
{
313+
OnTicketReceived = context =>
314+
{
315+
var identities = context.Principal?.Identities ?? Enumerable.Empty<ClaimsIdentity>();
316+
foreach (var identity in identities)
317+
{
318+
var removedClaim = identity.FindFirst("unused-custom-claim");
319+
_ = identity.TryRemoveClaim(removedClaim);
320+
}
321+
322+
return Task.CompletedTask;
323+
}
324+
};
325+
});
326+
```
327+
304328
### Use Server-side Sessions
305329

306330
You can use [server-side sessions][2] to store the

0 commit comments

Comments
 (0)