Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ArrayList Groups(string userDn, bool recursive)
- {
- ArrayList groupMemberships = new ArrayList();
- return AttributeValuesMultiString("memberOf", userDn,
- groupMemberships, recursive);
- }
- public ArrayList AttributeValuesMultiString(string attributeName,
- string objectDn, ArrayList valuesCollection, bool recursive)
- {
- DirectoryEntry ent = new DirectoryEntry(objectDn);
- PropertyValueCollection ValueCollection = ent.Properties[attributeName];
- IEnumerator en = ValueCollection.GetEnumerator();
- while (en.MoveNext())
- {
- if (en.Current != null)
- {
- if (!valuesCollection.Contains(en.Current.ToString()))
- {
- valuesCollection.Add(en.Current.ToString());
- if (recursive)
- {
- AttributeValuesMultiString(attributeName, "LDAP://" +
- en.Current.ToString(), valuesCollection, true);
- }
- }
- }
- }
- ent.Close();
- ent.Dispose();
- return valuesCollection;
- }
- public static bool IsUserInGroup(string username, string groupname, ContextType type)
- {
- PrincipalContext context = new PrincipalContext(type);
- UserPrincipal user = UserPrincipal.FindByIdentity(
- context,
- IdentityType.SamAccountName,
- username);
- GroupPrincipal group = GroupPrincipal.FindByIdentity(
- context, groupname);
- return user.IsMemberOf(group);
- }
- private int validateUserActiveDirectory()
- {
- IntPtr token = IntPtr.Zero;
- int DBgroupLevel = 0;
- // make sure you're yourself -- recommended at msdn http://support.microsoft.com/kb/248187
- RevertToSelf();
- if (LogonUser(txtUserName.Value, propDomain, txtUserPass.Text, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) != 0) {
- // ImpersonateLoggedOnUser not required for us -- we are not doing impersonated stuff, but leave it here for completeness.
- //ImpersonateLoggedOnUser(token);
- // do impersonated stuff
- // end impersonated stuff
- // ensure that we are the original user
- CloseHandle(token);
- RevertToSelf();
- System.Security.Principal.IdentityReferenceCollection groups = Context.Request.LogonUserIdentity.Groups;
- IdentityReference translatedGroup = default(IdentityReference);
- foreach (IdentityReference g in groups) {
- translatedGroup = g.Translate(typeof(NTAccount));
- if (translatedGroup.Value.ToLower().Contains("desired group")) {
- inDBGroup = true;
- return 1;
- }
- }
- }
- else {
- return 0;
- }
- }
Add Comment
Please, Sign In to add comment