Skip to content

Instantly share code, notes, and snippets.

@imcbride
Created November 15, 2016 21:14
Show Gist options
  • Save imcbride/8a659946d8c8e94b930d1341b63b8d15 to your computer and use it in GitHub Desktop.
Save imcbride/8a659946d8c8e94b930d1341b63b8d15 to your computer and use it in GitHub Desktop.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.DirectoryServices;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
/// <summary>
/// Summary description for RESTfulDirectory
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class RESTfulDirectory : System.Web.Services.WebService {
public RESTfulDirectory () {
}
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void Search(string q) {
Results results = new Results();
results.directory = new List<Result>();
if (q != null && q != "")
{
// get the results from the directory
string search = "the LDAP search query";
DataTable dt = Directory.query(search, "none");
foreach (DataRow dr in dt.Rows)
{
Result result = new Result();
// populate the property fields
results.directory.Add(result);
}
}
string json = Newtonsoft.Json.JsonConvert.SerializeObject(results, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
HttpContext.Current.Response.Write(json);
}
}
public class Results
{
public List<Result> directory;
}
public class Result
{
// properties
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment