gpt4 book ai didi

c# - Win32 API LogonUser 离线访问本地账户

转载 作者:行者123 更新时间:2023-11-30 18:29:04 24 4
gpt4 key购买 nike

是否有一些标志系列允许 LogonUser 在计算机未连接到网络时返回可用于模拟本地用户的 token (但所有帐户都已在本地存在)。

我有执行应用程序的域帐户

MYDOMAIN\FooUser

我正在尝试为

获取模拟 token

MYLAPTOP\TestUser

然后我读取了一个文件夹中的一系列文本文件,所有这些文件都可以被 FooUser 读取,但有些文件的读取权限被 TestUser 拒绝。

如果我登录到 Windows 并从 TestUser 运行应用程序,权限映射正确并且文件权限被拒绝。如果我连接到我的域并从 FooUser 运行应用程序,我还可以模拟 TestUser 并且文件权限再次按预期正确拒绝访问(使用 LOGON32_LOGON_INTERACTIVE)。

当我的以太网电缆被拔掉并且我尝试为 TestUser 调用 LogonUser 并且我希望我能够以某种方式验证本地凭据时,问题就出现了。 . 本地?

使用 LOGON32_LOGON_INTERACTIVE:

  • TestUser 输入凭据返回错误,指示“用户名或密码错误”
  • FooUser 输入凭据返回错误指示“没有可用的登录服务器”(有道理,我不是在提示......除了我如何在未连接到 Windows 时首先登录到 Windows我的域名?)

使用 LOGON32_LOGON_NEW_CREDENTIALS:

  • 输入乱码凭据会返回一个 token ,该 token 似乎与 FooUser
  • 具有相同的访问权限
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using Common.NativeMethods.Enumerations;

namespace Common.NativeMethods
{
public static class AdvApi32
{
// http://www.pinvoke.net/default.aspx/advapi32.logonuser
// http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.securestringtoglobalallocunicode(v=vs.100).aspx

// PInvoke into the Win32 API to provide access to the
// LogonUser and CloseHandle functions.
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool LogonUser(
IntPtr username,
IntPtr domain,
IntPtr password,
LogonType logonType,
LogonProvider logonProvider,
ref IntPtr token
);

public static WindowsIdentity LogonUser(SecureString p_userName, SecureString p_password, SecureString p_domainName)
{
IntPtr UserAccountToken = IntPtr.Zero;

IntPtr UserNamePointer = IntPtr.Zero;
IntPtr PasswordPointer = IntPtr.Zero;
IntPtr DomainNamePointer = IntPtr.Zero;

try
{
// Marshal the SecureString to unmanaged memory.
UserNamePointer = Marshal.SecureStringToGlobalAllocUnicode(p_password);
PasswordPointer = Marshal.SecureStringToGlobalAllocUnicode(p_userName);
DomainNamePointer = Marshal.SecureStringToGlobalAllocUnicode(p_domainName);

// Call LogonUser, passing the unmanaged (and decrypted) copy of the SecureString password.
bool ReturnValue =
AdvApi32
.LogonUser(
UserNamePointer,
DomainNamePointer,
PasswordPointer,
LogonType.LOGON32_LOGON_INTERACTIVE, //.LOGON32_LOGON_NEW_CREDENTIALS,
LogonProvider.LOGON32_PROVIDER_DEFAULT, //.LOGON32_PROVIDER_WINNT50,
ref UserAccountToken);

// Get the Last win32 Error and throw an exception.
if (!ReturnValue && UserAccountToken == IntPtr.Zero)
{
int error = Marshal.GetLastWin32Error();

throw
new Win32Exception(error);
}

// The token that is passed to the following constructor must
// be a primary token in order to use it for impersonation.
return
new WindowsIdentity(UserAccountToken);
}
finally
{
// Zero-out and free the unmanaged string reference.
Marshal.ZeroFreeGlobalAllocUnicode(UserNamePointer);
Marshal.ZeroFreeGlobalAllocUnicode(PasswordPointer);
Marshal.ZeroFreeGlobalAllocUnicode(DomainNamePointer);

// Close the token handle.
Kernel32.CloseHandle(UserAccountToken);
}
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.ConstrainedExecution;
using System.Security;

namespace Common.NativeMethods
{
// http://msdn.microsoft.com/en-us/library/system.security.principal.windowsimpersonationcontext%28v=vs.100%29.aspx

public static class Kernel32
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal extern static bool CloseHandle(IntPtr handle);
}
}

最佳答案

糟糕...我在重构时打错了字。 LogonUser 在未连接到域时工作正常;如果您至少指向正确的参数。

 UserNamePointer = Marshal.SecureStringToGlobalAllocUnicode(p_password);
PasswordPointer = Marshal.SecureStringToGlobalAllocUnicode(p_userName);

固定

 UserNamePointer = Marshal.SecureStringToGlobalAllocUnicode(p_userName);
PasswordPointer = Marshal.SecureStringToGlobalAllocUnicode(p_password);

关于c# - Win32 API LogonUser 离线访问本地账户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23643677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com