文章编号: 319615 - 最后修改: 2007年2月12日 - 修订: 3.3

PRB: 无法模拟用户"时出现错误信息使用 WindowsIdentity.Impersonate 方法

展开全部 | 关闭全部

症状

使用 System.Security.Principal.WindowsIdentity.Impersonate 方法时, 您可能会收到以下错误消息:
无法模拟用户。

原因

WindowsIdentity 对象创建时使用的令牌类型因发生此问题。 您会收到错误消息,如果该标记不是主令牌褰撴偍灏濊瘯模拟"症状"一节中列出的。 您必须具有模拟令牌,WindowsIdentity.Impersonate 方法才能正常工作。

解决方案

若要从模拟令牌中获取主令牌,使用 DuplicateToken Win32 函数。

状态

此 bug 已在 Microsoft.net 框架类中更正库 1.1。

更多信息

浣跨敤 Visual C# 绀轰緥浠 g 爜閲嶇幇閿欒娑堟伅后面。 此代码会导致令牌是主令牌创建的对 LogonUser 函数传递 LOGON32_LOGON_INTERACTIVE 值。 此代码还将 SecurityImpersonation 元素传递给 DuplicateToken 函数。 这允许将模拟标记重复的标记。
using System;
using System.Security.Principal;
using System.Runtime.InteropServices;

public class LogonUserNS
{
    [DllImport("advapi32.dll", SetLastError=true)]
    public extern static 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);

    [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
    public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, 
        int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);

    public static void Main(string[] args)
    {               
        IntPtr tokenHandle = new IntPtr(0);
        IntPtr dupeTokenHandle = new IntPtr(0);

        try
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: DomainName UserName Password");
                return;
            }
            // args[0] - DomainName
            // args[1] - UserName
            // args[2] - Password

            const int LOGON32_PROVIDER_DEFAULT = 0;
            //This parameter causes LogonUser to create a primary token.
            const int LOGON32_LOGON_INTERACTIVE = 2;
            const int SecurityImpersonation = 2;

            tokenHandle = IntPtr.Zero;
            dupeTokenHandle = IntPtr.Zero;

            // Call LogonUser to obtain an handle to an access token.
            bool returnValue = LogonUser(args[1], args[0], args[2], 
                LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
                ref tokenHandle);
            
            if (false == returnValue)
            {
                Console.WriteLine("LogonUser failed with error code : {0}",
                    Marshal.GetLastWin32Error());
                return;
            }

            // Check the identity.
            Console.WriteLine("Before impersonation: "
                + WindowsIdentity.GetCurrent().Name);
        
            // The token that is passed to the following constructor must 
            // be a primary token to impersonate.
            WindowsIdentity newId = new WindowsIdentity(tokenHandle);
            WindowsImpersonationContext impersonatedUser = newId.Impersonate();

            // Check the identity.
            Console.WriteLine("After impersonation: "
                + WindowsIdentity.GetCurrent().Name);
    
            // Stop impersonating.
            impersonatedUser.Undo();

            // Check the identity.
            Console.WriteLine("After Undo: " + WindowsIdentity.GetCurrent().Name);
        
            // Free the tokens.
            if (tokenHandle != IntPtr.Zero)
                CloseHandle(tokenHandle);
            if (dupeTokenHandle != IntPtr.Zero) 
                CloseHandle(dupeTokenHandle);
        }
        catch(Exception ex)
        {
            Console.WriteLine("Exception occurred. " + ex.Message);
        }
    }
}
				
若要解决此问题,调用之前创建 WindowsIdentity 对象下面的代码。 然后可以将重复的令牌传递 WindowsIdentity 对象的构造函数。
bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);
if (false == retVal)
{
  CloseHandle(tokenHandle);
  Console.WriteLine("Exception in token duplication.");		
  return;
}
				

参考

有关详细信息请访问以下 MSDN 网站:
LogonUser
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/Security/logonuser.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/Security/logonuser.asp)

DuplicateToken
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/Security/duplicatetoken.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/Security/duplicatetoken.asp)

这篇文章中的信息适用于:
  • Microsoft .NET Framework Class Libraries 1.0
关键字:?
kbmt kbkernbase kbprb kbsecurity KB319615 KbMtzh
机器翻译机器翻译
注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。
点击这里察看该文章的英文版: 319615? (http://support.microsoft.com/kb/319615/en-us/ )
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。
Retired KB Article不再更新的 KB 内容免责声明
本文介绍那些 Microsoft 不再提供支持的产品。因此本文按“原样”提供,并且不再更新。
 

文章翻译

 

Related Support Centers