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

Posted on 2012-08-28
Last Modified: 2012-08-28
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; 
                oUser = SPContext.Current.Web.AllUsers[sUserLoginName]; 
            foreach (SPUser item in oGroupToTestFor.Users) 
                if (item.UserToken == oUser.UserToken) 
                    bUserIsInGroup = true; 
        return bUserIsInGroup;  

Open in new window


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

EDIT: This ( seems to be the answer.
Question by:tolppa
    1 Comment
    LVL 44

    Accepted Solution

    I would use the built-in options of

    Open in new window




    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    There is one common problem that all we SharePoint developers share: custom solution deployment. This topic can't be covered fully in this short article, so all I want to do in this one is to review it from a development-to-operations perspectiv…
    Microsoft SharePoint Foundation 2010 and Microsoft SharePoint Server 2010 do not offer the option to configure the location of the SharePoint diagnostic trace log files during installation.  This can, however, be configured through Central Administr…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now