Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6223119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:25:57+00:00 2026-05-24T08:25:57+00:00

I have seen some code for working with AD in this stackoverflow question I

  • 0

I have seen some code for working with AD in this stackoverflow question

I am getting confused about the using statement. I thought that it was just used for things that you are worried could become memory leak, like a WebClient, or similar…

Anyway:

using (var context = new PrincipalContext( ContextType.Domain )) 
 {
      using (var user = UserPrincipal.FindByIdentity( context, "username" ))
      {
          var groups = user.GetAuthorizationGroups();
          ...
      }
 }

when I reach the line var groups = user.GetAuthorizationGroups() – user is null, so that line fails with a NullReference. When I mouse over it debugging it shows null, then shows Static Members, and has all the values.

If I take the line out of using statement and just have var user = UserPrincipal.FindByIdentity( context, "username" ) user is populated as required.

So what’s going on ???

Edit: I stuffed up and was sending a bogus username. Oddly though when I check the variables during debug, when you would expect user to be completely null if I sent the bogus userid, but it showed under user: null, static members, and there had values for what I was currently logged in as-so I thought it was to do with the using statement potentially.
Cheers!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:25:58+00:00Added an answer on May 24, 2026 at 8:25 am

    What you’re describing cannot happen. The specialness of a using statement doesn’t happen until the block is completed, when the object gets disposed. So inside that block, the user variable is the same regardless of whether it’s in a using statement or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today, I have seen some legacy code. In the destructor there is a statement
I have seen a couple of instances now where some legacy code I'm working
I have seen some other posts on how to get this working ... however
In some code I'm working on I have noticed that several variables are being
I have seen some very weird for loops when reading other people's code. I
I have seen the following code: [DefaultValue(100)] [Description(Some descriptive field here)] public int MyProperty{...}
Does anyone have some code that will determine if an IP address (IPv4 or
I've been working on some C++ code that a friend has written and I
I have a really strong feeling that this question will either be closed or
I have seen some websites use the following tag: <meta type=title content=Title of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.