Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Query AD from asp.net c#

Posted on 2012-09-18
Medium Priority
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 asp.net 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
  • 2
LVL 35

Accepted Solution

YZlat earned 2000 total points
ID: 38410091
just do

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

Expert Comment

ID: 38410094

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

bool test = IsExistInAD(USR);

Author Closing Comment

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

Thank you so much

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
Sometimes it necessary to set special permissions on user objects.  For instance when using a Blackberry server, the SendAs permission needs to be set. I see many admins struggle with the setting that permission only to see it disappear within a few…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

581 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