【问题标题】:Still getting duplicate token error after calling DuplicateTokenEx for impersonated token为模拟令牌调用 DuplicateTokenEx 后仍然出现重复令牌错误
【发布时间】:2012-10-31 20:21:38
【问题描述】:

我正在尝试从服务调用中返回Sytem.IntPtr,以便客户端可以使用模拟来调用某些代码。如果不从 WCF 服务传回令牌,我的模拟代码可以正常工作。我不确定为什么这不起作用。我收到以下错误:

“用于模拟的令牌无效 - 不能复制。”

这是我的代码,确实可以工作,除非我尝试将令牌从服务传递回 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,我应该能够创建一个对委托有效的令牌,并在远程系统的上下文中使用。当使用“SecurityDelegation”时,服务器进程可以在远程系统上模拟客户端的安全上下文。

谢谢!

【问题讨论】:

  • 你搞定了吗?
  • 不,我什至没有反映这种情况的代码。
  • 感谢康威。

标签: c# impersonation


【解决方案1】:

您使用的值 TokenAccessLevels.MaximumAllowed 好像它是允许的最大权限,这意味着:好像它会给您所有权限...

...但事实并非如此。

TokanAccesslevels.MaximumAllowed 的值为 0x02000000,这是枚举可以随时增长到的最大值,作为未来实现的参考。

有关枚举的实际实现,请参阅this page

要使您的代码正常工作,您需要通过按位操作一一设置所需的访问级别。

如果你用 MAXIMUM_ALLOWED 代替,代码肯定会运行

(uint)(TokenAccessLevels.Query | TokenAccessLevels.Duplicate | TokenAccessLevels.Impersonate)

在 DuplicateTokenEx 函数的 PInvoke 调用中。

我知道你不再有这个问题了,但这可以帮助其他人,毕竟你可能会再次遇到同样的问题...... ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    相关资源
    最近更新 更多