C# 添加所有用户SID';C中的Active Directory中的#
无法将所有Sid添加到当前循环中时出现问题。其他一切都如我所料。我只需要在代码中添加帮助,为代码显示的每个用户添加SID。现在将显示SID 新错误消息: 这是我目前的代码:C# 添加所有用户SID';C中的Active Directory中的#,c#,active-directory,C#,Active Directory,无法将所有Sid添加到当前循环中时出现问题。其他一切都如我所料。我只需要在代码中添加帮助,为代码显示的每个用户添加SID。现在将显示SID 新错误消息: 这是我目前的代码: namespace ActiveDirectoryDisplayNamesApp { class Program { static void Main(string[] args) { using (var context = new Princi
namespace ActiveDirectoryDisplayNamesApp
{
class Program
{
static void Main(string[] args)
{
using (var context = new PrincipalContext(ContextType.Domain, "nor-amcoldcorp.local"))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
var sidByte = ObjectToByteArray(de.Properties["objectSId"].Value);
Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
Console.WriteLine("SAM account name : " + de.Properties["samAccountName"].Value);
Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
Console.WriteLine("Object Sid: " + System.Text.Encoding.UTF8.GetString(sidByte)); //Here is the changement
Console.WriteLine();
}
}
}
Console.ReadLine();
}
static public byte[] ObjectToByteArray(Object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
}
编辑:
声明此函数:
private byte[] ObjectToByteArray(Object obj)
{
if(obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
而不是这样做:
byte[] bytes = Encoding.Default.GetBytes(de.Properties["objectSid"].value);
sidByte= Encoding.UTF8.GetString(bytes);
Console.WriteLine("Object Sid: " + sidByte ); //Here is the changement
或者您尝试这种方法(如果第一种方法不起作用),但保留将字节对象转换为字节的第一个函数:
声明函数
static string BytesToStringConverted(byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
using (var streamReader = new StreamReader(stream))
{
return streamReader.ReadToEnd();
}
}
}
而不是这样称呼它:
Console.WriteLine("Object Sid: " +BytesToStringConverted (sidByte)
编辑:
声明此函数:
private byte[] ObjectToByteArray(Object obj)
{
if(obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
而不是这样做:
byte[] bytes = Encoding.Default.GetBytes(de.Properties["objectSid"].value);
sidByte= Encoding.UTF8.GetString(bytes);
Console.WriteLine("Object Sid: " + sidByte ); //Here is the changement
或者您尝试这种方法(如果第一种方法不起作用),但保留将字节对象转换为字节的第一个函数:
声明函数
static string BytesToStringConverted(byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
using (var streamReader = new StreamReader(stream))
{
return streamReader.ReadToEnd();
}
}
}
而不是这样称呼它:
Console.WriteLine("Object Sid: " +BytesToStringConverted (sidByte)
de.Properties[“objectSid”].value
返回一个Byte[]数组,要查看SID,您需要将其解析为一个字符串,以获得所需的功能。一个关于如何做到这一点的好帖子可以找到
以下是将数组转换为可用字符串所需的函数:
public static string ConvertByteToStringSid(Byte[] sidBytes)
{
StringBuilder strSid = new StringBuilder();
strSid.Append("S-");
try
{
// Add SID revision.
strSid.Append(sidBytes[0].ToString());
// Next six bytes are SID authority value.
if (sidBytes[6] != 0 || sidBytes[5] != 0)
{
string strAuth = String.Format
("0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}",
(Int16)sidBytes[1],
(Int16)sidBytes[2],
(Int16)sidBytes[3],
(Int16)sidBytes[4],
(Int16)sidBytes[5],
(Int16)sidBytes[6]);
strSid.Append("-");
strSid.Append(strAuth);
}
else
{
Int64 iVal = (Int32)(sidBytes[1]) +
(Int32)(sidBytes[2] << 8) +
(Int32)(sidBytes[3] << 16) +
(Int32)(sidBytes[4] << 24);
strSid.Append("-");
strSid.Append(iVal.ToString());
// Get sub authority count...
int iSubCount = Convert.ToInt32(sidBytes[7]);
int idxAuth = 0;
for (int i = 0; i < iSubCount; i++)
{
idxAuth = 8 + i * 4;
UInt32 iSubAuth = BitConverter.ToUInt32(sidBytes, idxAuth);
strSid.Append("-");
strSid.Append(iSubAuth.ToString());
}
}
catch (Exception ex)
{
}
return strSid.ToString();
}
de.Properties[“objectSid”].value
返回一个Byte[]数组,要查看SID,您需要将其解析为一个字符串,以获得所需的功能。一个关于如何做到这一点的好帖子可以找到
以下是将数组转换为可用字符串所需的函数:
public static string ConvertByteToStringSid(Byte[] sidBytes)
{
StringBuilder strSid = new StringBuilder();
strSid.Append("S-");
try
{
// Add SID revision.
strSid.Append(sidBytes[0].ToString());
// Next six bytes are SID authority value.
if (sidBytes[6] != 0 || sidBytes[5] != 0)
{
string strAuth = String.Format
("0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}",
(Int16)sidBytes[1],
(Int16)sidBytes[2],
(Int16)sidBytes[3],
(Int16)sidBytes[4],
(Int16)sidBytes[5],
(Int16)sidBytes[6]);
strSid.Append("-");
strSid.Append(strAuth);
}
else
{
Int64 iVal = (Int32)(sidBytes[1]) +
(Int32)(sidBytes[2] << 8) +
(Int32)(sidBytes[3] << 16) +
(Int32)(sidBytes[4] << 24);
strSid.Append("-");
strSid.Append(iVal.ToString());
// Get sub authority count...
int iSubCount = Convert.ToInt32(sidBytes[7]);
int idxAuth = 0;
for (int i = 0; i < iSubCount; i++)
{
idxAuth = 8 + i * 4;
UInt32 iSubAuth = BitConverter.ToUInt32(sidBytes, idxAuth);
strSid.Append("-");
strSid.Append(iSubAuth.ToString());
}
}
catch (Exception ex)
{
}
return strSid.ToString();
}
已经有一个专用的Sid类可用于解码Sid数据
var sidBytes = (byte[])de.Properties["objectSId"].Value;
var sid = new SecurityIdentifier(sidBytes ,0);
string strSid = sid.Value;//Something like S-1-5-21..
.已经有一个专用的Sid类可用于解码Sid数据
var sidBytes = (byte[])de.Properties["objectSId"].Value;
var sid = new SecurityIdentifier(sidBytes ,0);
string strSid = sid.Value;//Something like S-1-5-21..
string strAuth=string.Format(“0x{0:x2}{1:x2}{2:x2}{3:x2}{4:x2}{5:x2}”,
SecurityIdentifier
仅在Windows上可用。
- 十六进制转换(错误的
,右:“0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}
)“0x{0:x2}{1:x2}{2:x2}{3:x2}{4:x2}{5:x2}
- 子权限计数在第二个字节,而不是第八个字节(错误
,右侧:Convert.ToInt32(sidBytes[7])
)Convert.ToInt32(sidBytes[1])
我遵循了最初的实现,并与类<代码>安全标识符< /代码>的结果进行比较,也考虑了注释:
SID解码是错误的Pin码 你好, 我认为您的SID解码是错误的。子权限的数量是SID字节数组中的第2个字节,而不是第8个字节,并且主权限的字节存储顺序与您正在读取的顺序不同 我想分享一个更好的版本,请访问: :公共静态字符串convertbyteTostringId(字节[]sidBytes)
{
if(sidBytes==null | | sidBytes.Length<8||
sidBytes.Length>68)//最多15个子权限
返回字符串。空;
var span=新的只读span(sidBytes);
var strSid=新的StringBuilder(“S-”);
//添加SID修订。
strSid.Append(span[0]);
//获取子权限计数。。。
var subAuthoritiesLength=Convert.ToInt32(span[1]);
if(sidBytes.Length!=8+子权限长度*4)
返回字符串。空;
长标识权限=
((长)跨度[2])
对于十六进制形式,它应该是:string strAuth=string.Format(“0x{0:x2}{1:x2}{2:x2}{3:x2}{4:x2}{5:x2}”,
对于.NET 5,SecurityIdentifier
仅在Windows上可用。
从原始版本中发现了一些错误
公共静态字符串ConvertByteToStringSid(字节[]sidBytes)
- 十六进制转换(错误的
“0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}
,右:“0x{0:x2}{1:x2}{2:x2}{3:x2}{4:x2}{5:x2}
)
- 子权限计数在第二个字节,而不是第八个字节(错误
Convert.ToInt32(sidBytes[7])
,右侧:Convert.ToInt32(sidBytes[1])
)
我遵循了最初的实现,并与类<代码>安全标识符< /代码>的结果进行比较,也考虑了注释:
SID解码是错误的Pin码
你好,
我认为您的SID解码是错误的。子权限的数量是SID字节数组中的第2个字节,而不是第8个字节,并且主权限的字节存储顺序与您正在读取的顺序不同
我想分享一个更好的版本,请访问:
:
公共静态字符串convertbyteTostringId(字节[]sidBytes)
{
if(sidBytes==null | | sidBytes.Length<8||
sidBytes.Length>68)//最多15个子权限
返回字符串。空;
var span=新的只读span(sidBytes);
var strSid=新的StringBuilder(“S-”);
//添加SID修订。
strSid.Append(span[0]);
//获取子权限计数。。。
var subAuthoritiesLength=Convert.ToInt32(span[1]);
if(sidBytes.Length!=8+子权限长度*4)
返回字符串。空;
长标识权限=
((长)跨度[2])你好@napi15,我试过了,但没有成功,我得到了这个系统。字节[]。未显示每个用户的实际Sid。谢谢。我将尝试并让您知道这是否有效,谢谢。Hello@napi15,在您添加的两个解决方案中。我收到重载方法错误。您缺少什么?我将使用image@AndresBryan29试试我编辑的内容,让我知道Hello@napi15,我试过了我得到了这个系统。字节[]。未显示每个用户的实际Sid。谢谢。我将尝试并让您知道这是否有效,谢谢。Hello@napi15,在您添加的两个解决方案中。我收到重载方法错误。您缺少什么?我将使用image@AndresBryan29试试我编辑的内容,让我知道现在我得到了一个weiSID的rd消息,I ad