Set UserAccountControl values on user

jwradhe

Member
Joined
Sep 23, 2021
Messages
7
Programming Experience
Beginner
Hi!
Im creating a ad-user and that works, but when it comes to set UserAccountControl values it doesnt work.
Im first trying to ad it as hex "0x10200" but it still ends up in AD to have 0x222 as ar default if nothing is added.
Then i tried to add in dec form, 512+65536 but that gives me big error that it doesnt want to take that value.
What am i doing wrong?


C#:
// Add UserAccountControl value

var UF_NORMAL_ACCOUNT = 0x0200;
var UF_ACCOUNTDISABLE = 0;
var UF_DONT_EXPIRE_PASSWD = 0;
var UF_PASSWD_CANT_CHANGE = 0;
var UF_PASSWD_NOTREQD = 0;

if (checkBox_accountisdisabled.Checked == true)
{
    try
    {
        UF_ACCOUNTDISABLE = 0x0002;
        MessageBox.Show("Added accountdisabled");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

if (checkBox_passwordneverexpires.Checked == true)
{
    try
    {
        UF_DONT_EXPIRE_PASSWD = 0x10000;
        MessageBox.Show("Added passwordneverexpires");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

if (checkBox_cannotchangepassword.Checked == true)
{
    try
    {
        UF_PASSWD_CANT_CHANGE = 0x0040;
        MessageBox.Show("Added Cannotchangepassword");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

try
{
    int val = (int)newUser.Properties["userAccountControl"].Value;
    newUser.Properties["userAccountControl"].Value = val | UF_NORMAL_ACCOUNT | UF_ACCOUNTDISABLE | UF_DONT_EXPIRE_PASSWD | UF_PASSWD_CANT_CHANGE | UF_PASSWD_NOTREQD;
    newUser.CommitChanges();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
 
Back
Top Bottom