为什么WellKnownSidType有时无法转换为sid / account?

时间:2011-02-18 17:25:16

标签: c# .net asp.net vb.net permissions

问题:我正在查找每个WellKnownSidType枚举成员的帐户名称和SID,如下所示。

为什么有时会失败?
为什么有时会将WellKnownSidType转换为sid? 据我了解,只有从sid到accountname的转换有时会失败,甚至只有当帐户不是本地而不是域时才会失败。



例如,在将枚举LogonIdsSid转换为SID时,我得到:
Bekannte SIDs des TypsLogonIdsSidkönnenichterstellt werden。
(无法创建LogonIdsSid类型的已知SID。)

或者在查找NTAuthoritySid的帐户名时 我得到:Manche oderallestatitätsverweisekontennichtübersetztwerden。
(部分或全部的标识 - 引用无法翻译。)

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<T>()
{
    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);
        }
    }

}

1 个答案:

答案 0 :(得分:5)

这种方法实际上会产生三个错误。

第一个是IdentityNotMappedException,这意味着您尝试制作的帐户实际上并不存在于计算机上。 WellKnownSidType枚举代表所有众所周知的SID,而不仅仅是特定于给定机器的SID。可能有一种机制可以用来查看它,但我不知道它在手边。您可能需要进行P / Invoke并使用CreateWellKnownSid或者只是捕获异常。

如果您尝试使用ArgumentException,则第二个是LogonIdsSid。如果您检查SecurityIdentifier LogonIdsSid,则会看到您可以使用ArgumentNullException

如果您尝试在未指定域SID的情况下创建以下众所周知的SID之一,则会出现第三个错误{{1}}。这也是documentation for the constructor

  • AccountAdministratorSid
  • AccountGuestSid
  • AccountKrbtgtSid
  • AccountDomainAdminsSid
  • AccountDomainUsersSid
  • AccountDomainGuestsSid
  • AccountComputersSid
  • AccountControllersSid
  • AccountCertAdminsSid
  • AccountSchemaAdminsSid
  • AccountEnterpriseAdminsSid
  • AccountPolicyAdminsSid
  • AccountRasAndIasServersSid