为什么WellKnownSidType有时无法转换为sid / account?
问题:我正在查找每个WellKnownSidType枚举成员的帐户名称和SID,如下所示。
为什么有时会失败?
为什么有时会将WellKnownSidType转换为sid失败?
据我了解,只有从sid到accountname的转换有时会失败,甚至只有当帐户不是本地而不是域时才会失败。
例如,在将枚举LogonIdsSid转换为SID时,我得到:
Bekannte SIDs des TypsLogonIdsSidkönnennichterstellt werden。
(无法创建LogonIdsSid类型的已知SID。)
或者在查找NTAuthoritySid的帐户名时
我得到:Manche oderallestartitätsverweisekontennichtübersetztwerden。
(部分或全部的idendity-references无法翻译。)
Sub Main() Enumerations.SidInfo(Of System.Security.Principal.WellKnownSidType)() End Sub Public Class Enumerations Public Shared Sub SidInfo(Of T)() Dim enumType As Type = GetType(T) For Each ThisEnumValue As T In System.Enum.GetValues(GetType(T)) Try Console.WriteLine("Enum: System.Security.Principal.WellKnownSidType." + System.Enum.Format(GetType(T), ThisEnumValue, "G")) Dim enumItem1 As System.Reflection.FieldInfo = enumType.GetField(System.Enum.Format(GetType(T), ThisEnumValue, "G")) Dim enumValue1 As T = CType(enumItem1.GetValue(enumType), T) Dim sid As System.Security.Principal.SecurityIdentifier = New System.Security.Principal.SecurityIdentifier(CType(CType(enumValue1, Object), System.Security.Principal.WellKnownSidType), Nothing) Console.WriteLine("SID: " + sid.ToString()) Dim ntAccount As Security.Principal.NTAccount = CType(sid.Translate(GetType(Security.Principal.NTAccount)), Security.Principal.NTAccount) Console.WriteLine("Account: " + ntAccount.ToString()) Console.WriteLine(vbCrLf) Catch ex As Exception Console.WriteLine("Exception on: " + System.Enum.Format(GetType(T), ThisEnumValue, "G")) Console.WriteLine(vbCrLf) End Try Next End Sub End Class
C#(自动翻译):
//Dim sid As System.Security.Principal.SecurityIdentifier = New System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, Nothing) //Dim rule As System.Security.AccessControl.MutexAccessRule = New System.Security.AccessControl.MutexAccessRule(sid, System.Security.AccessControl.MutexRights.FullControl, System.Security.AccessControl.AccessControlType.Allow) public static void GetSID() { Type enumType = typeof(T); foreach (T ThisEnumValue in System.Enum.GetValues(typeof(T))) { try { Console.WriteLine("Enum: System.Security.Principal.WellKnownSidType." + System.Enum.Format(typeof(T), ThisEnumValue, "G")); System.Reflection.FieldInfo enumItem1 = enumType.GetField(System.Enum.Format(typeof(T), ThisEnumValue, "G")); T enumValue1 = (T)enumItem1.GetValue(enumType); System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier((System.Security.Principal.WellKnownSidType)(object)enumValue1, null); Console.WriteLine("SID: " + sid.ToString()); System.Security.Principal.NTAccount ntAccount = (Security.Principal.NTAccount)sid.Translate(typeof(Security.Principal.NTAccount)); Console.WriteLine("Account: " + ntAccount.ToString()); Console.WriteLine(Constants.vbCrLf); } catch (Exception ex) { Console.WriteLine("Exception on: " + System.Enum.Format(typeof(T), ThisEnumValue, "G") + Constants.vbCrLf + ex.Message); Console.WriteLine(Constants.vbCrLf); } } }
这种方法实际上会产生三个错误。
第一个是IdentityNotMappedException
,这意味着您尝试制作的帐户实际上并不存在于计算机上。 WellKnownSidType
枚举代表所有众所周知的SID,而不仅仅是特定于给定机器的SID。 可能有一种机制可以用来查看它,但我不知道它在手边。 您可能需要进行P / Invoke并使用CreateWellKnownSid
或只捕获exception。
第二个是ArgumentException
,如果您尝试使用LogonIdsSid
将会发生。 如果您查看SecurityIdentifier
的构造函数的文档,您将看到可以使用LogonIdsSid
。
第三个错误是ArgumentNullException
,如果您尝试创建以下众所周知的SID之一而不指定域SID,则会发生此错误。 这也在文档中 。
- AccountAdministratorSid
- AccountGuestSid
- AccountKrbtgtSid
- AccountDomainAdminsSid
- AccountDomainUsersSid
- AccountDomainGuestsSid
- AccountComputersSid
- AccountControllersSid
- AccountCertAdminsSid
- AccountSchemaAdminsSid
- AccountEnterpriseAdminsSid
- AccountPolicyAdminsSid
- AccountRasAndIasServersSid