Both UserPrincipal
and PrincipalContext
are disposable classes, which would suggest that their disposal is somewhat independent. But what I find in practice is that disposing of the PrincipalContext
apparently does have some affect on the UserPrincipal
. Although I am still able to access properties of the UserPrincipal
after the PrincipalContext
is disposed, if I try to call a method like GetGroups()
on the UserPrincipal
after the PrincipalContext
is disposed, I get an error about trying to access a disposed object.
Keep in mind, though, that just because it has an effect doesn't mean that you shouldn't also separately dispose of the UserPrincipal
object when you're done using it. There's no telling what separate resources UserPrincipal
might need to clean up independently of the PrincipalContext
.
So summary of suggestions to your code:
- Don't try to access
userPrin
afterprincipalCtx
is disposed, or if you do, make sure you are only accessing properties that won't give you errors because I have verified that you can get errors by accessing methods after the context is disposed. - Do also dispose of
userPrin
or include it in ausing
block as well.