public static UserMembershipResult GenerateMembershipResultForInvalidUser() { UserMembershipResult result = new UserMembershipResult(); result.user = TestConstants.UserInvalidPrimary; result.code = 2; result.message = "Input user not found in Active Directory"; return(result); }
public static UserMembershipResult GenerateMembershipResultForValidUserWithNoMatchingException() { UserMembershipResult result = new UserMembershipResult(); result.user = TestConstants.UserNoMatching; result.code = 1; result.message = "An error occurred while enumerating the groups. The group could not be found."; return(result); }
public static UserMembershipResult GenerateMembershipResultForValidUser() { UserMembershipResult result = new UserMembershipResult(); result.user = TestConstants.UserPrimary; result.code = 0; result.message = "Group list generated successfully"; return(result); }
/// <summary> /// This method receives a UserMembershipResult object containing a username to check /// and get all of the AD groups the user is a member of /// </summary> /// <param name="membershipResult">A UserMembershipResult object containing a username /// to check.</param> /// <returns>A UserMembershipResult object containing the username to check, a result message, /// a status code for the AD query and the list of groups the user is a member of.</returns> public static UserMembershipResult GetAllGroupsByUser(UserMembershipResult membershipResult) { // Create output group names list. membershipResult.groups = new List <string>(); membershipResult.message = string.Empty; // Set up domain context. PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain, Constants.adQueryUser, Constants.adQueryPassword); // Find the user in AD. UserPrincipal user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, membershipResult.user); if (user != null) { PrincipalSearchResult <Principal> groups = user.GetAuthorizationGroups(); // Defining iterator object. var iterator = groups.GetEnumerator(); using (iterator) { while (iterator.MoveNext()) { try { Principal p = iterator.Current; if (p is GroupPrincipal) { membershipResult.groups.Add(p.SamAccountName); } } catch (NoMatchingPrincipalException pex) { if (membershipResult.code != 1) { membershipResult.message = pex.Message; membershipResult.code = 1; } continue; } } } if (membershipResult.message == string.Empty) { membershipResult.message = "Group list generated successfully"; membershipResult.code = 0; } return(membershipResult); } membershipResult.message = "Input user not found in Active Directory"; membershipResult.code = 2; return(membershipResult); }
public HttpResponseMessage UserGroupMembership(string user) { HttpResponseMessage response = new HttpResponseMessage(); UserMembershipResult result = new UserMembershipResult(); result.user = user; result = ActiveDirectory.GetAllGroupsByUser(result); response = Request.CreateResponse(HttpStatusCode.OK, result); return(response); }