your code is fine, Windows is a little smarter than we think, and caches user group memberships even when you disconnect the network cable. In fact, if you are in an AD domain, you can also disconnect the cable and write a log because everything is locally cached.
If you really want to check how this works, try disconnecting the cable, check if another group membership is connected and it will be false, and then add your user to another group on the server, and this check will only pass after connecting your computer log in again and log in / out.
source share