dotnet / dotnet/aspnetcore

Avoid creating Context classes when Events have not been set

Open
#42,325 2 comments 1 reaction 0 assignees View on GitHub
area-auth Perf
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.