Avoid creating Context classes when Events have not been set
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
There are multiple events in `CookieAuthenticationHandler` and each has its own context type which some of them are created _per request_ even when there's no event has been set.
The similar approach that used in [this PR](https://github.com/dotnet/aspnetcore/pull/36193) could be used to only allocate the context types if the event has been set.
This code:
https://github.com/dotnet/aspnetcore/blob/9ac541b23b22882aebffcdc931c3575d33edcec8/src/Security/Authentication/Cookies/src/CookieAuthenticationHandler.cs#L200
would change to this:
```c#
if (Events.GetType() == typeof(CookieAuthenticationEvents)
&& Events.OnValidatePrincipal == CookieAuthenticationEvents._defaultOnValidatePrincipal)
{
await Events.ValidatePrincipal(context);
}
```
This would also apply to other authentication handlers.
Contributor guide
Assessment
This issue has not been assessed yet.