internal string GetSddlForm()
        {
            string sidString = Value;

            WellKnownAccount acct = WellKnownAccount.LookupBySid(sidString);

            if (acct == null || acct.SddlForm == null)
            {
                return(sidString);
            }

            return(acct.SddlForm);
        }
        private static byte[] ParseSddlForm(string sddlForm)
        {
            string sid = sddlForm;

            // If only 2 characters long, can't be a full SID string - so assume
            // it's an attempted alias.  Do that conversion first.
            if (sddlForm.Length == 2)
            {
                WellKnownAccount acct = WellKnownAccount.LookupBySddlForm(sddlForm);
                if (acct == null)
                {
                    throw new ArgumentException(
                              "Invalid SDDL string - unrecognized account: " + sddlForm,
                              "sddlForm");
                }
                if (!acct.IsAbsolute)
                {
                    throw new NotImplementedException(
                              "Mono unable to convert account to SID: "
                              + (acct.Name != null ? acct.Name : sddlForm));
                }

                sid = acct.Sid;
            }

            string[] elements          = sid.ToUpper(CultureInfo.InvariantCulture).Split('-');
            int      numSubAuthorities = elements.Length - 3;

            if (elements.Length < 3 || elements[0] != "S" || numSubAuthorities > 15)
            {
                throw new ArgumentException("Value was invalid.");
            }

            if (elements[1] != "1")
            {
                throw new ArgumentException("Only SIDs with revision 1 are supported");
            }

            byte[] buffer = new byte[8 + (numSubAuthorities * 4)];
            buffer[0] = 1;
            buffer[1] = (byte)numSubAuthorities;

            ulong authority;

            if (!TryParseAuthority(elements[2], out authority))
                throw new ArgumentException("Value was invalid."); }
        public bool IsWellKnown(WellKnownSidType type)
        {
            WellKnownAccount acct = WellKnownAccount.LookupByType(type);

            if (acct == null)
            {
                return(false);
            }

            string sid = Value;

            if (acct.IsAbsolute)
            {
                return(sid == acct.Sid);
            }

            return(sid.StartsWith("S-1-5-21", StringComparison.OrdinalIgnoreCase) &&
                   sid.EndsWith("-" + acct.Rid, StringComparison.OrdinalIgnoreCase));
        }
        public override IdentityReference Translate(Type targetType)
        {
            if (targetType == typeof(NTAccount))
            {
                return(this);                // ? copy
            }
            if (targetType == typeof(SecurityIdentifier))
            {
                WellKnownAccount acct = WellKnownAccount.LookupByName(this.Value);
                if (acct == null || acct.Sid == null)
                {
                    throw new IdentityNotMappedException("Cannot map account name: " + this.Value);
                }

                return(new SecurityIdentifier(acct.Sid));
            }

            throw new ArgumentException("Unknown type", "targetType");
        }
        public override IdentityReference Translate(Type targetType)
        {
            if (targetType == typeof(SecurityIdentifier))
            {
                return(this);
            }

            if (targetType == typeof(NTAccount))
            {
                WellKnownAccount acct = WellKnownAccount.LookupBySid(this.Value);
                if (acct == null || acct.Name == null)
                {
                    throw new IdentityNotMappedException("Unable to map SID: " + this.Value);
                }

                return(new NTAccount(acct.Name));
            }

            throw new ArgumentException("Unknown type.", "targetType");
        }
        public SecurityIdentifier(WellKnownSidType sidType,
                                  SecurityIdentifier domainSid)
        {
            WellKnownAccount acct = WellKnownAccount.LookupByType(sidType);

            if (acct == null)
            {
                throw new ArgumentException("Unable to convert SID type: " + sidType);
            }

            if (acct.IsAbsolute)
            {
                buffer = ParseSddlForm(acct.Sid);
            }
            else
            {
                if (domainSid == null)
                {
                    throw new ArgumentNullException("domainSid");
                }

                buffer = ParseSddlForm(domainSid.Value + "-" + acct.Rid);
            }
        }