var principalContext =
new
PrincipalContext(ContextType.Domain,
"easyas.com"
,
"fred"
"fredsPassword"
);
GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName);
var members = groupPrincipal.GetMembers(
true
var membersList =
ArrayList();
foreach
(Principal member
in
members)
{
UserPrincipal userPrincipal = member
as
UserPrincipal;
if
(userPrincipal ==
null
)
continue
;
(!membersList.Contains(userPrincipal.DisplayName))
membersList.Add(userPrincipal.DisplayName);
}
var output =
StringBuilder();
output.AppendLine(String.Format(
"<span>Users in the {0} group:</span><br/>"
, groupName));
output.AppendLine(
"<ul>"
(
string
s
membersList)
"<li>{0}</li>"
, s));
"</ul>"
private
String GetMarketingUsersAsHtmlList()
const
adDomainName =
adUserAccount =
adUserAccountPassword =
"HokeyPokey"
PrincipalContext(ContextType.Domain, adDomainName, adUserAccount, adUserAccountPassword);
groupName =
"Marketing"
(groupPrincipal ==
return
String.Empty;
try
output.ToString();
finally
groupPrincipal.Dispose();
using
System;
System.ComponentModel;
System.DirectoryServices.AccountManagement;
System.Text;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
Microsoft.SharePoint;
namespace
SharePointTesting.IsUserAMember
[ToolboxItemAttribute(
false
)]
public
class
IsUserAMember : WebPart
Label _results;
static
PrincipalContext GetPrincipalContext
get
"svcAdQueryAccount"
"svcAdQueryAccountPassword"
principalContext;
Boolean IsUserMemberOfGroup(GroupPrincipal groupPrincipal, String identity, IdentityType identityType)
UserPrincipal userPrincipal = GetUser(identity, identityType);
userPrincipal.IsMemberOf(groupPrincipal);
UserPrincipal GetUser(String identity, IdentityType identityType)
PrincipalContext principalContext = GetPrincipalContext;
UserPrincipal.FindByIdentity(principalContext, identityType, identity);
GroupPrincipal GetGroup(String groupName)
GroupPrincipal.FindByIdentity(principalContext, IdentityType.Name, groupName);
protected
override
void
OnInit(EventArgs e)
base
.OnInit(e);
_results =
Label();
CreateChildControls()
Controls.Add(_results);
OnPreRender(EventArgs e)
.OnPreRender(e);
StringBuilder output =
GroupPrincipal group = GetGroup(
"Developers"
(group ==
_results.Text =
"Group not found."
output.Append(String.Format(
"Current user, {0}, {1} a member of {2}"
, SPContext.Current.Web.CurrentUser.Name, IsUserMemberOfGroup(group, SPContext.Current.Web.CurrentUser.Sid, IdentityType.Sid) ?
"is"
:
"is not"
, group.DisplayName));
output.Append(
"<br/>"
_results.Text = output.ToString();
System.Collections.Generic;
System.Web;
System.Web.UI;
Microsoft.SharePoint.WebControls;
SharePointTesting.GetMembersInGroup
GetMembersInGroup : WebPart
List<UserPrincipal> GetAllUsersInGroup(GroupPrincipal groupPrincipal, Boolean recurse)
List<UserPrincipal> userPrincipals =
List<UserPrincipal>();
PrincipalSearchResult<Principal> members = groupPrincipal.GetMembers(recurse);
(Principal principal
var userPrincipal = principal
userPrincipals.Add(userPrincipal);
userPrincipals;
var groupMembers = GetAllUsersInGroup(group,
String members = String.Empty;
(UserPrincipal userPrincipal
groupMembers)
members = String.Format(
"{0}{1}"
, String.IsNullOrEmpty(members) ?
""
: String.Format(
"{0}, "
, members), userPrincipal.DisplayName);
"<br/><br/>"
"The current list of users in the {0} group are: {1}"
, group.DisplayName, members));
UserPrincipal GetUser(String samAccountName)
UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, samAccountName);
Boolean IsUserMemberOfGroup(GroupPrincipal groupPrincipal, String samAccountName)
UserPrincipal userPrincipal = GetUser(samAccountName);
Boolean IsUserMemberOfGroup(GroupPrincipal groupPrincipal, UserPrincipal userPrincipal)
AddUserToGroup(GroupPrincipal groupPrincipal, String samAccountName)
throw
NullReferenceException(
"The user does not exist."
AddUserToGroup(groupPrincipal,userPrincipal)
userPrincipal.Dispose();
AddUserToGroup(GroupPrincipal groupPrincipal, UserPrincipal userPrincipal)
(!userPrincipal.IsMemberOf(groupPrincipal))
groupPrincipal.Members.Add(userPrincipal);
groupPrincipal.Save();
RemoveUserFromGroup(GroupPrincipal groupPrincipal, String samAccountName)
RemoveUserFromGroup(groupPrincipal,userPrincipal)
RemoveUserFromGroup(GroupPrincipal groupPrincipal, UserPrincipal userPrincipal)
(userPrincipal.IsMemberOf(groupPrincipal))
groupPrincipal.Members.Remove(userPrincipal);