Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - How do I get the machine sid in C#

Reply
 
Old 04-01-2009   #1 (permalink)
Scott


 
 

How do I get the machine sid in C#

In cpp is this straight forward. You get the domain and machine name,
LookupAccountName and convert the Sid to a string and you are done.

I can't find a simple way to do the equivalent in c#. Does someone have an
example in c# of getting the machine sid. Since this is only 10 lines in cpp
it should be a one liner (I hope) in c#?
--
Scott Norberg

My System SpecsSystem Spec
Old 04-01-2009   #2 (permalink)
Jie Wang [MSFT]


 
 

RE: How do I get the machine sid in C#

Hi Scott,

To get the machine SID in C#, you need NTAccount class and
SecurityIdentifier class. They live in System.Security.Principal namespace.

The code looks like this:

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

// we're done, show the results:
Console.WriteLine(account.Value);
Console.WriteLine(sid.Value);

If you have any further questions regarding this issue, please feel free to
post here.

Regards,

Jie Wang (jiewan@xxxxxx, remove 'online.')

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

My System SpecsSystem Spec
Old 04-01-2009   #3 (permalink)
Scott


 
 

RE: How do I get the machine sid in C#

Easy enough, but!

This does not return the same sid as the cpp code.

Sid = (PSID) malloc(cbSid);
pszDomainName = (LPSTR) malloc(cbDomainName);

bRC = LookupAccountName( NULL, m_szComputerName, Sid, &cbSid,
pszDomainName , &cbDomainName, &eUse );
if( bRC )
{
strcpy_s( m_szDomainName, sizeof(m_szDomainName), pszDomainName );
bRC = ConvertSidToStringSid( Sid, &pszSid );
if( bRC )
{
strcpy_s( m_szMachineSID, sizeof(m_szMachineSID), pszSid );
if (pszSid) LocalFree( pszSid );
}
}
This code returns a DomainSid when executed, with the domainname set to the
machine name. Yes the workstation is part of a domain.

Trying every possible combination of DomainNames with the known computer
name does not return the same sid from the c# code.

I really need these to be the same literal value.

Guess I am missing something here. I thought the machine was always given a
sid when it was installed. Then it will be given another sid when it joins
the domain, but does not destroy the original installation machine sid. The
computer sid in the domain then can change if the computer leaves the domain
and attachs to another domain. But the original sid from the install will
remain the same.

Correct me if I am wrong here.

I would like to get the original sid of the machine. This value should not
change (unless someone sepcifically resets the sid), regardless of the domain
the computer belongs to.

--
Scott Norberg


""Jie Wang [MSFT]"" wrote:
Quote:

> Hi Scott,
>
> To get the machine SID in C#, you need NTAccount class and
> SecurityIdentifier class. They live in System.Security.Principal namespace.
>
> The code looks like this:
>
> NTAccount account = new NTAccount("DOMAIN", "MACHINE-NAME$");
> SecurityIdentifier sid =
> (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
>
> // we're done, show the results:
> Console.WriteLine(account.Value);
> Console.WriteLine(sid.Value);
>
> If you have any further questions regarding this issue, please feel free to
> post here.
>
> Regards,
>
> Jie Wang (jiewan@xxxxxx, remove 'online.')
>
> Microsoft Online Community Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@xxxxxx.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subs...#notifications.
>
> Note: MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 2 business days is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions. Issues of this
> nature are best handled working with a dedicated Microsoft Support Engineer
> by contacting Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
My System SpecsSystem Spec
Old 04-01-2009   #4 (permalink)
Scott


 
 

RE: How do I get the machine sid in C#

More information.

The SID that is being returned from the C# example is the same sid that is
in the directory for the computer. Which is what I would have expected.

Changing my cpp code to include the domain name (domain\computer$) then I
get the same sid.

So what happens when a computer is not part of a domain? In my world we have
a mix of domains and standalone computers.

I need the cpp and C# code to return the same sid in both cases.
--
Scott Norberg


""Jie Wang [MSFT]"" wrote:
Quote:

> Hi Scott,
>
> To get the machine SID in C#, you need NTAccount class and
> SecurityIdentifier class. They live in System.Security.Principal namespace.
>
> The code looks like this:
>
> NTAccount account = new NTAccount("DOMAIN", "MACHINE-NAME$");
> SecurityIdentifier sid =
> (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
>
> // we're done, show the results:
> Console.WriteLine(account.Value);
> Console.WriteLine(sid.Value);
>
> If you have any further questions regarding this issue, please feel free to
> post here.
>
> Regards,
>
> Jie Wang (jiewan@xxxxxx, remove 'online.')
>
> Microsoft Online Community Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@xxxxxx.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subs...#notifications.
>
> Note: MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 2 business days is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions. Issues of this
> nature are best handled working with a dedicated Microsoft Support Engineer
> by contacting Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
My System SpecsSystem Spec
Old 04-01-2009   #5 (permalink)
Scott


 
 

RE: How do I get the machine sid in C#

Yet some more information;

I created an xp workstation outside of the domain. The CPP version returns
the following

DomainName:
ComputerName: XPTEST
Account : XPTEST
FQDN : XPTEST\XPTEST - SID_NAME_USE: 3
MachineSID: S-1-5-21-1202660629-1637723038-839522115

The C#
try
{
sMachine = String.Format( "{0}\\{0}", Environment.MachineName );
Console.WriteLine( sMachine );
nta = new NTAccount( sMachine );

sid = (SecurityIdentifier) nta.Translate( typeof(SecurityIdentifier));
Console.WriteLine( sid.Value );
acct = (NTAccount)sid.Translate(typeof(NTAccount));
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
}

Throws an exception on the translate no matter what I put in for a computer
name.

--
Scott Norberg


""Jie Wang [MSFT]"" wrote:
Quote:

> Hi Scott,
>
> To get the machine SID in C#, you need NTAccount class and
> SecurityIdentifier class. They live in System.Security.Principal namespace.
>
> The code looks like this:
>
> NTAccount account = new NTAccount("DOMAIN", "MACHINE-NAME$");
> SecurityIdentifier sid =
> (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
>
> // we're done, show the results:
> Console.WriteLine(account.Value);
> Console.WriteLine(sid.Value);
>
> If you have any further questions regarding this issue, please feel free to
> post here.
>
> Regards,
>
> Jie Wang (jiewan@xxxxxx, remove 'online.')
>
> Microsoft Online Community Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@xxxxxx.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subs...#notifications.
>
> Note: MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 2 business days is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions. Issues of this
> nature are best handled working with a dedicated Microsoft Support Engineer
> by contacting Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
My System SpecsSystem Spec
Old 04-03-2009   #6 (permalink)
Jie Wang [MSFT]


 
 

RE: How do I get the machine sid in C#

Hi Scott,

I totally understand the pain when you find something cannot be easily done
in .NET as expected.

In earlier time, when Windows XP just shipped, I found it was not that easy
to make my .NET 1.0 WinForm application adopt the XP Visual Style. One need
to make a manifest file, set the FlatStyle property of every control to
"System", and yet some controls were not drawn correctly. But later in .NET
1.1, we no longer needed the manifest file because there added a
EnableVisualStyles method. Even better, in .NET 2.0, we don't even have to
set the FlatStyle to System on each control.

This is just one of the many samples showing that Microsoft is making an on
going effort to make .NET Framework better and more friendly to developers.

You may also want your voice heard directly by the guys who make the
framework, then go to http://connect.microsoft.com/ and post your wishes.

And here is a handy table for *Unmanaged Features and Their Managed
Equivalents*, you may already know that, but I'd like to post it anyway so
others who don't know may benefit from it:
http://msdn.microsoft.com/en-us/library/ms717325.aspx

Best regards,

Jie Wang (jiewan@xxxxxx, remove 'online.')

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Vista Machine unable to print to Printer attached to 98 machine thru parallel port Network & Sharing
Booting my XP machine from a mapped drive on my vista machine Vista networking & sharing
easy way to transfer applications from XP machine to Vista machine Vista General
Importing Outlook 2003 Contacts and Mail from an XP machine to a new Vista machine Vista mail
Can't Back Up From Vista Machine to XP Machine Using Vista's Backu Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46