Solved

Get host sid in c#

Posted on 2011-03-11
3
949 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
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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