Hi Scott,
Sorry for the misunderstanding, when you say "You get the domain and
machine name, LookupAccountName and convert the Sid to a string and you are
done" I thought you wanted to get the SID of the computer on the domain.
Now let me fix this up.
The NTAccount is not working in this scenario - the SID we want is not
mapped to an NTAccount, so we just can't create one.
We're going to call LookupAccountName just like what you've done in C++.
Here is the code sample for doing this:
internal static class Helper
{
internal enum SID_NAME_USE
{
SidTypeUser = 1,
SidTypeGroup,
SidTypeDomain,
SidTypeAlias,
SidTypeWellKnownGroup,
SidTypeDeletedAccount,
SidTypeInvalid,
SidTypeUnknown,
SidTypeComputer
}
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern bool LookupAccountName(
string machineName,
string accountName,
byte[] sid,
ref int sidLen,
StringBuilder domainName,
ref int domainNameLen,
out SID_NAME_USE peUse);
public static SecurityIdentifier LookupAccountName(
string systemName,
string accountName,
out string refDomain,
out SID_NAME_USE use)
{
int sidLen = 0x400;
int domainLen = 0x400;
byte[] sid = new byte[sidLen];
StringBuilder domain = new StringBuilder(domainLen);
if (LookupAccountName(systemName, accountName, sid, ref sidLen,
domain, ref domainLen, out use))
{
refDomain = domain.ToString();
return new SecurityIdentifier(sid, 0);
}
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
Here I created a helper class which can be used like this to get the
machine SID:
string machineName = Environment.MachineName;
string refDomain;
Helper.SID_NAME_USE use;
Console.WriteLine("{0}: {1}", machineName, Helper.LookupAccountName(null,
machineName, out refDomain, out use));
Please let me know if this helps.
Thanks,
Jie Wang (
[email protected], 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:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#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/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.