Home > Enterprise >  How to get a userAccountControl Attribute in active directory
How to get a userAccountControl Attribute in active directory

Time:10-31

Am working on active directory and i need to get a value of a checkbox in userAccountControl enter image description here

and want to know if the check box is checked or not.. I tried to get the value of it by using the code

    VARIANT var;
    VariantInit(&var);
    hr = pUsr->Get(CComBSTR("userAccountControl"), &var);
    if (SUCCEEDED(hr)) {
        std::cout << V_I4(&var) << std::endl;
       }

and I got the output 512. The problem is even if the checkbox is checked or unchecked it has the same value 512. it changes for other checkboxes but shows the same value for this option i need a way to find if the checkbox is true or not

CodePudding user response:

You want to look at the pwdLastSet attribute. The documentation for that says:

If this value is set to 0 and the User-Account-Control attribute does not contain the UF_DONT_EXPIRE_PASSWD flag, then the user must set the password at the next logon.

So two things must be true to force a password change on next logon:

  1. The pwdLastSet attribute is 0.

  2. The account is not set to never expire the password. In C I think that would look something like this:

    !(V_I4(&var) & DONT_EXPIRE_PASSWORD)

CodePudding user response:

As suggested by the above answer I tried to get the pwdLastSet attribute of the user and checked if its zero with the following code

CComVariant svar;
    hr = spads->Get(CComBSTR("pwdLastSet"), &svar);
    std::string messageeee = std::system_category().message(hr);
    std::cout << messageeee << std::endl;
    if (FAILED(hr))
    {
        std::cout << "failed to get attribute" << std::endl;
        return hr;
    }

    // Get the IADsLargeInteger interface.
    CComPtr<IADsLargeInteger> spli;
    hr = svar.pdispVal->QueryInterface(IID_IADsLargeInteger,
        (LPVOID*)&spli);
    if (FAILED(hr))
    {
        return hr;
    }

    long lHigh;
    long lLow;
    hr = spli->get_HighPart(&lHigh);
    hr = spli->get_LowPart(&lLow);
    __int64 i64;
    i64 = (ULONG)lHigh;
    i64 = (i64 << 32);
    i64 = i64   (ULONG)lLow;
    // Print all of the values.
    printf("Combined = %I64d\n", i64);
}
  • Related