Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#使用模拟时用户名或密码错误_C#_Impersonation_Credentials - Fatal编程技术网

c#使用模拟时用户名或密码错误

c#使用模拟时用户名或密码错误,c#,impersonation,credentials,C#,Impersonation,Credentials,我试图做的是允许不在域访问权限内的计算机通过模拟域凭据来运行注册表检查工具…这对我域内的任何计算机都很好,但域外的任何计算机都会收到错误的用户名和密码错误,即使他们使用的是域管理员帐户。相同的凭据,只有一台计算机在域上,另一台不在域上。 这是我调用模拟的代码…(下面是模拟代码) 这是模拟文件 namespace Tools { 使用系统; 使用System.Security.Principal; 使用System.Runtime.InteropServices; 使用系统组件模型; 公共类模拟

我试图做的是允许不在域访问权限内的计算机通过模拟域凭据来运行注册表检查工具…这对我域内的任何计算机都很好,但域外的任何计算机都会收到错误的用户名和密码错误,即使他们使用的是域管理员帐户。相同的凭据,只有一台计算机在域上,另一台不在域上。 这是我调用模拟的代码…(下面是模拟代码)

这是模拟文件

namespace Tools
{

使用系统;
使用System.Security.Principal;
使用System.Runtime.InteropServices;
使用系统组件模型;
公共类模拟程序:
可识别
{
#区域公共方法。
// ------------------------------------------------------------------
公众模仿者(
字符串用户名,
字符串域名,
字符串(密码)
{
ImpersonateValidUser(用户名、域名、密码);
}
// ------------------------------------------------------------------
#端区
#区域IDisposable成员。
// ------------------------------------------------------------------
公共空间处置()
{
取消模拟();
}
// ------------------------------------------------------------------
#端区
#区域P/调用。
// ------------------------------------------------------------------
[DllImport(“advapi32.dll”,SetLastError=true)]
私有静态外部int LogonUser(
字符串lpszUserName,
字符串lpszDomain,
字符串lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport(“advapi32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
私有静态外部重复标记(
IntPtr hToken,
int模拟级别,
参考IntPtr hNewToken);
[DllImport(“advapi32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
私有静态外部bool retertoself();
[DllImport(“kernel32.dll”,CharSet=CharSet.Auto)]
私有静态外部布尔闭合句柄(
IntPtr手柄);
private const int LOGON32\u LOGON\u INTERACTIVE=2;
private const int LOGON32_PROVIDER_DEFAULT=0;
// ------------------------------------------------------------------
#端区
#区域私人成员。
// ------------------------------------------------------------------
/// 
///进行实际模拟。
/// 
///要充当的用户的名称。
///要充当的用户的域名。
///要充当的用户的密码。
私有无效模拟ValidUser(
字符串用户名,
字符串域,
字符串(密码)
{
WindowsIdentity tempWindowsIdentity=null;
IntPtr令牌=IntPtr.Zero;
IntPtr-tokenDuplicate=IntPtr.Zero;
尝试
{
if(restortoself())
{
如果(登录用户)(
用户名,
领域,
密码,
LOGON32\u LOGON\u INTERACTIVE,
LOGON32\u提供程序\u默认值,
参考标记)!=0)
{
if(DuplicateToken(token,2,ref tokenDuplicate)!=0)
{
tempWindowsIdentity=新的WindowsIdentity(令牌复制);
impersonationContext=tempWindowsIdentity.Impersonate();
}
其他的
{
抛出新的Win32Exception(Marshal.GetLastWin32Error());
}
}
其他的
{
抛出新的Win32Exception(Marshal.GetLastWin32Error());
}
}
其他的
{
抛出新的Win32Exception(Marshal.GetLastWin32Error());
}
}
最后
{
if(令牌!=IntPtr.Zero)
{
闭合手柄(令牌);
}
如果(令牌重复!=IntPtr.Zero)
{
闭合手柄(重复);
}
}
}
/// 
///还原模拟。
/// 
私有无效撤消模拟()
{
if(impersonationContext!=null)
{
impersonationContext.Undo();
}   
}
private WindowsImpersonationContext impersonationContext=null;

您是否尝试与该计算机上的用户交互登录?您尝试在不在域上的计算机上使用域凭据?LogonUser应该如何验证您传入的凭据?我可能错了,但我认为这不可能。嘿,您解决了这个问题吗?我遇到了完全相同的问题。.应用程序wWorks在域上可以正常工作,但在域外的PC上,它将无法使用相同的凭据工作。
namespace Tools
using System;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.ComponentModel;


public class Impersonator :
    IDisposable
{
    #region Public methods.
    // ------------------------------------------------------------------


    public Impersonator(
        string userName,
        string domainName,
        string password )
    {
        ImpersonateValidUser( userName, domainName, password );
    }

    // ------------------------------------------------------------------
    #endregion

    #region IDisposable member.
    // ------------------------------------------------------------------

    public void Dispose()
    {
        UndoImpersonation();
    }

    // ------------------------------------------------------------------
    #endregion

    #region P/Invoke.
    // ------------------------------------------------------------------

    [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);

    private const int LOGON32_LOGON_INTERACTIVE = 2;
    private const int LOGON32_PROVIDER_DEFAULT = 0;

    // ------------------------------------------------------------------
    #endregion

    #region Private member.
    // ------------------------------------------------------------------

    /// <summary>
    /// Does the actual impersonation.
    /// </summary>
    /// <param name="userName">The name of the user to act as.</param>
    /// <param name="domainName">The domain name of the user to act as.</param>
    /// <param name="password">The password of the user to act as.</param>
    private void ImpersonateValidUser(
        string userName, 
        string domain, 
        string password )
    {
        WindowsIdentity tempWindowsIdentity = null;
        IntPtr token = IntPtr.Zero;
        IntPtr tokenDuplicate = IntPtr.Zero;

        try
        {
            if ( RevertToSelf() )
            {
                if ( LogonUser(
                    userName, 
                    domain, 
                    password, 
                    LOGON32_LOGON_INTERACTIVE,
                    LOGON32_PROVIDER_DEFAULT, 
                    ref token ) != 0 )
                {
                    if ( DuplicateToken( token, 2, ref tokenDuplicate ) != 0 )
                    {
                        tempWindowsIdentity = new WindowsIdentity( tokenDuplicate );
                        impersonationContext = tempWindowsIdentity.Impersonate();
                    }
                    else
                    {
                        throw new Win32Exception( Marshal.GetLastWin32Error() );
                    }
                }
                else
                {
                    throw new Win32Exception( Marshal.GetLastWin32Error() );
                }
            }
            else
            {
                throw new Win32Exception( Marshal.GetLastWin32Error() );
            }
        }
        finally
        {
            if ( token!= IntPtr.Zero )
            {
                CloseHandle( token );
            }
            if ( tokenDuplicate!=IntPtr.Zero )
            {
                CloseHandle( tokenDuplicate );
            }
        }
    }

    /// <summary>
    /// Reverts the impersonation.
    /// </summary>
    private void UndoImpersonation()
    {
        if ( impersonationContext!=null )
        {
            impersonationContext.Undo();
        }   
    }

    private WindowsImpersonationContext impersonationContext = null;