Query AD from c#

Posted on 2012-09-18
Last Modified: 2012-09-18
Hi Experts,

This is a follow-on from an earlier question, I'm just feeling my way into querying AD from an c# web app:

In the code below I have a few lines that get the identity of the person logged in, I'd like to use this for my bool test so that I now look up the person logged in and confirm they exist in AD.

My plan is to then go on and retrieve certain details based on their identity:

Be grateful if someone could show me how to use my Session["username"] in the ("domain\\username") path, I have tried a few variations but clearly missing the obvious here.

protected void Page_Load(object sender, EventArgs e)

        System.Security.Principal.IPrincipal user;

        user = System.Web.HttpContext.Current.User;

        System.Security.Principal.IIdentity identity;

        identity = user.Identity;

        Session["username"] = identity.Name.Substring(identity.Name.IndexOf(@"\") + 1);

               bool test = IsExistInAD("domain\\username");
               if (test == true)
                   Response.Write("user exists");
                   Response.Write("user doesn't exist");


Open in new window

Question by:forsters
    LVL 35

    Accepted Solution

    just do

     bool test = IsExistInAD("domain\\" +  Session["username"] );
    LVL 35

    Expert Comment


    string user;
    usr="domain\\" +  Session["username"] ;

    bool test = IsExistInAD(USR);

    Author Closing Comment

    Perfect thank you, I knew it would be something simple, was trying to contain the whole path in my " ", sighs.

    Thank you so much

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Top Freelancers to Complete C# Projects

    Source the talented Expert Exchange community
    for top quality work on your C# projects.

    Hire the best. Collaborate easily. Get quality work.

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

    760 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

    7 Experts available now in Live!

    Get 1:1 Help Now