Solved

Get host sid in c#

Posted on 2011-03-11
3
941 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
Comment Utility
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
Comment Utility
What if iam not in domain
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 250 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now