admin管理员组文章数量:1125931
I have a piece of code that was working for over 10 years and now we see on Win11 after updating to FeaturePack 24H2 that something goes wrong - can't use the LogonUser method anymore - always resulting in error 1326 - The username or password is incorrect - running the same code in Win11 23H2 is still working (also Win10 or Server versions are working)! Don't know why - any help would be really appreciated
Error occurs on call of LogonUser
internal class Program
{
public const int LOGON32_LOGON_BATCH = 4;
public const int LOGON32_LOGON_NETWORK = 3;
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;
public const int LOGON32_PROVIDER_DEFAULT = 0;
public const int LOGON32_PROVIDER_WINNT50 = 3;
public const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
public const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
public const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr TokenHandle);
[DllImport("Kernel32.dll", SetLastError = true)]
static extern uint FormatMessage(uint dwFlags, IntPtr lpSource,
uint dwMessageId, uint dwLanguageId, ref IntPtr lpBuffer,
uint nSize, IntPtr pArguments);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr LocalFree(IntPtr hMem);
private static IntPtr m_tokenHandle = IntPtr.Zero;
static void Main(string[] args)
{
string user = "user";
string domain = "domain";
string pwd = "pwd";
if (!LogonUser(user, domain, pwd,
LOGON32_LOGON_NETWORK_CLEARTEXT, LOGON32_PROVIDER_DEFAULT,
ref m_tokenHandle))
Console.WriteLine("LogonUser " + getWin32ErrMessage());
else
Console.WriteLine("LogonUser successful");
Console.ReadKey();
}
private static string getWin32ErrMessage()
{
int errCode = -1;
string errMsg = "?";
try
{
errCode = Marshal.GetLastWin32Error();
errMsg = FormatMessage(errCode);
}
catch (Exception)
{
}
return String.Format("failed with error code {0}, msg: {1}", errCode, errMsg);
}
public static string FormatMessage(int winErrCode)
{
int nLastError = winErrCode;
IntPtr lpMsgBuf = IntPtr.Zero;
uint dwChars = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
IntPtr.Zero,
(uint)nLastError,
0, // Default language
ref lpMsgBuf,
0,
IntPtr.Zero);
if (dwChars == 0)
{
return null;
}
string sRet = Marshal.PtrToStringAnsi(lpMsgBuf);
// Free the buffer.
lpMsgBuf = LocalFree(lpMsgBuf);
return sRet;
}
}
本文标签: cLogonUser from advapi32dll not working on Win11 24H2Stack Overflow
版权声明:本文标题:c# - LogonUser from advapi32.dll not working on Win11 24H2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736633910a1945835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论