參考:https://support.microsoft.com/zh-cn/help/305144/how-to-use-the-useraccountcontrol-flags-to-manipulate-user-account-pro
(如何使用 UserAccountControl 標志操縱用戶帳戶屬性)
因為2是 用戶禁用屬性,只要用戶禁用了,UserAccountControl 的屬性值就加了2了
那么2數值這么小,怎么判斷用戶確實被禁用了呢
之前看到博客說等於UserAccountControl 屬性值等於514的,
我原來也這么寫的,但這個很片面,很少、我很傻很天真。
string userAcc = subEntry.Properties["userAccountControl"].Value.ToString();
if (userAcc == "514")
{
isDelete = "Y";
}
就比如說UserAccountControl 屬性值等於66082就是禁用的用戶,算個算數
66082-65536(用戶密碼永不過期)=546
645-512(典型用戶的默認帳戶類型)=34
34-32(不需要密碼)=2
哇,里面含有2,是禁用的,完美
現在要寫個算法,算UserAccountControl 屬性值包含2。思路清晰明確。
一下貼上我簡陋的function:
/// <summary>
/// 根據AD域的userAccountControl屬性判斷用戶是否禁用
/// </summary>
/// <param name="userAccContr"></param>
/// <returns>是否禁用</returns>
private bool GetUserDelete(int userAccContr)
{
if (userAccContr >= 16777216) //TRUSTED_TO_AUTH_FOR_DELEGATION - 允許該帳戶進行委派
{
userAccContr = userAccContr - 16777216;
}
if (userAccContr >= 8388608) //PASSWORD_EXPIRED - (Windows 2000/Windows Server 2003) 用戶的密碼已過期
{
userAccContr = userAccContr - 8388608;
}
if (userAccContr >= 4194304) //DONT_REQ_PREAUTH
{
userAccContr = userAccContr - 4194304;
}
if (userAccContr >= 2097152) //USE_DES_KEY_ONLY - (Windows 2000/Windows Server 2003) 將此用戶限制為僅使用數據加密標准 (DES) 加密類型的密鑰
{
userAccContr = userAccContr - 2097152;
}
if (userAccContr >= 1048576) //NOT_DELEGATED - 設置此標志后,即使將服務帳戶設置為信任其進行 Kerberos 委派,也不會將用戶的安全上下文委派給該服務
{
userAccContr = userAccContr - 1048576;
}
if (userAccContr >= 524288) //TRUSTED_FOR_DELEGATION - 設置此標志后,將信任運行服務的服務帳戶(用戶或計算機帳戶)進行 Kerberos 委派。任何此類服務都可模擬請求該服務的客戶端。若要允許服務進行 Kerberos 委派,必須在服務帳戶的 userAccountControl 屬性上設置此標志
{
userAccContr = userAccContr - 524288;
}
if (userAccContr >= 262144) //SMARTCARD_REQUIRED - 設置此標志后,將強制用戶使用智能卡登錄
{
userAccContr = userAccContr - 262144;
}
if (userAccContr >= 131072) //MNS_LOGON_ACCOUNT - 這是 MNS 登錄帳戶
{
userAccContr = userAccContr - 131072;
}
if (userAccContr >= 65536) //DONT_EXPIRE_PASSWORD-密碼永不過期
{
userAccContr = userAccContr - 65536;
}
if (userAccContr >= 2097152) //MNS_LOGON_ACCOUNT - 這是 MNS 登錄帳戶
{
userAccContr = userAccContr - 2097152;
}
if (userAccContr >= 8192) //SERVER_TRUST_ACCOUNT - 這是屬於該域的域控制器的計算機帳戶
{
userAccContr = userAccContr - 8192;
}
if (userAccContr >= 4096) //WORKSTATION_TRUST_ACCOUNT - 這是運行 Microsoft Windows NT 4.0 Workstation、Microsoft Windows NT 4.0 Server、Microsoft Windows 2000 Professional 或 Windows 2000 Server 並且屬於該域的計算機的計算機帳戶
{
userAccContr = userAccContr - 4096;
}
if (userAccContr >= 2048) //INTERDOMAIN_TRUST_ACCOUNT - 對於信任其他域的系統域,此屬性允許信任該系統域的帳戶
{
userAccContr = userAccContr - 2048;
}
if (userAccContr >= 512) //NORMAL_ACCOUNT - 這是表示典型用戶的默認帳戶類型
{
userAccContr = userAccContr - 512;
}
if (userAccContr >= 256) //TEMP_DUPLICATE_ACCOUNT - 此帳戶屬於其主帳戶位於另一個域中的用戶。此帳戶為用戶提供訪問該域的權限,但不提供訪問信任該域的任何域的權限。有時將這種帳戶稱為“本地用戶帳戶”
{
userAccContr = userAccContr - 256;
}
if (userAccContr >= 128) //ENCRYPTED_TEXT_PASSWORD_ALLOWED - 用戶可以發送加密的密碼
{
userAccContr = userAccContr - 128;
}
if (userAccContr >= 64) //PASSWD_CANT_CHANGE - 用戶不能更改密碼。可以讀取此標志,但不能直接設置它
{
userAccContr = userAccContr - 64;
}
if (userAccContr >= 32) //PASSWD_NOTREQD - 不需要密碼
{
userAccContr = userAccContr - 32;
}
if (userAccContr >= 16) //LOCKOUT
{
userAccContr = userAccContr - 16;
}
if (userAccContr >= 8) //HOMEDIR_REQUIRED - 需要主文件夾
{
userAccContr = userAccContr - 8;
}
//if (userAccContr >= 2) //ACCOUNTDISABLE - 禁用用戶帳戶
//{
// userAccContr = userAccContr - 2;
//}
//if (userAccContr >= 1) //SCRIPT - 將運行登錄腳本
//{
// userAccContr = userAccContr - 1;
//}
if (userAccContr >= 2)
{
return true;
}
return false;
}
調用部分:
string isDelete = "N";
string userAccContr = subEntry.Properties["userAccountControl"].Value.ToString();
if (!string.IsNullOrEmpty(userAccContr))
{
if (GetUserDelete(int.Parse(userAccContr)))
{
isDelete = "Y";
}
}
OVER,有更好的方法再更新
轉自:https://www.cnblogs.com/tldxh/p/7093360.html

