SwamyN
asked on
Disabling local windows user account using c#.net
i want to disable a local windows user account using c#.net 2005 code.
i am able to create new account(local) using the folllowing code , but not able to disable.
i am getting the following error on line 10:
System.Runtime.InteropServ ices.COMEx ception (0x8000500F): The directory property cannot be found in the cache
the code snippet:-
private void AddUser(string strDoamin, string strLogin, string strPwd)
{
try
{
obDirEntry = new DirectoryEntry("WinNT://" + Environment.MachineName + ",Computer", "username", "password");
DirectoryEntries entries = obDirEntry.Children;
DirectoryEntry obUser = entries.Add(strLogin, "User");
obUser.Properties["FullNam e"].Add("A migo");
object obRet = obUser.Invoke("SetPassword ", strPwd);
10: obUser.Properties["useracc ountcontro l"].Value = ADS_UF_ACCOUNTDISABLE;
obUser.CommitChanges();
MessageBox.Show("User Account Crreated Successfully!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message );
}
}
pls help.
i am able to create new account(local) using the folllowing code , but not able to disable.
i am getting the following error on line 10:
System.Runtime.InteropServ
the code snippet:-
private void AddUser(string strDoamin, string strLogin, string strPwd)
{
try
{
obDirEntry = new DirectoryEntry("WinNT://" + Environment.MachineName + ",Computer", "username", "password");
DirectoryEntries entries = obDirEntry.Children;
DirectoryEntry obUser = entries.Add(strLogin, "User");
obUser.Properties["FullNam
object obRet = obUser.Invoke("SetPassword
10: obUser.Properties["useracc
obUser.CommitChanges();
MessageBox.Show("User Account Crreated Successfully!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message
}
}
pls help.
ASKER
after Replacing the above suggested line the following exception is thrown:-
System.Runtime.InteropServ ices.COMEx ception
{"Exception from HRESULT: 0x8000500C"}
also the newly created local users: Convert.ToInt32(user.Prope rties["Use rFlags"].V alue) is 513
but setting user.Properties["UserFlags "].Value = valUsr | ADS_UF_ACCOUNTDISABLE; throws the exception.
pls help me out.
System.Runtime.InteropServ
{"Exception from HRESULT: 0x8000500C"}
also the newly created local users: Convert.ToInt32(user.Prope
but setting user.Properties["UserFlags
pls help me out.
So, if you comment out the line where you set UserFlags, the account is created all right?
You may check the value of UserFlags before commiting changes, if initial value is 513 after the OR operation it should be 515 (ADF_UF_ACCOUNTDISABLE is actually 2).
I cannot reproduce the error you mention, this is something related to Active Directory Services Interfaces (ADSI, see http://support.microsoft.com/kb/241981/en-us), maybe it is related to this ADSI version you have (check version like in http://support.microsoft.com/kb/247537).
Another try I can suggest is to set directly the value of UserFlags to 2:
obUser.Properties["UserFla gs"].Value =2;
You may check the value of UserFlags before commiting changes, if initial value is 513 after the OR operation it should be 515 (ADF_UF_ACCOUNTDISABLE is actually 2).
I cannot reproduce the error you mention, this is something related to Active Directory Services Interfaces (ADSI, see http://support.microsoft.com/kb/241981/en-us), maybe it is related to this ADSI version you have (check version like in http://support.microsoft.com/kb/247537).
Another try I can suggest is to set directly the value of UserFlags to 2:
obUser.Properties["UserFla
ASKER
thank you very much it solved my problem by using direct value.
Also when i use LDAP provider i am not able to add new user.
DirectoryEntry dsHelper = new UserAdmin("LDAP://"+ ddlDomain.SelectedItem.ToS tring(), txtUserNameI.Text, txtPasswordI.Text, AuthenticationTypes.Secure , ddlDomainI.SelectedText);
NewUser = AD.Children.Add("CN=" + dsUser.Username + "", "user");
if (impersonateValidUser(this .LoginUser name, this.DomainName, this.loginPassword))
{
NewUser.CommitChanges();
}
i am getting following error at NewUser.CommitChanges(); line:-
UnAuthorisedAccessExceptio n
General access denied error
System.UnauthorizedAccessE xception was unhandled
Message="General access denied error\r\n"
Source="Active Directory"
StackTrace:
at System.DirectoryServices.I nterop.Uns afeNativeM ethods.IAd s.SetInfo( )
at System.DirectoryServices.D irectoryEn try.Commit Changes()
at DSHelper.UserAdmin.SaveUse r(DSUser dsUser)
at DSAdmin.Form1.AddNewUser()
at DSAdmin.Form1.btnAddUser_C lick(Objec t sender, EventArgs e)
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.De buggableCa llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Unsaf eNativeMet hods.Dispa tchMessage W(MSG& msg)
at System.Windows.Forms.Appli cation.Com ponentMana ger.System .Windows.F orms.Unsaf eNativeMet hods.IMsoC omponentMa nager.FPus hMessageLo op(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoopInner (Int32 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoop(Int3 2 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Run (Form mainForm)
at DSAdmin.Program.Main()
at System.AppDomain.nExecuteA ssembly(As sembly assembly, String[] args)
at System.AppDomain.ExecuteAs sembly(Str ing assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssembly()
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
pls help , i am using Domain Administrator User name & password, using which i can manually Login to server and add new user.
Also when i use LDAP provider i am not able to add new user.
DirectoryEntry dsHelper = new UserAdmin("LDAP://"+ ddlDomain.SelectedItem.ToS
NewUser = AD.Children.Add("CN=" + dsUser.Username + "", "user");
if (impersonateValidUser(this
{
NewUser.CommitChanges();
}
i am getting following error at NewUser.CommitChanges(); line:-
UnAuthorisedAccessExceptio
General access denied error
System.UnauthorizedAccessE
Message="General access denied error\r\n"
Source="Active Directory"
StackTrace:
at System.DirectoryServices.I
at System.DirectoryServices.D
at DSHelper.UserAdmin.SaveUse
at DSAdmin.Form1.AddNewUser()
at DSAdmin.Form1.btnAddUser_C
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at DSAdmin.Program.Main()
at System.AppDomain.nExecuteA
at System.AppDomain.ExecuteAs
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.ThreadHel
pls help , i am using Domain Administrator User name & password, using which i can manually Login to server and add new user.
With LDAP you manage domain users only, the error you get shows that you don't have rights to create new users, maybe you should try first using the current logged in user (domain admin), without impersonation.
ASKER
without impersonation if i login using domain admin everything works fine, but while impersonating it gives error as described above.
pls provide help for impersonation.
pls provide help for impersonation.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Forced accept.
Computer101
EE Admin
Computer101
EE Admin
int valUsr = Convert.ToInt32(obUser.Pro
obUser.Properties["UserFla
Actually UserFlags is null (valUsr is always 0) for a newly created account, but i wrote a general approach.