Skip to content

Commit 0fb203c

Browse files
authored
Merge pull request #16 from jabbera/makeAdInteractionSimple
Use DN from UserPrincipal
2 parents 274c5c7 + 2adb0d2 commit 0fb203c

2 files changed

Lines changed: 2 additions & 63 deletions

File tree

RutaHttpModule/LdapExtensions.cs

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ internal static class LdapExtensions
1111
{
1212
private const string MembershipFilterFormatStringAllGroups = "(&(|(samAccountType=268435456)(samAccountType=268435457)(samAccountType=536870912)(samAccountType=536870913))(member:1.2.840.113556.1.4.1941:={0}))";
1313
private const string ADAttribute_CommonName = "cn";
14-
private const string ADAttribute_DistinguishedName = "distinguishedName";
1514
private const string LDAPPathPrefix = "LDAP://";
16-
private const string UserNameSearchFilterFormatString = "(&(samAccountType=805306368)(|(userPrincipalName={0})(samAccountName={0})))";
1715

1816
internal static IEnumerable<string> GetGroupsFast(this UserPrincipal user, string userContainer, string groupsContainer)
1917
{
2018
if (user == null) throw new ArgumentNullException(nameof(user));
2119

2220
using (var groupsDirectoryEntry = BindToContainer(groupsContainer))
23-
using (var userDirectoryEntry = BindToContainer(userContainer))
2421
{
25-
return GetGroupNamesForUser(user.SamAccountName, userDirectoryEntry, groupsDirectoryEntry);
22+
return SearchForUsersGroupCommonNames(groupsDirectoryEntry, user.DistinguishedName);
2623
}
2724
}
2825

@@ -58,23 +55,7 @@ private static IEnumerable<string> SearchForUsersGroupCommonNames(DirectoryEntry
5855
}
5956
}
6057
}
61-
62-
private static IEnumerable<string> GetGroupNamesForUser(string userName, DirectoryEntry userDirectoryEntry, DirectoryEntry groupsDirectoryEntry)
63-
{
64-
if (userName == null) throw new ArgumentNullException(nameof(userName));
65-
if (userDirectoryEntry == null) throw new ArgumentNullException(nameof(userDirectoryEntry));
66-
67-
var user = SearchForUser(userName, userDirectoryEntry, new[] { ADAttribute_DistinguishedName });
68-
if (user == null) return null;
69-
70-
var userDistinguishedName = ExtractUserDistinguishedName(userName, user);
71-
72-
var groupContainer = groupsDirectoryEntry; // search group context
73-
74-
// ReSharper disable once ExpressionIsAlwaysNull
75-
return SearchForUsersGroupCommonNames(groupContainer, userDistinguishedName);
76-
}
77-
58+
7859
private static DirectoryEntry BindToContainer(string container)
7960
{
8061
string path = null;
@@ -89,44 +70,5 @@ private static DirectoryEntry BindToContainer(string container)
8970
| AuthenticationTypes.ReadonlyServer // request closest read-only directory service
9071
| AuthenticationTypes.Signing);
9172
}
92-
93-
/// <summary>
94-
/// Searches for the specified user in the specified user container,
95-
/// and returns a <c>SearchResult</c> representing the user that
96-
/// contains the Active Directory properties specified in <c>adPropertiesToLoad</c>.
97-
/// </summary>
98-
private static SearchResult SearchForUser(string userName, DirectoryEntry userContainer, string[] adPropertiesToLoad)
99-
{
100-
if (userName == null) throw new ArgumentNullException(nameof(userName));
101-
if (userContainer == null) throw new ArgumentNullException(nameof(userContainer));
102-
103-
using (var userSearcher = new DirectorySearcher(userContainer))
104-
{
105-
userSearcher.Filter = string.Format(UserNameSearchFilterFormatString, userName);
106-
userSearcher.PropertiesToLoad.AddRange(adPropertiesToLoad);
107-
108-
var user = userSearcher.FindOne();
109-
110-
return user;
111-
}
112-
}
113-
114-
private static string ExtractUserDistinguishedName(string userName, SearchResult user)
115-
{
116-
var userDnValueCollection = user.Properties[ADAttribute_DistinguishedName];
117-
118-
if (userDnValueCollection == null)
119-
{
120-
var message = string.Format("Could not find the {1} property in the specified user ({0}).", userName, ADAttribute_DistinguishedName);
121-
throw new ArgumentException(message);
122-
}
123-
var userDistinguishedName = userDnValueCollection[0] as string;
124-
if (userDistinguishedName == null)
125-
{
126-
var message = $"Active Directory is broken. Retrieved user {userName} with an empty collection of {ADAttribute_DistinguishedName} values.";
127-
throw new ActiveDirectoryOperationException(message);
128-
}
129-
return userDistinguishedName;
130-
}
13173
}
13274
}

RutaHttpModuleTest/AdInteractionTest.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ public void GroupDnFilterTest()
7575
{
7676
var result = this.adInteraction.GetUserInformation(WindowsIdentity.GetCurrent().Name);
7777

78-
Assert.IsTrue(WindowsIdentity.GetCurrent().Name.EndsWith(result.login, StringComparison.Ordinal));
79-
Assert.IsNotNull(result.name);
80-
Assert.IsTrue(emailRegex.IsMatch(result.email));
8178
CollectionAssert.DoesNotContain(result.groups.ToArray(), "Domain Users");
8279
}
8380

0 commit comments

Comments
 (0)