LDAP samples

Authentication:

//LoadAssembly:System.dll
//LoadAssembly:System.DirectoryServices.dll

using System;
using System.DirectoryServices;
using NSi.AutoStore.Capture.DataModel;

public static class Script
{
  public static void CustomAuthenticate(MetadataCollection authData, AuthResult result)
  {
    //Sample custom authentication for a configuration with two authentication prompts: Username and Password
    string username = authData.GetData("Username");
    string password = authData.GetData("Password");

    DirectoryEntry user = new DirectoryEntry("<LDAP Server>", username, password, AuthenticationTypes.Secure);
    if (user != null)
    {
      result.IsAuthenticated = true;
      result.UserInfo = new UserInfo();
      result.UserInfo.UserName = user.Username;
    }
  }
}

Data lookup:

//LoadAssembly:System.dll
//LoadAssembly:System.DirectoryServices.dll

using System;
using System.DirectoryServices;
using NSi.AutoStore.Capture.DataModel;

public static class Script
{
  public static void Form_OnLoad(MFPEventData eventData)
  {
    //Initialize dynamic fields
    ListField userList = new ListField();
    userList.Name = "UserList";
    userList.Display = "User List";
    userList.RaiseFindEvent = true;
    //Can populate data here
    PopulateUser(userList, null);
    eventData.Form.Fields.Add(userList);
  }

  public static void UserList_OnFind(MFPEventData eventData)
  {
    //Initialize dynamic fields
    ListField userList = (ListField)eventData.Form.Fields.GetField("UserList");
    PopulateUser(userList, eventData.SearchCriteria);
  }

  private static void PopulateUser(ListField userList, string searchCriteria)
  {
    if (userList == null)
    {
      userList = new ListField();
    }
    userList.Items.Clear();
    bool isSearchable = string.IsNullOrEmpty(searchCriteria) ? false : true;
    DirectoryEntry root = new DirectoryEntry("<LDAP Server>", "<username>", "<password>", AuthenticationTypes.Secure);
    DirectorySearcher searcher = new DirectorySearcher(root);
    string query = string.Empty;
    if (isSearchable)
    {
      query = string.Format("(&(objectClass=user)(objectCategory=person)(name={0}*))", searchCriteria);
    }
    else
    {
      query = "(&(objectClass=user)(objectCategory=person))";
    }
    searcher.Filter = query;
    searcher.SearchScope = SearchScope.Subtree;

    SearchResultCollection results = searcher.FindAll();
    if (results != null)
    {
      for (int i = 0; i < results.Count; i++)
      {
        SearchResult result = results[i];
        ListItem item = new ListItem((String)result.Properties["samaccountname"][0], i.ToString());
        userList.Items.Add(item);
      }
    }
  }
}