-
Notifications
You must be signed in to change notification settings - Fork 862
UserManager depends on IHttpContextAccessor and It behaves incorrectly when lifetimes don't match up #1010
Comments
|
Thanks, I can remember that thread now. That makes sense.
At least for now but I believe the way that More importantly, the other annoying thing is the fact that |
So there has always been an implicit assumption that the mangers are scoped. @divega do you see any problem with this assumption? We haven't really tried to enable things like singleton lifetimes.. |
Yes, we assume the managers are scoped and I don't think we ever intended them to be used with a different lifetime. Just a few thoughts:
|
#655 drove why we made it optional |
Its required for SIgnInManager for sure, since that can't work without an HttpContext. We could(should?) have made it optional for RoleManager as well. |
@HaoK thanks for refreshing my memory. Let's chat about this in person in triage. From reading #655 and linked issues it seems to me that if it is was valid to use |
Sure but in that issue it seems more that we don't really care too much if the functionality is lost, so there's certainly no good reason to add a billion overloads to pass around cancellation tokens on the manager APIs. |
We could do something cheap like a new setter on the managers to set your own CancellationToken which would get used instead of the HttpContext's as a cheap way to enable this fringe scenario... but I don't think many (any?) people would use this feature if we added it... |
@divega It's not much of an issue from the consumption point of view when you know UserManager uses it. However, it's not intuitive at the first. I was surprised to see UM was caring about request abort. So, it's not a strong case to request it to be removed. However, I would love to hear what made you guys to remove |
TL;DR; is that
UserManager
depends on a per-request instance dependency,IHttpContextAccessor
, and it can construct itself successfully even if it's (theUserManager
) is registered as singleton.What Happened?
I was seeing
System.OperationCanceledException
while working with ASP.NET Core Identity (see tugberkugurlu/AspNetCore.Identity.MongoDB#15 for more context). After digging into the issue for a while, I discovered this line: https://github.com/aspnet/Identity/blob/rel/1.1.0-preview1/src/Microsoft.AspNetCore.Identity/UserManager.cs#L46 which made sense and I started belamng my reverse proxy causing requests to be aborted in the middle of the operations. However, it didn't take long to understand it was not the case at all.The actual problem was related to the fact that I was registering the
UserManager
as singleton (see https://github.com/tugberkugurlu/AspNetCoreSamples/blob/a4004c851dfbf84fa92891b0d8ef54fa822c6ff6/haproxy-redis-auth/src/Startup.cs#L113) and whereas it was relying on a per-request dependency in a very wrong way 😞 (see https://github.com/aspnet/Identity/blob/rel/1.1.0-preview1/src/Microsoft.AspNetCore.Identity/UserManager.cs#L98)So, as expected, the
CancellationToken
property of UserManager entered into the canceled state right after the first request finished, which caused all subsequent calls to the store implementation to receive an already cancelled cancellation token.What Should Happen Instead
It should have blown up at the startup complaining about the fact that a single instance registration is relying on an instance which is registered with a shorter-lived scoped (per-request).
I am not sure whether it's OK to classify this is a bug but to me, it seems like it really needs fixing ASAP because it's so annoying to debug and understand when you hit this 😞
The text was updated successfully, but these errors were encountered: