Help and Support

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

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

展开全部 | 关闭全部

症状

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

原因

出现此问题是标记的由于创建 WindowsIdentity 对象时使用的类型。 您收到如果令牌主令牌时不尝试模拟"症状"一节中列出的错误信息。 您必须具有 WindowsIdentity.Impersonate 方法使用模拟令牌。

解决方案

若要从模拟令牌,请获取主令牌,使用 DuplicateToken Win 32 函数。

状态

此错误已在 Microsoft.NET Framework 类中纠正库 1.1。

更多信息

使用 Visual C# 示例代码重现该错误信息的。 此代码将 LOGON32_LOGON_INTERACTIVE 值传递给 LogonUser 函数会导致创建为一个主令牌的令牌中。 此代码也将 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