/
ADInformation.cs
169 lines (147 loc) · 5.51 KB
/
ADInformation.cs
1
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EmTrac2SF
{
public class ADInformation
{
public string SAMAccountName;
public string userAccountControl;
public string givenName;
public string sn;
public string LastName { get { return sn; } set { sn = value; } }
public string FirstName { get { return givenName; } set { givenName = value; } }
public string LastFirstName { get{ return string.Concat(sn, ", ", givenName );}}
public string TelephoneNumber;
public string mail;
public string title;
public string department;
public string company;
public string physicalDeliveryOfficeName;
public string displayName;
public bool Active;
public bool Found = false;
public ADInformation(string strUserId)
{
SAMAccountName = strUserId;
LastName = "";
FirstName = "";
Found = GetADInformation();
}
public ADInformation( string strLastName, string strFirstName )
{
SAMAccountName = "";
LastName = strLastName;
FirstName = strFirstName;
Found = GetADInformation();
}
public bool GetADInformation()
{
string strUserId = "", strFilter = "";
if(!SAMAccountName.Equals( "" ))
{
strUserId = SAMAccountName;
if(strUserId.Contains( @"\" ))
strUserId = strUserId.Substring( 5 );
// only EmCare/EMSC users
strFilter = string.Format( "(|(&(objectClass=User)(sAMAccountName={0})(|(company=EmCare*)(company=EMSC*))))", strUserId );
}
if(!LastName.Equals( "" ))
// only EmCare/EMSC users
strFilter = string.Format( "(|(&(objectClass=User)(givenname={0})(sn={1})(|(company=EmCare*)(company=EMSC*))))", FirstName, LastName );
string strServer = System.Configuration.ConfigurationManager.AppSettings["EMSC"].ToString();
string strADUser = System.Configuration.ConfigurationManager.AppSettings["LDAPUID"].ToString();
string strADPwd = System.Configuration.ConfigurationManager.AppSettings["LDAPPwd"].ToString();
string sLDAPPath = string.Format("LDAP://{0}/DC=EMSC,DC=root01,DC=org", strServer);
System.DirectoryServices.DirectoryEntry objDE = null;
System.DirectoryServices.DirectorySearcher objDS = null;
try
{
objDE = new System.DirectoryServices.DirectoryEntry( sLDAPPath, strADUser, strADPwd, System.DirectoryServices.AuthenticationTypes.Secure );
objDS = new System.DirectoryServices.DirectorySearcher( objDE );
// get the LDAP filter string based on selections
objDS.Filter = strFilter;
objDS.ReferralChasing = System.DirectoryServices.ReferralChasingOption.None;
//String strResult = String.Format(
//"(&(objectClass={0})(givenname={1})(sn={2}))",
//sLDAPUserObjectClass, sFirstNameSearchFilter, sLastNameSearchFilter);
//string sFilter =
//String.Format("(&(objectclass=user)(MemberOf=CN={0},OU=Groups,DC={1},DC=root01,DC=org))",
// strGroupName, strDomain);
objDS.PropertiesToLoad.Add( "userAccountControl" );
objDS.PropertiesToLoad.Add( "SAMAccountName" );
objDS.PropertiesToLoad.Add( "givenName" );
objDS.PropertiesToLoad.Add( "sn" );
objDS.PropertiesToLoad.Add( "TelephoneNumber" );
objDS.PropertiesToLoad.Add( "mail" );
objDS.PropertiesToLoad.Add( "title" );
objDS.PropertiesToLoad.Add( "department" );
objDS.PropertiesToLoad.Add( "company" );
objDS.PropertiesToLoad.Add( "physicalDeliveryOfficeName" );
objDS.PropertiesToLoad.Add( "displayName" );
//start searching
System.DirectoryServices.SearchResultCollection objSRC = objDS.FindAll();
try
{
if( objSRC.Count != 0 )
{
//if(objSRC.Count > 1)
// Found = Found;
// grab the first search result
System.DirectoryServices.SearchResult objSR = objSRC[ 0 ];
Found = true;
displayName = objSR.Properties[ "displayName" ][ 0 ].ToString();
givenName = objSR.Properties[ "givenName" ][ 0 ].ToString();
sn = objSR.Properties[ "sn" ][ 0 ].ToString();
SAMAccountName = objSR.Properties[ "SAMAccountName" ][ 0 ].ToString();
userAccountControl = objSR.Properties[ "userAccountControl" ][ 0 ].ToString();
int iInactiveFlag = Convert.ToInt32( userAccountControl );
iInactiveFlag = iInactiveFlag & 0x0002;
Active = iInactiveFlag <= 0;
if( objSR.Properties[ "TelephoneNumber" ].Count > 0 )
TelephoneNumber = objSR.Properties[ "TelephoneNumber" ][ 0 ].ToString();
if( objSR.Properties[ "mail" ].Count > 0 )
mail = objSR.Properties[ "mail" ][ 0 ].ToString();
if( objSR.Properties[ "title" ].Count > 0 )
title = objSR.Properties[ "title" ][ 0 ].ToString();
if( objSR.Properties[ "department" ].Count > 0 )
department = objSR.Properties[ "department" ][ 0 ].ToString();
if( objSR.Properties[ "company" ].Count > 0 )
company = objSR.Properties[ "company" ][ 0 ].ToString();
if( objSR.Properties[ "physicalDeliveryOfficeName" ].Count > 0 )
physicalDeliveryOfficeName = objSR.Properties[ "physicalDeliveryOfficeName" ][ 0 ].ToString();
}
else
{
Found = false;
return Found;
}
}
catch( Exception )
{
// ignore errors
Found = false;
return false;
}
finally
{
objDE.Dispose();
objSRC.Dispose();
//objDS.Dispose();
}
}
catch( Exception )
{
// ignore errors
Found = false;
return false;
}
finally
{
objDS.Dispose();
}
return Found;
}
}
}