This course will introduce you to C++ 11 and teach you about syntax fundamentals.
Do more with
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="WebApplication3.index" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<ajax:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" ></ajax:ToolkitScriptManager>
<div>
<asp:TextBox ID="txtUserName" runat="server" AutoCompleteType="None"></asp:TextBox>
<ajax:AutoCompleteExtender ID="AutoCompleteExtender1" ServiceMethod="findEmp"
MinimumPrefixLength="2"
CompletionInterval="100"
EnableCaching="true"
CompletionSetCount="10"
TargetControlID="txtUserName"
runat="server" ></ajax:AutoCompleteExtender>
<br />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication3
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static string[] findEmp(string prefixText, int count)
{
List<string> items = new List<string>();
return items.ToArray();
}
}
}
$('#<%= txtName.ClientID %>').autocomplete({
source: function(request, response) {
$.ajax({
url: "ADS.asmx/findit",
data: "{ 'prefixText': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function(data) { return data; },
success: function(data) {
response($.map(data.d, function(item) {
return {
value: item
}
}))
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
minLength: 2
});
public static string FriendlyDomainToLdapDomain(string friendlyDomainName)
{
string ldapPath = null;
try
{
DirectoryContext objContext = new DirectoryContext(
DirectoryContextType.Domain, friendlyDomainName);
Domain objDomain = Domain.GetDomain(objContext);
ldapPath = objDomain.Name;
}
catch (DirectoryServicesCOMException e)
{
ldapPath = e.Message.ToString();
}
return ldapPath;
}
public static string GetDomainDN(string domain)
{
DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domain);
Domain d = Domain.GetDomain(context);
DirectoryEntry de = d.GetDirectoryEntry();
return de.Properties["DistinguishedName"].Value.ToString();
}
public static List<string> GetADUsers2_0(string searchTerm)
{
List<string> res = new List<string>();
// define a directory searcher for your default context
string searchRootLDAPPath = GetDomainDN(Domain.GetCurrentDomain().Name);
using (DirectoryEntry defaultDE = new DirectoryEntry(String.Format("LDAP://{0}", searchRootLDAPPath)))
{
// define searcher - search through entire subtree, search for users
// (objectCategory=Person)
using (DirectorySearcher dsAllUsers = new DirectorySearcher(defaultDE))
{
dsAllUsers.SearchScope = SearchScope.Subtree;
dsAllUsers.Filter = String.Format("(&(objectCategory=person)(objectClass=user)(cn={0}*))", searchTerm);
// get the results
SearchResultCollection result = dsAllUsers.FindAll();
foreach (SearchResult searchResult in result)
{
res.Add(searchResult.GetDirectoryEntry().Name.Remove(0, 3)); //removing the CN=
}
}
}
return res;
}
private static List<string> GetADUsers(string searchTerm)
{
List<string> res = new List<string>();
using (var context = new PrincipalContext(ContextType.Domain, Domain.GetCurrentDomain().Name))
{
// create a principal object representation to describe
// what will be searched
UserPrincipal user = new UserPrincipal(context);
// define the properties of the search (this can use wildcards)
user.Enabled = true;
user.Name = String.Format("{0}*", searchTerm);
// create a principal searcher for running a search operation
using (PrincipalSearcher pS = new PrincipalSearcher())
{
// assign the query filter property for the principal object
// you created
// you can also pass the user principal in the
// PrincipalSearcher constructor
pS.QueryFilter = user;
// run the query
PrincipalSearchResult<Principal> results = pS.FindAll();
foreach (Principal result in results)
{
res.Add(result.Name);
}
}
}
return res;
}
In order for this to work you have to add reference to System.DirectoryServices.Apublic static string GetDomainDN(string domain)
{
DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domain);
Domain d = Domain.GetDomain(context);
DirectoryEntry de = d.GetDirectoryEntry();
return de.Properties["DistinguishedName"].Value.ToString();
}
[WebMethod]
public List<string> GetADUsers(string term)
{
List<string> res = new List<string>();
// define a directory searcher for your default context
string searchRootLDAPPath = GetDomainDN(Domain.GetCurrentDomain().Name);
using (DirectoryEntry defaultDE = new DirectoryEntry(String.Format("LDAP://{0}", searchRootLDAPPath)))
{
// define searcher - search through entire subtree, search for users
// (objectCategory=Person)
using (DirectorySearcher dsAllUsers = new DirectorySearcher(defaultDE))
{
dsAllUsers.SearchScope = SearchScope.Subtree;
dsAllUsers.Filter = String.Format("(&(objectCategory=person)(objectClass=user)(cn={0}*))", term);
// get the results
SearchResultCollection result = dsAllUsers.FindAll();
foreach (SearchResult searchResult in result)
{
res.Add(searchResult.GetDirectoryEntry().Name.Remove(0, 3)); //removing the CN=
}
}
}
return res;
}
<script type="text/javascript">
$(function() {
$("#<%= txtName.ClientID %>").autocomplete({
source: function(request, response) {
$.ajax({
url: "ADS.asmx/GetADUsers",
data: "{ 'term': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function(data) { return data; },
success: function(data) {
response($.map(data.d, function(item) {
return {
value: item
}
}))
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
minLength: 2
});
});
</script>
[WebMethod]
public List<string> GetADUsers(string term)
{
List<string> res = new List<string>();
// define a directory searcher for your default context
try
{
string searchRootLDAPPath = GetDomainDN(Domain.GetCurrentDomain().Name);
using (DirectoryEntry defaultDE = new DirectoryEntry(String.Format("LDAP://{0}", searchRootLDAPPath)))
{
// define searcher - search through entire subtree, search for users
// (objectCategory=Person)
using (DirectorySearcher dsAllUsers = new DirectorySearcher(defaultDE))
{
dsAllUsers.SearchScope = SearchScope.Subtree;
dsAllUsers.Filter = String.Format("(&(objectCategory=person)(objectClass=user)(cn={0}*))", term);
// get the results
SearchResultCollection result = dsAllUsers.FindAll();
foreach (SearchResult searchResult in result)
{
res.Add(searchResult.GetDirectoryEntry().Name.Remove(0, 3)); //removing the CN=
}
}
}
}
catch (Exception ex)
{
res.Add(String.Format("Error: {0}", ex.Message));
}
return res;
}
The error message occurs when you are not in a domain. At least in my part. At work where I am in a domain works great.public static string GetDomainDN(string domain)
{
DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domain);
Domain d = Domain.GetDomain(context);
DirectoryEntry de = d.GetDirectoryEntry();
return de.Properties["DistinguishedName"].Value.ToString();
}
Premium Content
You need an Expert Office subscription to comment.Start Free Trial