我正在尝试从服务调用返回Sytem.IntPtr
,以便客户端可以使用模拟来调用某些代码。如果没有从 WCF 服务传回 token ,我的模拟代码可以正常工作。我不知道为什么这不起作用。我收到以下错误:
"Invalid token for impersonation - it cannot be duplicated."
这是我的代码,除了当我尝试将 token 从服务传递回 WinForm C# 客户端然后进行模拟时,该代码确实工作。
[DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx")]
public extern static bool DuplicateTokenEx(IntPtr ExistingTokenHandle, uint dwDesiredAccess, ref SECURITY_ATTRIBUTES lpThreadAttributes, int TokenType, int ImpersonationLevel, ref IntPtr DuplicateTokenHandle);
private IntPtr tokenHandle = new IntPtr(0);
private IntPtr dupeTokenHandle = new IntPtr(0);
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int Length;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
public enum SecurityImpersonationLevel
{
SecurityAnonymous = 0,
SecurityIdentification = 1,
SecurityImpersonation = 2,
SecurityDelegation = 3
}
public enum TokenType
{
TokenPrimary = 1,
TokenImpersonation = 2
}
private const int MAXIMUM_ALLOWED = 0x2000000;
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public System.IntPtr GetWindowsUserToken(string UserName, string Password, string DomainName)
{
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupTokenHandle = new IntPtr(0);
const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;
//Initialize the token handle
tokenHandle = IntPtr.Zero;
//Call LogonUser to obtain a handle to an access token for credentials supplied.
bool returnValue = LogonUser(UserName, DomainName, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);
//Make sure a token was returned; if no populate the ResultCode and throw an exception:
int ResultCode = 0;
if (false == returnValue)
{
ResultCode = Marshal.GetLastWin32Error();
throw new System.ComponentModel.Win32Exception(ResultCode, "API call to LogonUser failed with error code : " + ResultCode);
}
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
sa.bInheritHandle = true;
sa.Length = Marshal.SizeOf(sa);
sa.lpSecurityDescriptor = (IntPtr)0;
bool dupReturnValue = DuplicateTokenEx(tokenHandle, MAXIMUM_ALLOWED, ref sa, (int)SecurityImpersonationLevel.SecurityDelegation, (int)TokenType.TokenImpersonation, ref dupTokenHandle);
int ResultCodeDup = 0;
if (false == dupReturnValue)
{
ResultCodeDup = Marshal.GetLastWin32Error();
throw new System.ComponentModel.Win32Exception(ResultCode, "API call to DuplicateToken failed with error code : " + ResultCode);
}
//Return the user token
return dupTokenHandle;
}
知道我是否没有正确使用对 DuplicateTokenEx
的调用吗?根据我阅读的MSDN文档here我应该能够创建一个对委托(delegate)有效的 token ,并在远程系统的上下文中使用。当使用“SecurityDelegation”时,服务器进程可以模拟远程系统上客户端的安全上下文。
谢谢!
最佳答案
您使用的值 TokenAccessLevels.MaximumAllowed 就好像它是允许的最大权限一样,这意味着:就好像它会给您所有权限...
...但事实并非如此。
TokanAccesslevels.MaximumAllowed 的值为 0x02000000,这是枚举可以随时增长到的最大值,为将来的实现提供引用。
请参阅this page用于枚举的实际实现。
为了让您的代码正常工作,您需要通过按位运算一一设置所需的访问级别。
如果您将 MAXIMUM_ALLOWED 替换为 ,代码肯定会运行
(uint)(TokenAccessLevels.Query | TokenAccessLevels.Duplicate | TokenAccessLevels.Impersonate)
在 PInvoke 调用 DuplicateTokenEx 函数中。
我知道您不再遇到这个问题,但这可以帮助其他人,毕竟您可能会再次遇到同样的问题...;)
关于c# - 为模拟 token 调用 DuplicateTokenEx 后仍然出现重复 token 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13166848/