Home > Deveploment and Programming, SharePoint Development > How to lock the AD (Active Directory) Account programmatically in C#

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
  1. Dave Graper
    31/10/2012 at 7:24 pm

    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. 16/11/2012 at 5:18 am

    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!

  3. 19/06/2013 at 1:31 pm

    When writing this article I found some great information about
    and at. Are you going on a lighthouse tour of the Oregon Coast.
    Find more information, about Deer Hunting in Alabama here.

  1. No trackbacks yet.

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

%d bloggers like this: