3

我需要通过 Directory.GetDirectories() 和 Directory.GetFiles() 方法访问当前 IPrincipal 可以访问的文件和目录,而不列出其他文件。该进程本身作为 NETWORK SERVICE 运行,因此在这些调用期间它必须将主体更改为当前用户(通过 IPrincipal)。

我试图在文件访问部分之前将 Thread.CurrentPrincipal 更改为新的 IPrincipal ,但它似乎没有任何区别。

还有什么我可以做的,还是我错过了什么?

4

3 回答 3

5

Windows 模拟通过使用登录详细信息获取用户令牌来解决此问题。然后可以使用此令牌获取 WindowsIdentity,然后使用该标识生成模拟上下文。在此上下文范围内,您可以作为模拟用户访问文件系统。

当然,您需要存储用户名和密码才能使这种方法起作用。

首先,定义从 Windows 获取用户令牌所需的 Windows API:

internal class WindowsAPI 
{
    public const int LOGON32_PROVIDER_DEFAULT = 0;
    public const int LOGON32_LOGON_INTERACTIVE = 2;

    [DllImport( "advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode )]
    public static extern bool LogonUser( String lpszUsername, 
        String lpszDomain, String lpszPassword,
        int dwLogonType, int dwLogonProvider, ref IntPtr phToken 
    );

    [DllImport( "kernel32.dll", CharSet = CharSet.Auto )]
    public extern static bool CloseHandle( IntPtr handle );
}

然后,使用这些 API 获取 WindowsIdentity:

private WindowsIdentity GetIdentity( string userName, string password )
{
    _userToken = IntPtr.Zero;

    if ( !WindowsAPI.LogonUser(
        userName,
        AbbGrainDomain,
        password,
        WindowsAPI.LOGON32_LOGON_INTERACTIVE, WindowsAPI.LOGON32_PROVIDER_DEFAULT,
        ref _userToken
    ) )
    {
        int errorCode = Marshal.GetLastWin32Error();
        throw new System.ComponentModel.Win32Exception( errorCode );
    }

    return new WindowsIdentity( _userToken );
}

最后,使用此身份生成模拟上下文:

public List<string> GetDirectories( string searchPath )
{
    using ( WindowsImpersonationContext wic = GetIdentity().Impersonate() )
    {
        var directories = new List<string>();

        var di = new DirectoryInfo( searchPath );
        directories.AddRange( di.GetDirectories().Select( d => d.FullName ) );

        return directories;
    }
}

最后,使用存储的 _userToken 使用 IDisposable 模式清理窗口句柄很重要:

if ( _userToken != IntPtr.Zero )
    WindowsAPI.CloseHandle( _userToken );
于 2009-04-06T00:31:34.953 回答
0

我不认为你这样做是正确的。您应该考虑使用模拟。例如,查看教程以了解如何执行此操作。

于 2009-03-31T23:46:40.830 回答
-1

您可以使用 DllImport 和 LogonUser win32 API 来模拟另一个用户。

于 2009-04-01T01:17:51.690 回答