user113784
user113784

Reputation: 47

C# AD users password expiry

I am using following code in form but I get an error

public DataTable Passwordexpire()
{
   PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

   UserPrincipal userTemplate = new UserPrincipal(ctx);
   userTemplate.AdvancedSearchFilter.AccountExpirationDate(DateTime.Today.AddDays(3), MatchType.LessThanOrEquals);

   PrincipalSearcher searcher = new PrincipalSearcher(userTemplate);


  foreach (Principal foundPrincipal in searcher.FindAll())
  {
     UserPrincipal foundUser = (foundPrincipal as UserPrincipal);

     if (foundUser != null)
     {
       DataTable dt = new DataTable();
       dt.Columns.Add("AccountName");
       dt.Columns.Add("Name");
       dt.Columns.Add("Empolyee ID");
       dt.Columns.Add("Company");

       foreach (SearchResult sResultSet in Dsearch.FindAll())
       {
         DataRow dr = dt.NewRow();
           dr[0] = (GetProperty(sResultSet, "samaccountname"));
           dr[1] = (GetProperty(sResultSet, "name"));
          dr[2] = (GetProperty(sResultSet, "ExtensionAttribute2"));
          dr[3] = (GetProperty(sResultSet, "Company"));
          dt.Rows.Add(dr);
       }
       return dt;
   }
}

The error is:

The type or namespace name User Principal could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name Principal Searcher could not be found (are you missing a using directive or an assembly reference?)
The name 'MatchType' does not exist in the current context

Upvotes: 3

Views: 541

Answers (2)

Jeremy Thompson
Jeremy Thompson

Reputation: 65544

Add a Reference to the System.DirectoryServices .Net DLL.

Then zaitsman's answer:

using System.DirectoryServices.AccountManagement;

Upvotes: 1

zaitsman
zaitsman

Reputation: 9499

Do you have the using System.DirectoryServices.AccountManagement; directive at the top of the .cs file in which this Passwordexpire method exists?

Upvotes: 3

Related Questions