为什么 System.IO.Directory.Delete(string,bool) 仅将文件夹标记为已删除,当 asp.net 站点模拟用户时

Why System.IO.Directory.Delete(string,bool) only markes a folder as deleted, when a asp.net site impersonates a user

我的 asp.net 4.0 Application 有一个奇怪的行为,因为我已经更新到 Windows10(我认为它是 10)。

我有一个在 IIS 中使用 BasicAuthentication 的应用程序 - 在我的 Login.aspx 中,我手动 verifying userpassword 反对定义的 AD 域。 如果凭据有效,我会在会话中存储一个包含一些用户数据的简单对象并重定向到我的主页。

到目前为止一切顺利 - 使用该应用程序,用户可以删除文件和目录。这些操作总是performed under当前用户的impersonation context

输入"Impersonation Context":(仅主要部分)

[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public virtual void Enter(LogonType logonType = LogonType.LOGON32_LOGON_INTERACTIVE, LogonProvider provider = LogonProvider.LOGON32_PROVIDER_DEFAULT)
{       
    token = IntPtr.Zero;

    //Create the token
    bool logonSuccessfull = GetToken(this.username, this.password, this.domain, ref token, logonType, provider);

    WindowsIdentity identity;
    identity = new WindowsIdentity(token);
    impContext = identity.Impersonate();                               
}

如果用户删除目录:

Public void Delete(string directory)
{
    //1. Entering impersonation context before (context.Enter();)

    //2. Delete the file (executing this basic .net method):
      System.IO.Directory.Delete(directory, true);

    //3. Leaving the impersonation context after (context.Leave() -> .Undo();)
}

在那次操作之后 i can still see the directory 在资源管理器中以管理员身份(之前已关闭)。但是如果我想打开文件夹,我会收到 access denied 消息。我也在not able to看目录的权限或者become the owner这个'ghost-folder'。快速文件系统检查也无济于事。

但是: 如果 applicationpool ends - 文件夹不见了...

Applicationpool 是一个 Classic .net 4.0Network-Identity(更改此设置并未解决此时的问题)

有谁知道为什么他们没有立即被删除? 以及如何强制执行?

至少我找到了问题所在。 问题是,在某些情况下我调用了 Impersonation twice or more。 所以我已经处于模拟状态,再次调用模拟代码...... 这在 Windows 10 之前不是问题,但在 10 之后似乎......