1
//*********************************************************************
2
//
3
// DirectoryHelper Class
4
//
5
// Uses the DirectorySearcher .NET Framework class to retrieve user information fro
6
// the Active Directory or the WindowsSAM store.
7
//using System.Configuration;
8
//using System.DirectoryServices;
9
//*********************************************************************
10
11
public class DirectoryHelper
12
{
13
private const string _activeDirectoryPath = @"GC://";
14
private const string _filter = "(&(ObjectClass=Person)(SAMAccountName={0}))";
15
private const string _windowsSAMPath = @"WinNT://";
16
17
private static string _path = String.Empty;
18
19
//*********************************************************************
20
//
21
// Methods are all static -- don't allow construction.
22
//
23
//*********************************************************************
24
25
private DirectoryHelper()
26
{
27
}
28
29
//*********************************************************************
30
//
31
// This does the core directory searching using the filters specified above.
32
//
33
//*********************************************************************
34
35
public static bool FindUser(string identification, ref string FirstName, ref string LastName)
36
{
37
bool result = false;
38
39
// Determine which method to use to retrieve user information
40
41
// WindowsSAM
42
if (ConfigurationSettings.AppSettings[Web.Global.CfgKeyUserAcctSource] == "WindowsSAM")
43
{
44
// Extract the machine or domain name and the user name from the
45
// identification string
46
string [] samPath = identification.Split(new char[] {'\\'});
47
_path = _windowsSAMPath + samPath[0];
48
49
try
50
{
51
// Find the user
52
DirectoryEntry entryRoot = new DirectoryEntry(_path);
53
DirectoryEntry userEntry = entryRoot.Children.Find(samPath[1], "user");
54
LastName = userEntry.Properties["FullName"].Value.ToString();
55
}
56
catch
57
{
58
result = false;
59
}
60
result = true;
61
}
62
63
// Active Directory
64
else if (ConfigurationSettings.AppSettings[Web.Global.CfgKeyUserAcctSource] == "ActiveDirectory")
65
{
66
_path = _activeDirectoryPath;
67
68
// Setup the filter
69
identification = identification.Substring(identification.LastIndexOf(@"\") + 1,
70
identification.Length - identification.LastIndexOf(@"\")-1);
71
string userNameFilter = string.Format(_filter, identification);
72
73
// Get a Directory Searcher to the LDAPPath
74
DirectorySearcher searcher = new DirectorySearcher(_path);
75
if (searcher == null)
76
{
77
return false;
78
}
79
80
// Add the properties that need to be retrieved
81
searcher.PropertiesToLoad.Add("givenName");
82
searcher.PropertiesToLoad.Add("sn");
83
84
// Set the filter for the search
85
searcher.Filter = userNameFilter;
86
87
try
88
{
89
// Execute the search
90
SearchResult search = searcher.FindOne();
91
92
if (search != null)
93
{
94
FirstName = SearchResultProperty(search, "givenName");
95
LastName = SearchResultProperty(search, "sn");
96
result = true;
97
}
98
else
99
result = false;
100
}
101
catch
102
{
103
result = false;
104
}
105
}
106
else
107
{
108
// The user has not choosen an UserAccountSource or UserAccountSource as None
109
// Usernames will be displayed as "Domain/Username"
110
result = false;
111
}
112
113
return result;
114
}
115
116
//*********************************************************************
117
//
118
// Retrieves the specified property from the SearchResult collection.
119
//
120
//*********************************************************************
121
122
private static String SearchResultProperty(SearchResult sr, string field)
123
{
124
if (sr.Properties[field] != null)
125
{
126
return (String)sr.Properties[field][0];
127
}
128
129
return null;
130
}
131
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131
