User13622593 posted
I am trying to get the user name from active directory. I used the code given but seem to get an error :Unknown error (0x80005000) on line
using (SearchResultCollection src = ds.FindAll())
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string adPath = "ldap://oscar:389/OU=Atlanta,DC=datadist,DC=com";
//use current login as search key
string sFilter = String.Format(
"(&(objectCategory=person)(sAMAccountName={0}))",
HttpContext.Current.User.Identity.Name.Split(new char[] { '\\' })[1]
);
//this is optional, but more efficient
string[] attribs = new string[] { "cn", "sn", "givenName" };
DirectoryEntry de = new DirectoryEntry(
adPath,
null,
null,
AuthenticationTypes.Secure
);
using (de)
{
DirectorySearcher ds = new DirectorySearcher(
de,
sFilter,
attribs, //set this to null to retrieve all attribs
SearchScope.Subtree
);
SearchResult sr = null;
using (SearchResultCollection src = ds.FindAll())
{
if (src.Count > 0)
sr = src[0];
}
if (sr == null)
{
Label1.Text = "User not found...";
return;
}
StringBuilder sb = new StringBuilder();
//dump everything we have
foreach (string key in sr.Properties.PropertyNames)
{
if (sr.Properties.Contains(key))
{
foreach (object o in sr.Properties[key])
{
//output every item
sb.AppendFormat("{0} : {1}<br>", key, o);
}
}
}
Label1.Text = "Hello " + sb.ToString();
}
}
}