C# Is the Logged User a member of specific Windows Domain Group?

2013/10/19 11:10
C sharp code
using System.DirectoryServices.AccountManagement;
string userName = SystemInformation.UserName;
string UDN = SystemInformation.UserDomainName;
string domainUser = UDN + "\\" + userName;
PrincipalContext pc = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, domainUser);
GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, "Domain_Group_Name");
bool is_member = user.IsMemberOf(group);