Link to home
Start Free TrialLog in
Avatar of tolppa
tolppa

asked on

SP2010: How to check if current user exist in given AD group?

I have users who are on a AD group (ADGroup1) and I have a sharepoint group (SPGroup1) which has the above AD group . My question is: how do I check if current user is member of given AdGroup?

private Boolean isUserInGroup(SPGroup oGroupToTestFor, String sUserLoginName) 
    {    
        Boolean bUserIsInGroup = false; 
 
            SPUser oUser =null; 
            try{ 
                oUser = SPContext.Current.Web.AllUsers[sUserLoginName]; 
                } 
            catch{} 
            if(oUser!=null){ 
            foreach (SPUser item in oGroupToTestFor.Users) 
            { 
                if (item.UserToken == oUser.UserToken) 
                { 
                    bUserIsInGroup = true; 
                    break; 
                }                     
            } 
            } 
        return bUserIsInGroup;  
    } 

Open in new window

(http://stackoverflow.com/questions/1063931/sharepoint-check-if-a-user-is-member-of-a-group)

does not work if user exist on AD group (ADGroup1) which exist on sharepoint group (SPGroup1).

EDIT: This (http://chrissyblanco.blogspot.fi/2007/07/determining-whether-user-is-member-of.html) seems to be the answer.
ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial