如何检查windows用户是否设置了密码? [英] How to check if windows user has a password set?
问题描述
问题
我不知道这会很难弄清楚,但我来了.
I didn't know it would be this difficult to figure out but here I am.
我正在开发一个网络支持客户端,它必须检测当前登录的用户是否设置了密码.我尝试使用 WMI 检查 Win32_UserAccount
类中的 PasswordRequired
属性,但它返回 false
即使我的帐户是 密码保护.我没有想法...
I'm developing a net support client which has to detect if the current logged in user has a password set. I tried it with WMI checking the PasswordRequired
property in the Win32_UserAccount
class, but it returns false
even if my account is password protected. I'm out of ideas...
(背景:我需要此信息来告诉用户他必须设置一个,以便我可以通过远程桌面连接到他,如果帐户不受保护",这不是很高兴.如果有办法获得围绕这一点,我也接受不同的解决方案.)
(Background: I need this info to tell the user he has to set one so I can connect to him via remote desktop, which isn't very happy if the account is "unprotected". If there is a way to get around this I'd also accept a different solution.)
真诚的你
奈法留斯
Sincerely yours
Nefarius
解决方案
比我想象的更容易,我使用 WinAPI 函数 LogonUser
管理它并为您提供这个简单的包装代码:
Easier than I thought, I managed it with the WinAPI function LogonUser
and provide you this simple wrapper code:
private bool PasswordRequired
{
get
{
IntPtr phToken;
// http://www.pinvoke.net/default.aspx/advapi32/LogonUser.html
bool loggedIn = LogonUser(Environment.UserName,
null,
"",
(int)LogonType.LOGON32_LOGON_INTERACTIVE,
(int)LogonProvider.LOGON32_PROVIDER_DEFAULT,
out phToken);
int error = Marshal.GetLastWin32Error();
if (phToken != IntPtr.Zero)
// http://www.pinvoke.net/default.aspx/kernel32/CloseHandle.html
CloseHandle(phToken);
// 1327 = empty password
if (loggedIn || error == 1327)
return false;
else
return true;
}
}
这正是我所需要的,感谢大家快速而有能力的回答,我总是可以指望你们!=)
That's exactly what I needed, thank you all for your fast and competent answers, I can always count on you! =)
推荐答案
为什么不直接尝试使用空密码登录用户?
Why not just to try to LogonUser with empty password?
这篇关于如何检查windows用户是否设置了密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!