views:

215

answers:

1

Hi,

Say that i have an active directory group called "Group1". I add this Group1 to a SharePoint site ( People and Group >> New User).

All the users that are part of Group1 can now access sharepoint site.

Now,for a specific custom webpart, i need to target to only users who belong to Group1. But when a user who belongs to Group1 logs into the sharepoint site, i am not finding a way to determine from within SharePoint context if this user belongs to Group1. Any thoughts?

Thanks, Faiz

+1  A: 

Use the following code to check if the current user is in the given security group:

WindowsIdentity wi = WindowsIdentity.GetCurrent();
WindowsPrincipal wp = new WindowsPrincipal(wi);
wp.IsInRole("Group1");
Jason
That gives only SPGroups. I need an ad group. But when we add this to SharePoint it takes as SPUser so i am not able to get users from this SPUser
Faiz
I do not really understand your comment... The code I've posted checks if the current identity belongs to a given ad security group. You do not have to retrieve the SPUser object of the security group to check group membership for the current user. Please post some code if you already wrote some thing...
Jason
When i replied, your code was querying for SPGroup. I guess an edit has happened. Thank you for your reply.
Faiz