שתף באמצעות


Getting the SID of the current user

Question

Friday, January 24, 2014 6:46 AM

In VB.NET, I would like to get the SID of the user currently logged in. How can I do this?

All replies (5)

Friday, January 24, 2014 7:11 AM ✅Answered | 1 vote

you mean

        Dim ff As System.Security.Principal.WindowsIdentity
        ff = System.Security.Principal.WindowsIdentity.GetCurrent

'dim AccSid as string = ff.User.AccountDomainSid.tostring

Friday, January 24, 2014 7:38 AM

you mean

        Dim ff As System.Security.Principal.WindowsIdentity
        ff = System.Security.Principal.WindowsIdentity.GetCurrent

'dim AccSid as string = ff.User.AccountDomainSid.tostring

Alright, this gets me close. It's missing the last 4 digits, which define the exact user.


Friday, January 24, 2014 8:02 AM | 1 vote

no really you just used the line I commented out :-)

use

ff.User.ToString


Friday, January 24, 2014 8:13 AM

no really you just used the line I commented out :-)

use

ff.User.ToString

Haha, I was a bit confused why you commented it out. Perfect! So then here's the code that confirmed the correct SID.

        Dim ff As System.Security.Principal.WindowsIdentity
        ff = System.Security.Principal.WindowsIdentity.GetCurrent
        Dim ee = ff.User.ToString()
        MsgBox(ee)

Friday, January 24, 2014 8:51 AM

no really you just used the line I commented out :-)

use

ff.User.ToString

Edit: Solved it. I discovered Applications Events. So I did a start up even before the form loads.

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Public Sub MyApplication_Startup(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            Dim programPath = (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) & "\Tweaky"
            Dim ff = System.Security.Principal.WindowsIdentity.GetCurrent
            Dim sid = ff.User.ToString
            MsgBox(programPath)
            IO.File.WriteAllText(programPath & "\sid.txt", sid)

            Dim process As New Process()
            process.StartInfo.FileName = "cmd.exe "
            process.StartInfo.Verb = "runas"
            process.StartInfo.UseShellExecute = True
            process.Start()
        End Sub
    End Class


End Namespace

I need to pass over the SID to the main form. The only way I could figure out how to do it was write a text file with the SID. Then I have the main form start as Administrator. When the main form loads, have it read and store it in a variable. From there, I can use that SID to correctly edit the right registry hive. Originally I edited app.manifest (requestedExecutionLevel level="asAdministrator"). Problem is that the ENTIRE program runs as Administrator. I needed to run a little bit of code before the rest could run as Administrator.

____________________________________________________________________________________________________
I've run into a problem. I want to run my entire application as Administrator. This will make things much easier. However, the code you gave me gives the SID of who is running the application.

I am currently logged in as Tyler. However, I have to elevate the program as Administrator. I want the SID of Tyler, the one who invoked the program to run as Administrator.