在ASP.NET中,WindowsIdentity
不会自动流动AspNetSynchronizationContext
,不像说Thread.CurrentPrincipal
。每次ASP.NET进入一个新的线程池时,模拟上下文都会被保存,并将其设置为应用程序池用户的here。当ASP.NET离开该线程时,它将被恢复为here。作为延续回调调用的一部分(由AspNetSynchronizationContext.Post
排队的调用),这也会发生在await
延续中。因此,如果您希望跨越ASP.NET中多个线程的等待事件来保持身份,则需要手动进行流式处理。你可以使用本地或类成员变量。或者,你可以通过logical call context,.NET 4.6 AsyncLocal<T>
或类似的东西Stephen Cleary's AsyncLocal
。
或者,您的代码将工作,如果你使用ConfigureAwait(false)
预期:
await Task.Delay(1).ConfigureAwait(false);
(注意:虽然你会在这种情况下失去了HttpContext.Current
。)
上面会的工作,因为在没有同步上下文,WindowsIdentity
确实流过了await
。它几乎流入the same way as Thread.CurrentPrincipal
does,即跨越并进入异步调用(但不在这些异步调用之外)。我相信这是作为SecurityContext
流程的一部分完成的,该流程本身是ExecutionContext
的一部分,并显示相同的写入时复制行为。
为了支持这一说法,我做了一个小实验用控制台应用程序:
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static async Task TestAsync()
{
ShowIdentity();
// substitute your actual test credentials
using (ImpersonateIdentity(
userName: "TestUser1", domain: "TestDomain", password: "TestPassword1"))
{
ShowIdentity();
await Task.Run(() =>
{
Thread.Sleep(100);
ShowIdentity();
ImpersonateIdentity(userName: "TestUser2", domain: "TestDomain", password: "TestPassword2");
ShowIdentity();
}).ConfigureAwait(false);
ShowIdentity();
}
ShowIdentity();
}
static WindowsImpersonationContext ImpersonateIdentity(string userName, string domain, string password)
{
var userToken = IntPtr.Zero;
var success = NativeMethods.LogonUser(
userName,
domain,
password,
(int)NativeMethods.LogonType.LOGON32_LOGON_INTERACTIVE,
(int)NativeMethods.LogonProvider.LOGON32_PROVIDER_DEFAULT,
out userToken);
if (!success)
{
throw new SecurityException("Logon user failed");
}
try
{
return WindowsIdentity.Impersonate(userToken);
}
finally
{
NativeMethods.CloseHandle(userToken);
}
}
static void Main(string[] args)
{
TestAsync().Wait();
Console.ReadLine();
}
static void ShowIdentity(
[CallerMemberName] string callerName = "",
[CallerLineNumber] int lineNumber = -1,
[CallerFilePath] string filePath = "")
{
// format the output so I can double-click it in the Debuger output window
Debug.WriteLine("{0}({1}): {2}", filePath, lineNumber,
new { Environment.CurrentManagedThreadId, WindowsIdentity.GetCurrent().Name });
}
static class NativeMethods
{
public enum LogonType
{
LOGON32_LOGON_INTERACTIVE = 2,
LOGON32_LOGON_NETWORK = 3,
LOGON32_LOGON_BATCH = 4,
LOGON32_LOGON_SERVICE = 5,
LOGON32_LOGON_UNLOCK = 7,
LOGON32_LOGON_NETWORK_CLEARTEXT = 8,
LOGON32_LOGON_NEW_CREDENTIALS = 9
};
public enum LogonProvider
{
LOGON32_PROVIDER_DEFAULT = 0,
LOGON32_PROVIDER_WINNT35 = 1,
LOGON32_PROVIDER_WINNT40 = 2,
LOGON32_PROVIDER_WINNT50 = 3
};
public enum ImpersonationLevel
{
SecurityAnonymous = 0,
SecurityIdentification = 1,
SecurityImpersonation = 2,
SecurityDelegation = 3
}
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
out IntPtr phToken);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
}
}
}
更新,如@PawelForys建议在评论中,另一种选择流动模拟环境是自动在全局文件
aspnet.config
中使用
<alwaysFlowImpersonationPolicy enabled="true"/>
(如果需要,也可使用
<legacyImpersonationPolicy enabled="false"/>
,例如
HttpWebRequest
)。
你已经模拟了一些随机线程池线程。运行它的下一个请求可能会受此影响。超级危险。 – usr
@usr,因为它变成了,除非你冒充类似'UnsafeQueueUserWorkItem'这样的东西,否则它并不是那么危险。否则,身份得到正确的传播和恢复,它不会留在池线程中。看[这个小实验](https://gist.github.com/noserati/940c21b488e59d502dd1),特别是'GoThruThreads'。在ASP.NET中更安全,请检查我的更新。 – Noseratio
@Noseratio很高兴知道。 – usr