Skip to content

2.15.1 must wait all OnTokenValidated delegates to completion #2513

@recumbented

Description

@recumbented

Microsoft.Identity.Web Library

Microsoft.Identity.Web

Microsoft.Identity.Web version

2.15.1

Web app

Sign-in users

Web API

Protected web APIs (validating tokens)

Token cache serialization

In-memory caches

Description

Related to #2508

Since 2.15.1, OnTokenValidated is used as MulticastDelegate.
So await _onTokenValidated(context); will not wait it's invocations.
This causes unexpected behavior in users's OnTokenValidated invocation.

It's desired to wait all invocations like

await Task.WhenAll(((MulticastDelegate)_onTokenValidated)
	.GetInvocationList()
	.Cast<Func<TokenValidatedContext, Task>>()
	.Select(t=>t(context)));

Reproduction steps

If you use scoped services from DI in OnTokenValidated,
async method invocation may throw ObjectDisposedException.

Error message

No response

Id Web logs

No response

Relevant code snippets

options.Events.OnTokenValidated = async context =>
{
    var dep = context.HttpContext.RequestServices.GetRequiredService<Service>();
    await Task.Delay(1000);
    await dep.UseResourceAsync(); //Out of DI scope at the time. ObjectDisposedException may be thrown.
}


### Regression

_No response_

### Expected behavior

All OpenIdConnectEvents delegates must be awaited to completion.

Metadata

Metadata

Assignees

Labels

questionFurther information is requestedregressionregression between Microsoft Identity Web versions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions