Skip to content

Premature disposal of WindowsIdentity in async controller method #255

Closed

Description

@avivanoff

I have a Web API that uses Integrated Windows authentication and the following controller:

[Authorize]
[RoutePrefix("identity")]
public sealed class IdentityController : ApiController
{
    [HttpGet]
    [Route("1")]
    public async Task<String> GetIdentity1Async(CancellationToken cancellationToken)
    {
        var identity = (WindowsIdentity)this.User.Identity;
        using (identity.Impersonate())
        {
            await Task.Delay(1, cancellationToken)
                      .ConfigureAwait(true);
        }

        return identity.Name;
    }

    [HttpGet]
    [Route("2")]
    public async Task<String> GetIdentity2Async(CancellationToken cancellationToken)
    {
        await Task.Delay(1, cancellationToken)
                  .ConfigureAwait(true);

        var identity = (WindowsIdentity)this.User.Identity;
        using (identity.Impersonate())
        {
            await Task.Delay(1, cancellationToken)
                      .ConfigureAwait(true);
        }

        return identity.Name;
    }
}

Why does GetAsync1Method works, while GetAsync2Method throws

System.ObjectDisposedException: 'Safe handle has been closed'

on return statement?

Activity

  1. mkArtakMSFT commented on Oct 13, 2021

    @mkArtakMSFT

    Hi. It looks like this is a question about how to use ASP.NET. While we do our best to look through all the issues filed here, we are not a general-purpose forum. To get a faster response we suggest posting your questions to StackOverflow.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions