Solved

Get host sid in c#

Posted on 2011-03-11
3
967 Views
Last Modified: 2012-05-11
Hi,

I know that each windows machine have unique id (sid)
How to get that sid from host name or ip address.
How to get this in .net c#
0
Comment
Question by:lavitz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Accepted Solution

by:
avilov earned 250 total points
ID: 35112036
System.Security.Principal namespace has two classes - NTAccount and SecurityIdentifier. code looks something like the one below


NTAccount account = new NTAccount("DOMAIN", "MACHINE-NAME$");
SecurityIdentifier sid =
(SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));

Console.WriteLine(sid.Value);
0
 

Author Comment

by:lavitz
ID: 35114889
What if iam not in domain
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 250 total points
ID: 35117958
I'm not sure if there's a way with the "built-in" classes, but using the Windows API LookupAccountName isn't terribly difficult.

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        SecurityIdentifier machineSid = Win32.GetMachineSid();
    }
}

class Win32
{
    const int NO_ERROR = 0;
    const int ERROR_INSUFFICIENT_BUFFER = 122;

    private enum SID_NAME_USE
    {
        SidTypeUser = 1,
        SidTypeGroup,
        SidTypeDomain,
        SidTypeAlias,
        SidTypeWellKnownGroup,
        SidTypeDeletedAccount,
        SidTypeInvalid,
        SidTypeUnknown,
        SidTypeComputer
    }

    [DllImport("advapi32.dll", SetLastError = true)]
    private static extern bool LookupAccountName(string lpSystemName, string lpAccountName, [MarshalAs(UnmanagedType.LPArray)] byte[] Sid,
          ref uint cbSid, StringBuilder ReferencedDomainName, ref uint cchReferencedDomainName, out SID_NAME_USE peUse);

    public static SecurityIdentifier GetMachineSid()
    {
        SID_NAME_USE sidUse;
        byte[] sid = null;
        uint cbSid = 0;
        StringBuilder referencedDomainName = new StringBuilder();
        uint cchReferencedDomainName = 0;
        int error = NO_ERROR;

        if (!LookupAccountName(null, Environment.MachineName, sid, ref cbSid, null, ref cchReferencedDomainName, out sidUse))
        {
            error = Marshal.GetLastWin32Error();
            if (error == ERROR_INSUFFICIENT_BUFFER)
            {
                error = NO_ERROR;
                sid = new byte[cbSid];
                referencedDomainName.EnsureCapacity((int)cchReferencedDomainName);
                if (!LookupAccountName(null, Environment.MachineName, sid, ref cbSid, referencedDomainName, ref cchReferencedDomainName, out sidUse))
                    error = Marshal.GetLastWin32Error();
            }
            else
            {
                // Should never happen, would indicate SID not found
                return null;
            }
        }

        if (error == NO_ERROR)
            return new SecurityIdentifier(sid, 0);
        else
            throw new Win32Exception(error);
    }
}

Open in new window

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question