?
Solved

Get host sid in c#

Posted on 2011-03-11
3
Medium Priority
?
975 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 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

752 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