public static SerializedClientSecurityContext CreateFromOwaIdentity(OwaIdentity owaIdentity)
        {
            SerializedClientSecurityContext serializedClientSecurityContext = new SerializedClientSecurityContext();

            owaIdentity.ClientSecurityContext.SetSecurityAccessToken(serializedClientSecurityContext);
            serializedClientSecurityContext.LogonName          = owaIdentity.GetLogonName();
            serializedClientSecurityContext.AuthenticationType = owaIdentity.AuthenticationType;
            return(serializedClientSecurityContext);
        }
        internal static SerializedClientSecurityContext Deserialize(Stream input)
        {
            XmlTextReader xmlTextReader = null;
            SerializedClientSecurityContext result;

            try
            {
                xmlTextReader = new XmlTextReader(input);
                xmlTextReader.WhitespaceHandling = WhitespaceHandling.All;
                SerializedClientSecurityContext serializedClientSecurityContext = SerializedClientSecurityContext.Deserialize(xmlTextReader);
                result = serializedClientSecurityContext;
            }
            finally
            {
                if (xmlTextReader != null)
                {
                    xmlTextReader.Close();
                }
            }
            return(result);
        }
 public void Serialize(XmlTextWriter writer)
 {
     writer.WriteStartElement(SerializedClientSecurityContext.rootElementName);
     writer.WriteAttributeString(SerializedClientSecurityContext.authenticationTypeAttributeName, this.authenticationType);
     writer.WriteAttributeString(SerializedClientSecurityContext.logonNameAttributeName, this.logonName);
     SerializedClientSecurityContext.WriteSid(writer, this.UserSid, 0U, SerializedClientSecurityContext.SidType.User);
     if (this.GroupSids != null)
     {
         for (int i = 0; i < this.GroupSids.Length; i++)
         {
             SerializedClientSecurityContext.WriteSid(writer, this.GroupSids[i].SecurityIdentifier, this.GroupSids[i].Attributes, SerializedClientSecurityContext.SidType.Group);
         }
     }
     if (this.RestrictedGroupSids != null)
     {
         for (int j = 0; j < this.RestrictedGroupSids.Length; j++)
         {
             SerializedClientSecurityContext.WriteSid(writer, this.RestrictedGroupSids[j].SecurityIdentifier, this.RestrictedGroupSids[j].Attributes, SerializedClientSecurityContext.SidType.RestrictedGroup);
         }
     }
     writer.WriteEndElement();
 }
        internal static SerializedClientSecurityContext Deserialize(XmlTextReader reader)
        {
            SerializedClientSecurityContext serializedClientSecurityContext = new SerializedClientSecurityContext();

            serializedClientSecurityContext.UserSid             = null;
            serializedClientSecurityContext.GroupSids           = null;
            serializedClientSecurityContext.RestrictedGroupSids = null;
            try
            {
                List <SidStringAndAttributes> list  = new List <SidStringAndAttributes>();
                List <SidStringAndAttributes> list2 = new List <SidStringAndAttributes>();
                if (!reader.Read() || XmlNodeType.Element != reader.NodeType || StringComparer.OrdinalIgnoreCase.Compare(reader.Name, SerializedClientSecurityContext.rootElementName) != 0)
                {
                    SerializedClientSecurityContext.ThrowParserException(reader, "Missing or invalid root node");
                }
                if (reader.MoveToFirstAttribute())
                {
                    do
                    {
                        if (StringComparer.OrdinalIgnoreCase.Compare(reader.Name, SerializedClientSecurityContext.authenticationTypeAttributeName) == 0)
                        {
                            if (serializedClientSecurityContext.authenticationType != null)
                            {
                                SerializedClientSecurityContext.ThrowParserException(reader, string.Format("Duplicated attribute {0}", SerializedClientSecurityContext.authenticationTypeAttributeName));
                            }
                            serializedClientSecurityContext.authenticationType = reader.Value;
                        }
                        else if (StringComparer.OrdinalIgnoreCase.Compare(reader.Name, SerializedClientSecurityContext.logonNameAttributeName) == 0)
                        {
                            if (serializedClientSecurityContext.logonName != null)
                            {
                                SerializedClientSecurityContext.ThrowParserException(reader, string.Format("Duplicated attribute {0}", SerializedClientSecurityContext.logonNameAttributeName));
                            }
                            serializedClientSecurityContext.logonName = reader.Value;
                        }
                        else
                        {
                            SerializedClientSecurityContext.ThrowParserException(reader, "Found invalid attribute in root element");
                        }
                    }while (reader.MoveToNextAttribute());
                }
                if (serializedClientSecurityContext.authenticationType == null || serializedClientSecurityContext.logonName == null)
                {
                    SerializedClientSecurityContext.ThrowParserException(reader, "Auth type or logon name attributes are missing");
                }
                bool flag = false;
                int  num  = 0;
                while (reader.Read())
                {
                    if (XmlNodeType.EndElement == reader.NodeType && StringComparer.OrdinalIgnoreCase.Compare(reader.Name, SerializedClientSecurityContext.rootElementName) == 0)
                    {
                        flag = true;
                        break;
                    }
                    if (XmlNodeType.Element != reader.NodeType || StringComparer.OrdinalIgnoreCase.Compare(reader.Name, SerializedClientSecurityContext.sidElementName) != 0)
                    {
                        SerializedClientSecurityContext.ThrowParserException(reader, "Expecting SID node");
                    }
                    SerializedClientSecurityContext.SidType sidType = SerializedClientSecurityContext.SidType.User;
                    uint num2 = 0U;
                    if (reader.MoveToFirstAttribute())
                    {
                        do
                        {
                            if (StringComparer.OrdinalIgnoreCase.Compare(reader.Name, SerializedClientSecurityContext.sidTypeAttributeName) == 0)
                            {
                                int num3 = int.Parse(reader.Value);
                                if (num3 == 1)
                                {
                                    sidType = SerializedClientSecurityContext.SidType.Group;
                                }
                                else if (num3 == 2)
                                {
                                    sidType = SerializedClientSecurityContext.SidType.RestrictedGroup;
                                }
                                else
                                {
                                    SerializedClientSecurityContext.ThrowParserException(reader, "Invalid SID type");
                                }
                            }
                            else if (StringComparer.OrdinalIgnoreCase.Compare(reader.Name, SerializedClientSecurityContext.sidAttributesAttributeName) == 0)
                            {
                                num2 = uint.Parse(reader.Value);
                            }
                            else
                            {
                                SerializedClientSecurityContext.ThrowParserException(reader, "Found invalid attribute in SID element");
                            }
                        }while (reader.MoveToNextAttribute());
                    }
                    if (sidType == SerializedClientSecurityContext.SidType.User)
                    {
                        if (num2 != 0U)
                        {
                            SerializedClientSecurityContext.ThrowParserException(reader, "'Attributes' shouldn't be set in an user SID");
                        }
                        else if (serializedClientSecurityContext.UserSid != null)
                        {
                            SerializedClientSecurityContext.ThrowParserException(reader, "There can only be one user SID in the XML document");
                        }
                    }
                    if (!reader.Read() || XmlNodeType.Text != reader.NodeType || string.IsNullOrEmpty(reader.Value))
                    {
                        SerializedClientSecurityContext.ThrowParserException(reader, "Expecting SID value in SDDL format");
                    }
                    string value = reader.Value;
                    if (sidType == SerializedClientSecurityContext.SidType.User)
                    {
                        serializedClientSecurityContext.UserSid = value;
                    }
                    else if (sidType == SerializedClientSecurityContext.SidType.Group)
                    {
                        SidStringAndAttributes item = new SidStringAndAttributes(value, num2);
                        list.Add(item);
                    }
                    else if (sidType == SerializedClientSecurityContext.SidType.RestrictedGroup)
                    {
                        SidStringAndAttributes item2 = new SidStringAndAttributes(value, num2);
                        list2.Add(item2);
                    }
                    if (!reader.Read() || XmlNodeType.EndElement != reader.NodeType)
                    {
                        SerializedClientSecurityContext.ThrowParserException(reader, "Expected end of SID node");
                    }
                    num++;
                    if (num > SerializedClientSecurityContext.maximumSidsPerContext)
                    {
                        throw new OwaSecurityContextSidLimitException(string.Format("Too many SID nodes in the request, maximum is {0}", SerializedClientSecurityContext.maximumSidsPerContext), serializedClientSecurityContext.logonName, serializedClientSecurityContext.authenticationType);
                    }
                }
                if (serializedClientSecurityContext.UserSid == null)
                {
                    SerializedClientSecurityContext.ThrowParserException(reader, "Serialized context should at least contain an user SID");
                }
                if (!flag)
                {
                    SerializedClientSecurityContext.ThrowParserException(reader, "Parsing error");
                }
                if (list.Count > 0)
                {
                    serializedClientSecurityContext.GroupSids = list.ToArray();
                }
                if (list2.Count > 0)
                {
                    serializedClientSecurityContext.RestrictedGroupSids = list2.ToArray();
                }
            }
            catch (XmlException ex)
            {
                SerializedClientSecurityContext.ThrowParserException(reader, string.Format("Parser threw an XML exception: {0}", ex.Message));
            }
            return(serializedClientSecurityContext);
        }