c++winapisessionid

Are session IDs obtained by WTSEnumerateSessions API unique?


Say, if at one point in time I call WTSEnumerateSessions from a local service application and obtain the list of current user session IDs. So if at some later time I need to know if some particular user session is still in the same state, can I rely on WTS_SESSION_INFO::SessionId to be unique? And if not, how to do this?


Solution

  • Windows gives a SID to every logon session. MSDN has sample code to look it up and it is guaranteed to be unique and constant for a given logon session. If the user logs out and logs back in again, he will be attributed a new Logon SID.

    If the same user is logged locally and logged on remotely at the same, there will be a different Logon SID for each logon session. This image shows what I mean :

    Alice has two logon session, with distinct Logon SID.

    (Disclaimer - I stole it from a blog post of mine)

    You can get the Logon SID with this command. It can be useful to play what-if scenarios without writing any code.

    c:\>whoami /logonid
    S-1-5-5-0-329693570
    

    Tested on Windows Server 2008. YMMV on other versions.

    Edit: Fixed Grammatical Errors