Home
Manage Your Code
Snippet: Get A Property From Active Directory For A User (In this example: EmployeeId) (C#)
Title: Get A Property From Active Directory For A User (In this example: EmployeeId) Language: C#
Description: Get a property for an active directory user. In this example, employeeid is retrieved. Views: 138
Author: Kevin Shuma Date Added: 11/27/2007
Copy Code  
1                /// <summary>

2		/// Get a user's badge number from their logon name.

3		/// </summary>

4		/// <param name="DomainName">string.  The user's domain name.  </param>

5		/// <param name="UserName">string.  User name. </param>

6		/// <returns>string.  Dell badge number.</returns>

7		public static string GetBadgeNumber(string DomainName, string UserName)
8		{
9			try
10			{
11				//string DomainName = Environment.UserDomainName;

12				DirectoryEntry Entry = new DirectoryEntry("LDAP://" + DomainName);
13				DirectorySearcher Search = new DirectorySearcher(Entry);
14				Search.Filter = "(&(objectCategory=person)(samAccountName=" + UserName + "))";
15				Search.PropertiesToLoad.Add("employeeid");
16				SearchResult Result = Search.FindOne();
17				if (Result == null)
18					return string.Empty;
19				else
20					return Result.Properties["employeeid"][0].ToString();
21			}
22			catch (Exception ex)
23			{
24				Logging.LogException(MethodBase.GetCurrentMethod(), ref ex);
25				return string.Empty;
26			}
27		}