Skip to content

DbContext Object disposed exception at OnTokenValidated method #1992

Open

Description

Microsoft.Identity.Web Library

Microsoft.Identity.Web.UI

Microsoft.Identity.Web version

1.25.0

Web app

Not Applicable

Web API

Not Applicable

Token cache serialization

Not Applicable

Description

I was using version 1.4.0 of Microsoft.Identity.Web and now while part of upgrading to .net7 I decided to updated to latest 1.25.0
version. I was unable to login and while debugging I figure out that problem was that EF core dbContext gets disposed although used to work previously. This is relavant part of my code:

 _ = services.Configure((Action<MicrosoftIdentityOptions>)(options =>
        {
            var onTokenValidated = options.Events.OnTokenValidated;
            options.Events = new OpenIdConnectEvents
            {
                OnTokenValidated = async context =>
                {
                    await onMicrosoftLoginSucceeded(context);
                    await onTokenValidated(context);
                }
            };
        }));

        _ = services.AddMicrosoftIdentityWebAppAuthentication(config);

and this is mine onMicrosoftLoginSucceeded method:
            var user = (ClaimsIdentity?)context.Principal?.Identity;
            var email = user?.Name;
            var userManager = context.HttpContext.RequestServices.GetService<UserManager<User>>()!;

           var user=userManager.FindByEmailAsync(email);
...

Altought I had version 1.4.0 I was updating one by one (last one it worked for me was 1.18.0)
1.20 and 1.22 shown some Derived method 'GetBaseConfigurationAsync' error so not sure where exactly at which version this difference in behaviour was introduced

Reproduction steps

  1. Use dbContext inside OnTokenValidated event
  2. Use version 1.25 of this lib but this issue might exist in some earlier version (1.18 works, was unable to test on 1.20 and any between because of some other error)
  3. no step 3

Error message

No response

Id Web logs

No response

Relevant code snippets

_ = services.Configure((Action<MicrosoftIdentityOptions>)(options =>
        {
            var onTokenValidated = options.Events.OnTokenValidated;
            options.Events = new OpenIdConnectEvents
            {
                OnTokenValidated = async context =>
                {
                    await onMicrosoftLoginSucceeded(context);
                    await onTokenValidated(context);
                }
            };
        }));

        _ = services.AddMicrosoftIdentityWebAppAuthentication(config);

and this is mine onMicrosoftLoginSucceeded method:
            var user = (ClaimsIdentity?)context.Principal?.Identity;
            var email = user?.Name;
            var userManager = context.HttpContext.RequestServices.GetService<UserManager<User>>()!;

           var user=userManager.FindByEmailAsync(email);
...

Regression

1.18

Expected behavior

To have consisent behaviour between versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions