Get Unique SID for machine / user

We have gotten a lot of questions on how to get a unique identifier for a machine for licensing and other purposes.

We have gotten a lot of questions on how to get a unique identifier for a machine for licensing and other purposes.

It seems that getting the CPU identifier may only return the Class of CPU rather than the unique CPU "Serial number". So here is some code that's easy to use that returns the unique SID (e.g., "S-1-5-21-1692944411-1583482546-720635935-8985") for a user on the machine:


using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Management;
using System.Diagnostics;
namespace PAB.Utils
{
/// <summary>
/// Gets unique SID for give user (e.g. "S-1-5-21-1692944411-1583482546-720635935-8985")
/// </summary>
public class SIDClass
{
private ManagementObjectSearcher query;
private ManagementObjectCollection queryCollection;

public string ShowUserSID(string username)
{
// local scope
ConnectionOptions co = new ConnectionOptions();
co.Username = username;
ManagementScope msc = new ManagementScope ("\\root\\cimv2",co);
string queryString =
"SELECT * FROM Win32_UserAccount where name='" +co.Username +"'" ;
SelectQuery q = new SelectQuery (queryString);
query = new ManagementObjectSearcher(msc, q);
queryCollection = query.Get();
string res=String.Empty;
foreach( ManagementObject mo in queryCollection )
{
// there should be only one here!
res+= mo["SID"].ToString();
}
return res;
}
}
}



Submission Date:  9/23/2005 3:21:12 PM
Submitted By:  Peter Bromberg
My Home Page:  http://www.eggheadcafe.com

By Peter Bromberg   Popularity  (467 Views)