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
Solved

Get host sid in c#

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Directions API to Map URL -C#? 3 37
SqlDataBase 7 53
how to check to see if datatable has headers or not 2 34
How to read text with RegEx... 2 25
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…

856 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