Sea*_*ter 8 c# windows authentication winapi
我在codeproject上找到了一些允许用户模拟的示例代码.
此代码通过导入以下非托管Win32 API函数来工作:
[DllImport("advapi32.dll", SetLastError = true)]
private static extern int LogonUser(
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);
Run Code Online (Sandbox Code Playgroud)
这些函数用于模拟目标用户,然后执行某些操作,然后还原模拟上下文.冒充用户是这样实现的:
if ( LogonUser(userName, domainName, password, LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT, ref token ) != 0 )
{
if ( DuplicateToken( token, 2, ref tokenDuplicate ) != 0 )
{
tempWindowsIdentity = new WindowsIdentity( tokenDuplicate );
impersonationContext = tempWindowsIdentity.Impersonate();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试理解为什么此代码首先获取所需的令牌LogonUser
,然后在对复制的令牌执行模拟之前复制该令牌.为什么不模仿使用从LogonUser
方法中获得的令牌.
显然,写这篇文章的人比我更了解这一点,看起来我错过了一些东西.我能否解释为什么需要这个过程看似冗余的令牌复制步骤?
据我所知,传递给WindowsIdentity ctor的令牌应该是一个假冒令牌.所以,该代码的作者使用
DuplicateToken( token, 2, ref tokenDuplicate )
Run Code Online (Sandbox Code Playgroud)
从LogonUser()返回的主令牌创建模拟令牌.'2'幻数代表SECURITY_IMPERSONATION_LEVEL枚举的SecurityImpersonation成员.
链接:
http://msdn.microsoft.com/en-us/library/aa378184%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa379572%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa446616%28v=vs.85%29.aspx
归档时间: |
|
查看次数: |
3426 次 |
最近记录: |