Przetczak
Przetczak

Reputation: 131

C# get pwdLastSet from AD

I tried get "pwdLastSet" from AD but have problem with data convert.

DirectoryEntry de = new DirectoryEntry();
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=" + Login + "))";
ds.SearchScope = SearchScope.Subtree;
SearchResult rs = ds.FindOne();

if (rs.GetDirectoryEntry().Properties["samaccountname"].Value == null)
{
    var window = Application.Current.Windows.OfType<MetroWindow>().FirstOrDefault();
    if (window != null)
        await window.ShowMessageAsync("error!", "error");
    return;
}
else
{
        TextBox_Password.Text = rs.GetDirectoryEntry().Properties["pwdLastSet"].Value.ToString();
}

Here, I get: System.__ComObject

I also tried:

long value = (long)rs.Properties["pwdLastSet"][0];
DateTime pwdLastSet = DateTime.FromFileTimeUtc(value);
value = long.Parse(TextBox_Password.Text);

Here, I get Exception: Incorrect input string format

Upvotes: 1

Views: 4758

Answers (1)

mm8
mm8

Reputation: 169320

You should set the TextBox_Password.Text property to a string representation of the retrieved date and not call long.Parse:

long value = (long)rs.Properties["pwdLastSet"][0];
DateTime pwdLastSet = DateTime.FromFileTimeUtc(value);
TextBox_Password.Text = pwdLastSet.ToString();

Upvotes: 1

Related Questions