Identity: SignInManager PasswordSignInAsync exception: Cannot access a disposed object. Object name: 'UserManager1'.`

Created on 31 May 2016  路  2Comments  路  Source: aspnet/Identity

i'm using asp.net rc2 web api mvc ,
when i try to login i have error on code:

var result = await _signInManager.PasswordSignInAsync(vm.UserName, vm.Password, true , lockoutOnFailure: false);

Cannot access a disposed object. Object name: 'UserManager1'.`

at Microsoft.AspNetCore.Identity.UserManager1.ThrowIfDisposed() at Microsoft.AspNetCore.Identity.UserManager1.FindByNameAsync(String userName)
at Microsoft.AspNetCore.Identity.SignInManager1.<PasswordSignInAsync>d__33.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at WebApplication5.Controllers.AuthController.d__3.MoveNext()

Login:

   [HttpPost]
    public async void Post([FromBody]UserViewModel vm)
    {
        if (ModelState.IsValid)
        {
             //   var user = await _userManager.FindByNameAsync(vm.UserName);
                var result = await   _signInManager.PasswordSignInAsync(vm.UserName,vm.Password, true , lockoutOnFailure: false); // *** EXCEPTION HERE
                if (result.Succeeded)
                {
                }
                else
                {
                }
        }
    }

ConfigureServices:

       services.AddIdentity<ApplicationUser, IdentityRole>(config =>
        {
            config.User.RequireUniqueEmail = true;
            config.Password.RequiredLength = 8;
            //config.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
            config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents()
            {
                OnRedirectToLogin = ctx =>
                {
                    if (ctx.Request.Path.StartsWithSegments("/api") &&
                                   ctx.Response.StatusCode == (int)HttpStatusCode.OK)
                    {
                        ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                    }
                    else
                    {
                        ctx.Response.Redirect(ctx.RedirectUri);
                    }
                   return Task.FromResult(0);
                }
            };
        })
        .AddEntityFrameworkStores<Dev_InfoCRM_WinContext>()
        .AddDefaultTokenProviders ();

Configure:
app.UseIdentity();

Most helpful comment

It's a race condition, caused by the fact you're using async void (which is basically fire-and-forget async).

Replace public async void Post([FromBody]UserViewModel vm) by public async Task Post([FromBody]UserViewModel vm) and it should work.

All 2 comments

It's a race condition, caused by the fact you're using async void (which is basically fire-and-forget async).

Replace public async void Post([FromBody]UserViewModel vm) by public async Task Post([FromBody]UserViewModel vm) and it should work.

Yep what @PinpointTownes said :)

Was this page helpful?
0 / 5 - 0 ratings