无法找到名为“LogonUser”的入口点在DLL“advapi32.dll”中冒充异常

发布于 2024-11-27 07:13:38 字数 684 浏览 2 评论 0原文

在执行一些遗留模拟逻辑时,我遇到以下异常:

无法在 DLL 'advapi32.dll' 中找到名为 'LogonUser' 的入口点

我知道该错误意味着我的应用程序无法在 DLL 中找到 LogonUser 方法advapi32.dll。

代码看起来像这样:

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String     lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);



if(LogonUser(_username, _domainname, _password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref _tokenHandle))
{
//do stuff...
}

有没有人遇到过类似的错误 - 有关如何修复它或为什么会发生的任何建议?除了使用 advapi32.dll (它是 .net 3.5 解决方案,但有很多遗留类)之外,还有更好的方法吗?

I'm getting the following exception when going through some legacy impersonation logic:

Unable to find an entry point named 'LogonUser' in DLL 'advapi32.dll'

I understand that the error means that my app can't find the LogonUser method in the advapi32.dll.

The code looks something like this:

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String     lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);



if(LogonUser(_username, _domainname, _password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref _tokenHandle))
{
//do stuff...
}

Has anyone had a similar error - any suggestions on how to fix it or why it is happening? Is there a better way to do this besides using the advapi32.dll (its a .net 3.5 solution but there are lots of legacy classes)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫弦 2024-12-04 07:13:38

也许它与“ExactSpelling = true”有关

这似乎有效:

public enum LogonType : int
{
    Interactive = 2,
    Network = 3,
    Batch = 4,
    Service = 5,
    Unlock = 7,
    NetworkCleartText = 8,
    NewCredentials = 9,
}

public enum LogonProvider : int
{  
    Default = 0,
}

public class Impersonation : IDisposable
{
    #region Dll Imports

    [DllImport("kernel32.dll")]
    private static extern Boolean CloseHandle(IntPtr hObject);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool LogonUser(string username, string domain,
                                          string password, LogonType logonType,
                                          LogonProvider logonProvider,
                                          out IntPtr userToken);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool DuplicateToken(IntPtr token, int impersonationLevel,
        ref IntPtr duplication);

          [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool ImpersonateLoggedOnUser(IntPtr userToken);
    #endregion

    #region Private members

    private bool _disposed;

    private WindowsImpersonationContext _impersonationContext;

    #endregion

    #region Constructors

    public Impersonation(String username, String domain, String password)
    {
        IntPtr userToken = IntPtr.Zero;
        IntPtr userTokenDuplication = IntPtr.Zero;

        // Logon with user and get token.
        bool loggedOn = LogonUser(username, domain, password,
            LogonType.Interactive, LogonProvider.Default,
            out userToken);

        if (loggedOn)
        {
            try
            {
                // Create a duplication of the usertoken, this is a solution
                // for the known bug that is published under KB article Q319615.
                if (DuplicateToken(userToken, 2, ref userTokenDuplication))
                {
                    // Create windows identity from the token and impersonate the user.
                    WindowsIdentity identity = new WindowsIdentity(userTokenDuplication);
                    _impersonationContext = identity.Impersonate();
                }
                else
                {
                    // Token duplication failed!
                    // Use the default ctor overload
                    // that will use Mashal.GetLastWin32Error();
                    // to create the exceptions details.
                    throw new Exception("Could not copy token");
                }
            }
            finally
            {
                // Close usertoken handle duplication when created.
                if (!userTokenDuplication.Equals(IntPtr.Zero))
                {
                    // Closes the handle of the user.
                    CloseHandle(userTokenDuplication);
                    userTokenDuplication = IntPtr.Zero;
                }

                // Close usertoken handle when created.
                if (!userToken.Equals(IntPtr.Zero))
                {
                    // Closes the handle of the user.
                    CloseHandle(userToken);
                    userToken = IntPtr.Zero;
                }
            }
        }
        else
        {               
            throw new Exception("Login failed");
        }
    }

    ~Impersonation()
    {
        Dispose(false);
    }
    #endregion

    #region Public methods

    public void Revert()
    {
        if (_impersonationContext != null)
        {
            // Revert to previous user.
            _impersonationContext.Undo();
            _impersonationContext = null;
        }
    }
    #endregion

    #region IDisposable implementation.

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            Revert();

            _disposed = true;
        }
    }
    #endregion
}

Maybe it has something to do with the "ExactSpelling = true"

This seems to work:

public enum LogonType : int
{
    Interactive = 2,
    Network = 3,
    Batch = 4,
    Service = 5,
    Unlock = 7,
    NetworkCleartText = 8,
    NewCredentials = 9,
}

public enum LogonProvider : int
{  
    Default = 0,
}

public class Impersonation : IDisposable
{
    #region Dll Imports

    [DllImport("kernel32.dll")]
    private static extern Boolean CloseHandle(IntPtr hObject);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool LogonUser(string username, string domain,
                                          string password, LogonType logonType,
                                          LogonProvider logonProvider,
                                          out IntPtr userToken);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool DuplicateToken(IntPtr token, int impersonationLevel,
        ref IntPtr duplication);

          [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool ImpersonateLoggedOnUser(IntPtr userToken);
    #endregion

    #region Private members

    private bool _disposed;

    private WindowsImpersonationContext _impersonationContext;

    #endregion

    #region Constructors

    public Impersonation(String username, String domain, String password)
    {
        IntPtr userToken = IntPtr.Zero;
        IntPtr userTokenDuplication = IntPtr.Zero;

        // Logon with user and get token.
        bool loggedOn = LogonUser(username, domain, password,
            LogonType.Interactive, LogonProvider.Default,
            out userToken);

        if (loggedOn)
        {
            try
            {
                // Create a duplication of the usertoken, this is a solution
                // for the known bug that is published under KB article Q319615.
                if (DuplicateToken(userToken, 2, ref userTokenDuplication))
                {
                    // Create windows identity from the token and impersonate the user.
                    WindowsIdentity identity = new WindowsIdentity(userTokenDuplication);
                    _impersonationContext = identity.Impersonate();
                }
                else
                {
                    // Token duplication failed!
                    // Use the default ctor overload
                    // that will use Mashal.GetLastWin32Error();
                    // to create the exceptions details.
                    throw new Exception("Could not copy token");
                }
            }
            finally
            {
                // Close usertoken handle duplication when created.
                if (!userTokenDuplication.Equals(IntPtr.Zero))
                {
                    // Closes the handle of the user.
                    CloseHandle(userTokenDuplication);
                    userTokenDuplication = IntPtr.Zero;
                }

                // Close usertoken handle when created.
                if (!userToken.Equals(IntPtr.Zero))
                {
                    // Closes the handle of the user.
                    CloseHandle(userToken);
                    userToken = IntPtr.Zero;
                }
            }
        }
        else
        {               
            throw new Exception("Login failed");
        }
    }

    ~Impersonation()
    {
        Dispose(false);
    }
    #endregion

    #region Public methods

    public void Revert()
    {
        if (_impersonationContext != null)
        {
            // Revert to previous user.
            _impersonationContext.Undo();
            _impersonationContext = null;
        }
    }
    #endregion

    #region IDisposable implementation.

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            Revert();

            _disposed = true;
        }
    }
    #endregion
}
小姐丶请自重 2024-12-04 07:13:38

您是否尝试过使用 pinvoke.net 上提供的 LogonUser 签名版本?

Have you tried using the version of LogonUser's signature provided on pinvoke.net?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文