[英]C# How to get the AD user cannot change the password property from LDAP attribute userAccountControl?
我正在嘗試在ASP .NET Core 5
中使用庫Novell.Directory.Ldap
獲取用戶帳戶控制屬性。 當我搜索用戶屬性時,我發現屬性名稱userAccountControl
設置為某個數字。 搜索解決方案后,我能夠找到:
bool isUserActive = false;
bool userMustChangePassword = false;
bool passwordNeverExpires = false;
bool passwordCannotBeChanged = false;
var flags = Convert.ToInt32(attributeSet.GetAttribute("userAccountControl").StringValue);
isUserActive = !Convert.ToBoolean(flags & 0x0002); //1. checks if user is enabled
if ((flags == 66048)) //65536+512
{
passwordNeverExpires = true; //2. Password never expires property
}
long value = Convert.ToInt64(attributeSet.GetAttribute("pwdLastSet").StringValue);
if (value == 0)
{
userMustChangePassword = true; //3. User must change password at next login
}
但我無法弄清楚如何讓User cannot change password
以及account is locked
屬性? 或者我如何比較像0x0040
這樣的二進制值? 請幫忙
userAccountControl
值是一個位標志,這意味着數字的二進制表示中的每個位都是“開”或“關”,具體取決於它是 1 還是 0。所以十進制值沒有意義。
當您檢查它是否已啟用時,您已經在正確檢查該值:
isUserActive = !Convert.ToBoolean(flags & 0x0002); //1. checks if user is enabled
同樣,在檢查任何其他標志時也應該這樣做。 每個值都列在文檔中。
當您檢查密碼是否設置為永不過期時,您正在比較十進制值,這並不總是給您正確的答案。 相反,檢查位值:
passwordNeverExpires = Convert.ToBoolean(flags & 0x10000);
如果用戶無法更改密碼,則類似:
var userCannotChangePassword = Convert.ToBoolean(flags & 0x0040);
並且帳戶被鎖定:
var accountLocked = Convert.ToBoolean(flags & 0x0010);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.