Problem Enabling Account

I just finished rewiriting an app in C# (was in VB.NET), but it doesn't work the same. When I ran the VB code below, I would end up with an account that was enabled. However, when I run the C# code below, I still get the user, but it is disabled.

Please advise.

VB.NET:
Dim userACFlags As Object = newUser.Properties("userAccountControl").Value
newUser.Properties("userAccountControl").Value = userACFlags Or &H200 Or &H10000 Xor &H2

C#:
int userACFlags = (int) newUser.Properties["userAccountControl"].Value; //returns 546
newUser.Properties["userAccountControl"].Value = userACFlags | 0x200 | 0x10000 ^ 0x2;
jawhitmoyerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ihenryCommented:
Jim, the constant enumeration is invalid and the bitwise operation is not correct. Please refer to the following documentation.

ADS_USER_FLAG_ENUM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/ads_user_flag_enum.asp

If userAccountControl attribute of a user object is equal to 546, that means it was not completely created.
546 = ADS_UF_NORMAL_ACCOUNT + ADS_UF_PASSWD_NOTREQD + ADS_UF_ACCOUNTDISABLE

Or literally means, the user object is practically useless because it doesn't have a password and disabled.
At the very least, you need to do these two steps after the user is created.

1. Set a password and commit changes.
newUser.Invoke( "SetPassword", new object[] { "P@ssw0rd" } );
newUser.CommitChanges();

2. Set to enable then commit change one last time.
const int ADS_UF_ACCOUNTDISABLE = 0x2;
int val = (int) newUser.Properties["userAccountControl"].Value;
newUser.Properties["userAccountControl"].Value = val & ~ADS_UF_ACCOUNTDISABLE;
newUser.CommitChanges();

HTH
Henry

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jawhitmoyerAuthor Commented:
Thanks Henry. As always you know your stuff!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.