How to lock the AD (Active Directory) Account programmatically in C#

1. Below is the function to Lock Account.

private void LockAccount()

{

string _userAccountWithoutDomain = “test”;

string _domainName = “IND”;

string _userBadPassword = “yyyyy”; // password should be incorrect

int _passwordExpiryPolicy = 3;

string _connectionPrefix = “LDAP://” + _domainName;

for (int i = 0; i < _passwordExpiryPolicy; i++)

{

try {

new DirectoryEntry(_connectionPrefix, _userAccountWithoutDomain, _userBadPassword).RefreshCache(); }

catch (Exception)

{ }

}

}

this is the tested code and works well.

Advertisements

3 thoughts on “How to lock the AD (Active Directory) Account programmatically in C#

  1. Thanks very much … and thank you for actually testing your code to make sure it works! All of the other code I’ve seen on this (including from StackOverflow, which I usually trust as the best source) clearly was never tested.

  2. Wow, superb weblog format! How long have you ever been running a blog for? you made blogging glance easy. The total look of your web site is fantastic, let alone the content material!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s