c# - Active Directory Acquiring Locked/Unlocked status in a Windows Service -


i making windows service gets locked/unlocked status of active directory accounts in local domain try.local. though account name user1 locked, gives false value isaccountlocked().

using (var context = new principalcontext(contexttype.domain, "try.local")) {     using (var searcher = new principalsearcher(new userprincipal(context)))     {         foreach (var result in searcher.findall())         {             directoryentry de = result.getunderlyingobject() directoryentry;             library.writeerrorlog("first name: " + de.properties["givenname"].value);             try{                 string name = (string)de.properties["samaccountname"].value;                 principalcontext ctx = new principalcontext(contexttype.domain,"try.local","cn="+name+",ou=users,dc=try,dc=local","administrator","password");                 userprincipal usr = userprincipal.findbyidentity(ctx, name);                 if(usr!=null){                     library.writeerrorlog("isaccountlockedout\t"+usr.isaccountlockedout());                 }                 usr.dispose();                 ctx.dispose();             }         catch(exception e){                                                      library.writeerrorlog(e);         }     } } 

i tried

using (var context = new principalcontext(contexttype.domain, "try.local")) {     using (var searcher = new principalsearcher(new userprincipal(context)))     {         foreach (var result in searcher.findall())         {             directoryentry de = result.getunderlyingobject() directoryentry;             library.writeerrorlog("sam account name : " + de.properties["samaccountname"].value);             int uc = convert.toint32(de.properties["useraccountcontrol"][0]);             const int ads_lockout = 0x00000010;             bool account_lockedout = (uc & ads_lockout)==ads_lockout;             library.writeerrorlog("isaccountlockedout : "+account_lockedout);                                                }     } } 

writeerrorlog(string abc) writes abc textfileit gives false if account locked

user1 locked

i grateful if guidance on issue new active directory. in advance!

you must have valid network credentials in order query active directory.

when service runs local service, not have network credentials - can act on local system. if need network credentials, configure service run network service instead. (if need administrator access on local machine, use local system; has network credentials , local administrator access.)

services running network service or local system use computer's active directory account when accessing network, i.e., if computer named plugh username used access network plugh$.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -