我最终找到了一个助手类,几年前我做过类似的事情。助手实现 IDisposable,因此只需将文件访问代码包装在“使用”中,如下所示:-
using (Impersonate imp = new Impersonate())
{
// Code in here will run under the identity specified in the helper
}
这是帮助程序类的代码(我删除了“使用”以节省一些空间)。您会注意到用户帐户在构造函数中是硬编码的:
internal class Impersonate : IDisposable
{
private const int LOGON32_LOGON_INTERACTIVE = 2;
private const int LOGON32_PROVIDER_DEFAULT = 0;
private bool _disposed = false;
private WindowsImpersonationContext _context = null;
[DllImport("advapi32.dll")]
private static extern int LogonUserA(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool CloseHandle(IntPtr handle);
internal Impersonate()
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
string domain = "<whatever>";
string username = "<whatever>";
string password = "<whatever>";
try
{
if (RevertToSelf())
{
if (LogonUserA(username, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
_context = tempWindowsIdentity.Impersonate();
}
}
}
}
finally
{
if (token != IntPtr.Zero)
{
CloseHandle(token);
}
if (tokenDuplicate != IntPtr.Zero)
{
CloseHandle(tokenDuplicate);
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// Dispose any managed resources here.
}
// Stop the impersonation.
_context.Undo();
_disposed = true;
}
}
}
试一试,让我知道你的进展如何......
安德鲁