Christian DeBono

Christian DeBono

  • NA
  • 1
  • 22.1k

Unlock Account on Active Directory

Nov 8 2010 10:54 AM
I'm experiencing some problems when unlocking a locked account on AD.

I'll keep it very simple. I'm assuming that the account is currently locked.
 
  1. UserPrincipal oUserPrincipal = ADMethods.GetUser("test.user");    
  2.   
  3. oUserPrincipal.UnlockAccount();    
  4. oUserPrincipal.Save();   
oUserPrincipal is returned using an Admin account on the AD though a PrincipalContext using

Now

oUserPrincipal.UnlockAccount(); is generating an exception System.UnauthorizedAccessException

When checking the logs on the "Test AD server", it's returning a failed negotiation stating that "test.user" is locked out. Since I'm returning a user principal using an admin account, why is unlockAccount method generating an error?

Would appreciate your help. attached please find the entire class
 
here's the exception:

System.UnauthorizedAccessException: Access is denied.
at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.SetInfo()
at System.DirectoryServices.DirectoryEntry.CommitChanges()
at System.DirectoryServices.AccountManagement.SDSUtils.WriteAttribute(String dePath, String attribute, Int32 value, NetCred credentials, AuthenticationTypes authTypes)
at System.DirectoryServices.AccountManagement.ADStoreCtx.WriteAttribute(Principal p, String attribute, Int32 value)
at System.DirectoryServices.AccountManagement.ADStoreCtx.UnlockAccount(AuthenticablePrincipal p)
at System.DirectoryServices.AccountManagement.AccountInfo.UnlockAccount()
at UnlockApp.Form1.UnlockUserAccount(String sUserName) in C:\Users\christian.debono\Documents\Visual Studio 2010\Projects\UnlockApp\UnlockApp\Form1.cs:line 78
at UnlockApp.Form1.UnlockButton_Click(Object sender, EventArgs e) in C:\Users\christian.debono\Documents\Visual Studio 2010\Projects\UnlockApp\UnlockApp\Form1.cs:line 45
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Attachment: Form1.rar

Answers (1)