TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Get Userdetails from Active Directory
Shridhar Sharma
Aug 12
2015
Code
1.1
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
public
string
[] getUserDetails(
string
Username,
string
strPwd,
string
strAccountName)
{
string
[] resultSet =
new
string
[6];
string
strUsername =
string
.Empty;
//string domainAndUsername = strDomain + @"\" + strUsername;
DirectoryEntry entry =
new
DirectoryEntry(_path, domainAndUsername, strPwd);
try
{
DirectorySearcher search =
new
DirectorySearcher(entry);
if
(strAccountName.Contains(@
"\"
))
{
strUsername = strAccountName.Substring(strAccountName.IndexOf(@
"\"
) + 1);
}
else
if
(strAccountName.Contains(
"@"
))
{
strUsername = strAccountName.Substring(0, strAccountName.IndexOf(
"@"
));
}
else
{
strUsername = strAccountName;
}
search.Filter =
"(SAMAccountName="
+ strUsername +
")"
;
search.PropertiesToLoad.Add(
"Email"
);
search.PropertiesToLoad.Add(
"Department"
);
search.PropertiesToLoad.Add(
"Manager"
);
search.PropertiesToLoad.Add(
"Location"
);
search.PropertiesToLoad.Add(
"Telephone"
);
SearchResult result = search.FindOne();
resultSet[0] = (result.Properties[
"Email"
]).Count == 0 ?
string
.Empty : (
string
)result.Properties[
"Email"
][0]; ;
resultSet[1] = (result.Properties[
"Department"
]).Count == 0 ?
string
.Empty : (
string
)result.Properties[
"Department"
][0];
resultSet[2] = (result.Properties[
"Manager"
]).Count == 0 ?
string
.Empty : (
string
)result.Properties[
"Manager"
][0];
resultSet[3] = (result.Properties[
"Location"
]).Count == 0 ?
string
.Empty : (
string
)result.Properties[
"Location"
][0];
resultSet[4] = (result.Properties[
"Telephone"
]).Count == 0 ?
string
.Empty : (
string
)result.Properties[
"Telephone"
][0];
if
(
null
== result)
{
return
resultSet;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (
string
)result.Properties[
"cn"
][0];
}
catch
(Exception ex)
{
throw
ex;
}
return
resultSet;
}
Active directory