-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSID.cs
More file actions
49 lines (47 loc) · 1.39 KB
/
SID.cs
File metadata and controls
49 lines (47 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Management;
using System.Diagnostics;
namespace CCMClient
{
/// <summary>
/// Gets unique SID for give user (e.g. "S-1-5-21-1692944411-1583482546-720635935-8985")
/// </summary>
public class SID
{
private static ManagementObjectSearcher query;
private static ManagementObjectCollection queryCollection;
public static string ShowUserSID(string username)
{
// local scope
string[] unames = username.Split("\\".ToCharArray(),2);
string d = "";
string n = "";
d = unames[0];
if (unames.Length < 2)
{
n = unames[0];
d = "US_IBS";
}
else
n = unames[1];
ConnectionOptions co = new ConnectionOptions();
//co.Username = username;
ManagementScope msc = new ManagementScope ("\\root\\cimv2",co);
string queryString = "SELECT * FROM Win32_UserAccount where LocalAccount = false AND SIDType = 1 AND Domain = '" + d+ "' AND Name = '" + n + "'";
//System.Windows.Forms.MessageBox.Show(queryString);
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();
//res+= mo["Name"]+"\n";
}
return res;
}
}
}