Hey everyone,

I have been told there is a way to get a users login name and their SID in C#, does anybody have code or know a very detailed and easy to understand site?

What I am trying to do is this, when a user logs in my application starts, I then need to update a table in PostgreSQL to change the amount of logins they have. But to do that (as there will be many different users) I need the username that logged in, and the SID to search and update.

Can anybody help me out please?

Cheers,

:cool:

thier nt login?

there is a few ways in vb 2005 (i dont know c#) that i know for the username.

just use:

somevariable = Environment.UserName

to get it to have the full name e.g LAPTOP\JBENNET then add these includes:

Imports System.Security 
Imports System.Security.Principal.WindowsIdentity

then usie it in you app like this

MsgBox(GetCurrent.Name)

Cheers mate I will give that a try, is there a way to get the SID?

not sure, i will have a play and see if i can find out also btw you might get different results than me as im in an xp home (workgroup) environment not a domain environment. Also i only have vb.net not c# so you will need to change the code a bit

For the SID

Dim objNetwork : objNetwork = CreateObject("WScript.Network")
 
Dim wmiQuery : wmiQuery = "Select * From Win32_UserAccount Where Domain='" & objNetwork.UserDomain & "' And Name='" & objNetwork.UserName & "'"
 
Dim objWMIService : objWMIService = GetObject("winmgmts:\\.\root\cimv2")
 
Dim colItems : colItems = objWMIService.ExecQuery(wmiQuery)
 
Dim objItem
 
       For Each objItem In colItems
           MsgBox("Your SID is : " & objItem.SID)
       Next

this works in vb2005 express but i had to play around with references a bit cant remember which ones though so im afraid if it doesnt work straight off then its trial and error

the second Dim chunk should all be on one line BTW.

Member Avatar for iamthwee

What's SID?

SID is a unique user identifier (like a primary key in a database) . This is so windows still knows who you are even if you change your username. You see SIDs a lot in the registry, they look like S-12-843Y873-3535 etc...

fire up regedit and take a look at HKEY_USERS on a machine with a couple of users and youll see what i mean. (be carefful not to change anything!!!)

string username = String.Format(@"{0}\{1}", Environment.UserDomainName, Environment.UserName);
System.Security.Principal.WindowsIdentity id = new System.Security.Principal.WindowsIdentity(username);
string sid = id.User.AccountDomainSid.ToString();
string username = String.Format(@"{0}\{1}", Environment.UserDomainName, Environment.UserName);
System.Security.Principal.WindowsIdentity id = new System.Security.Principal.WindowsIdentity(username);
string sid = id.User.AccountDomainSid.ToString();

Actually, using the WindowsIdentity cctor might result in an expection. Instead, use:

System.Security.Principal.WindowsIdentity id = System.Security.Principal.WindowsIdentity.GetCurrent();
            string sid = id.User.AccountDomainSid.ToString();

Hey guys,

Sorry for not getting back to you all sooner, that all worked great. Having a few problems taking the sid, getting the DomainID from Postgres using the SID, and then storing the DID myself. But that seems to be down to PPP (piss poor planning) by managers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.