locked
Unable to show the "LockOutTime" value from my AD RRS feed

  • Question

  • User1312557753 posted

    Hello, for debugging purposes I am trying to display the "LockOutTime"

    Here is my code (Using Windows Forms / C#):

     string strUserName = tbxUserAcc.Text;

    string strPath = "LDAP://dc=CB,dc=MYSITE,dc=COM";

    DirectoryEntry usr = new DirectoryEntry(strPath);

    DirectorySearcher searcher = new DirectorySearcher(usr);

    searcher.Filter = "(SAMAccountName=" + strUserName + ")";

    searcher.CacheResults = false;

    SearchResult result = searcher.FindOne();

    MessageBox.Show(result.Properties["LockOutTime"][0].ToString());

     

    ...I am getting an error message of (An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

                                                         Additional information: Index was out of range. Must be non-negative and less than the size of collection.)

     

    fun stuff :)

    Monday, October 20, 2008 9:59 AM

Answers

  • User1312557753 posted

    Do you need the [0] in this row?

    MessageBox.Show(result.Properties["LockOutTime"][0].ToString());

     

    I think so because when I remove it I get this: System.DirectoryServices.ResultPropertyValueCollection

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 20, 2008 10:25 AM
  • User1312557753 posted

    It doesn't like that, getting error: Cannot convert type 'System.DirectoryServices.ResultPropertyValueCollection' to 'int'

    The thing is I can display other values from the AD using similar code such as:

    MessageBox.Show(result.Properties["givenName"][0].ToString()); //a string

    MessageBox.Show(result.Properties["userAccountControl"][0].ToString()); //an int

     so I can't figure this out :|

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 20, 2008 10:48 AM
  • User1312557753 posted
    Basically what I am trying to do with this code is unlock an account,   should i use this instead?  

    userAccountControl    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 20, 2008 11:04 AM

All replies

  • User2130758966 posted

    Do you need the [0] in this row?

    MessageBox.Show(result.Properties["LockOutTime"][0].ToString());

     

    Monday, October 20, 2008 10:15 AM
  • User1312557753 posted

    Do you need the [0] in this row?

    MessageBox.Show(result.Properties["LockOutTime"][0].ToString());

     

    I think so because when I remove it I get this: System.DirectoryServices.ResultPropertyValueCollection

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 20, 2008 10:25 AM
  • User2130758966 posted

    how about

    MessageBox.Show(((int)result.Properties["LockOutTime"]).ToString());

     

    Is it this line that the exception is occuring on?

    You could put a breakpoint in there and then explore the result datatype in the debugger...

    Monday, October 20, 2008 10:37 AM
  • User1312557753 posted

    It doesn't like that, getting error: Cannot convert type 'System.DirectoryServices.ResultPropertyValueCollection' to 'int'

    The thing is I can display other values from the AD using similar code such as:

    MessageBox.Show(result.Properties["givenName"][0].ToString()); //a string

    MessageBox.Show(result.Properties["userAccountControl"][0].ToString()); //an int

     so I can't figure this out :|

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 20, 2008 10:48 AM
  • User1312557753 posted
    Basically what I am trying to do with this code is unlock an account,   should i use this instead?  

    userAccountControl    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 20, 2008 11:04 AM
  • User-1679943560 posted

    Check if the "LockOutTime" attribute exists for the particular user in AD using adsiedit. I used to get a similar error message and i found out that the attribute which i was trying to get, there was no value for it

    Tuesday, November 25, 2008 7:49 PM
  • User-1679943560 posted

     setting lockouttime to zero will unlock the account

    Tuesday, November 25, 2008 7:50 PM