Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vNext. AspNet.Identity and custom UserStore. UserStore disposed exception

I'm trying to understand vNext.
I wrote custom UserStore, that works with MongoDB and implements these interfaces:

  public class UserStore : IUserStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>, IUserSecurityStampStore<ApplicationUser>,
    IUserLoginStore<ApplicationUser>, IUserClaimStore<ApplicationUser>, IUserEmailStore<ApplicationUser>, IUserRoleStore<ApplicationUser>,
    IUserTwoFactorStore<ApplicationUser>

In Startup.cs added:

app.UseServices(services =>
        {

            services.AddIdentity<ApplicationUser>()
                .AddUserStore(() => { return new UserStore(); })
                .AddUserManager<UserManager<ApplicationUser>>()
                .AddHttpSignIn();

            services.AddMvc();
        });

Then tried to use unchanged AccountController from Visual Studio template and have troubles.
When signing in i getting ObjectDisposedException in UserStore.FindByNameAsync() -- something called UserStore.Dispose().
In UserManager code on github.com/aspnet Store.Dispose() called only in UserManager.Dispose().
I can just ignore calls of Dispose and all works fine, but this is not good way.
So i have no ideas what shall i do

P.S. The Question is: what (and why) can call UserStore.Dispose()?

like image 979
no_one Avatar asked Nov 10 '22 02:11

no_one


1 Answers

In vNext, DI is built in and manages the lifetime of the identity services. You are probably trying to use identity after the services have been disposed, by default identity services have lifetimes scoped to a request, so if for example, you are trying to hang onto a reference to a user manager and reuse it across multiple requests, that would cause the ObjectDisposedException.

like image 166
Hao Kung Avatar answered Nov 15 '22 06:11

Hao Kung