ReadElementContentAsBase64() public method

public ReadElementContentAsBase64 ( ) : byte[]
return byte[]
示例#1
0
        /// <summary>
        /// Reads the XML stream or document with an <see cref="T:System.Xml.XmlDictionaryReader" /> and returns the deserialized object; it also enables you to specify whether the serializer can read the data before attempting to read it.
        /// </summary>
        /// <param name="reader">An <see cref="T:System.Xml.XmlDictionaryReader" /> used to read the XML document.</param>
        /// <param name="verifyObjectName">true to check whether the enclosing XML element name and namespace correspond to the root name and root namespace; otherwise, false to skip the verification.</param>
        /// <returns>
        /// The deserialized object.
        /// </returns>
        public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
        {
            Argument.IsNotNull("reader", reader);

            var memoryStream = new MemoryStream(reader.ReadElementContentAsBase64());
            return BinarySerializerHelper.DiscoverAndDeSerialize(memoryStream, _type);
        }
		public static void Deserialize(XmlDictionaryReader reader, out IList<DirectoryControl> controls, bool mustBePresent, bool fullChecks)
		{
			string str = null;
			string str1 = null;
			bool flag;
			byte[] numArray = null;
			controls = new List<DirectoryControl>();
			if (mustBePresent || reader.IsStartElement("controls", "http://schemas.microsoft.com/2008/1/ActiveDirectory"))
			{
				reader.ReadFullStartElement("controls", "http://schemas.microsoft.com/2008/1/ActiveDirectory");
				while (reader.IsStartElement("control", "http://schemas.microsoft.com/2008/1/ActiveDirectory"))
				{
					string attribute = reader.GetAttribute("type");
					string attribute1 = reader.GetAttribute("criticality");
					reader.Read();
					if (!reader.IsStartElement("controlValue", "http://schemas.microsoft.com/2008/1/ActiveDirectory"))
					{
						numArray = null;
					}
					else
					{
						string attribute2 = reader.GetAttribute("type", "http://www.w3.org/2001/XMLSchema-instance");
						if (attribute2 != null)
						{
							XmlUtility.SplitPrefix(attribute2, out str, out str1);
							numArray = reader.ReadElementContentAsBase64();
						}
						else
						{
							throw new ArgumentException();
						}
					}
					if (!string.Equals("true", attribute1))
					{
						flag = false;
					}
					else
					{
						flag = true;
					}
					DirectoryControl directoryControl = new DirectoryControl(attribute, numArray, flag, true);
					controls.Add(directoryControl);
					reader.Read();
				}
				return;
			}
			else
			{
				return;
			}
		}
 public object ReadValue(XmlDictionaryReader reader)
 {
     object value;
     if (_isArray)
     {
         switch (_typeCode)
         {
             case TypeCode.Byte:
                 value = reader.ReadElementContentAsBase64();
                 break;
             case TypeCode.Boolean:
                 if (!reader.IsEmptyElement)
                 {
                     reader.ReadStartElement();
                     value = reader.ReadBooleanArray(_itemName, _itemNamespace);
                     reader.ReadEndElement();
                 }
                 else
                 {
                     reader.Read();
                     value = Array.Empty<bool>();
                 }
                 break;
             case TypeCode.DateTime:
                 if (!reader.IsEmptyElement)
                 {
                     reader.ReadStartElement();
                     value = reader.ReadDateTimeArray(_itemName, _itemNamespace);
                     reader.ReadEndElement();
                 }
                 else
                 {
                     reader.Read();
                     value = Array.Empty<DateTime>();
                 }
                 break;
             case TypeCode.Decimal:
                 if (!reader.IsEmptyElement)
                 {
                     reader.ReadStartElement();
                     value = reader.ReadDecimalArray(_itemName, _itemNamespace);
                     reader.ReadEndElement();
                 }
                 else
                 {
                     reader.Read();
                     value = Array.Empty<Decimal>();
                 }
                 break;
             case TypeCode.Int32:
                 if (!reader.IsEmptyElement)
                 {
                     reader.ReadStartElement();
                     value = reader.ReadInt32Array(_itemName, _itemNamespace);
                     reader.ReadEndElement();
                 }
                 else
                 {
                     reader.Read();
                     value = Array.Empty<Int32>();
                 }
                 break;
             case TypeCode.Int64:
                 if (!reader.IsEmptyElement)
                 {
                     reader.ReadStartElement();
                     value = reader.ReadInt64Array(_itemName, _itemNamespace);
                     reader.ReadEndElement();
                 }
                 else
                 {
                     reader.Read();
                     value = Array.Empty<Int64>();
                 }
                 break;
             case TypeCode.Single:
                 if (!reader.IsEmptyElement)
                 {
                     reader.ReadStartElement();
                     value = reader.ReadSingleArray(_itemName, _itemNamespace);
                     reader.ReadEndElement();
                 }
                 else
                 {
                     reader.Read();
                     value = Array.Empty<Single>();
                 }
                 break;
             case TypeCode.Double:
                 if (!reader.IsEmptyElement)
                 {
                     reader.ReadStartElement();
                     value = reader.ReadDoubleArray(_itemName, _itemNamespace);
                     reader.ReadEndElement();
                 }
                 else
                 {
                     reader.Read();
                     value = Array.Empty<Double>();
                 }
                 break;
             default:
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxInvalidUseOfPrimitiveOperationFormatter));
         }
     }
     else
     {
         switch (_typeCode)
         {
             case TypeCode.Boolean:
                 value = reader.ReadElementContentAsBoolean();
                 break;
             case TypeCode.DateTime:
                 value = reader.ReadElementContentAsDateTime();
                 break;
             case TypeCode.Decimal:
                 value = reader.ReadElementContentAsDecimal();
                 break;
             case TypeCode.Double:
                 value = reader.ReadElementContentAsDouble();
                 break;
             case TypeCode.Int32:
                 value = reader.ReadElementContentAsInt();
                 break;
             case TypeCode.Int64:
                 value = reader.ReadElementContentAsLong();
                 break;
             case TypeCode.Single:
                 value = reader.ReadElementContentAsFloat();
                 break;
             case TypeCode.String:
                 return reader.ReadElementContentAsString();
             default:
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxInvalidUseOfPrimitiveOperationFormatter));
         }
     }
     return value;
 }
            public override SecurityToken ReadTokenCore(XmlDictionaryReader reader, SecurityTokenResolver tokenResolver)
            {
                string wsuId = reader.GetAttribute(XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace);
                string valueTypeUri = reader.GetAttribute(ValueTypeAttribute, null);
                string encoding = reader.GetAttribute(EncodingTypeAttribute, null);

                byte[] binaryData;
                if (encoding == null || encoding == EncodingTypeValueBase64Binary)
                {
                    binaryData = reader.ReadElementContentAsBase64();
                }
                else if (encoding == EncodingTypeValueHexBinary)
                {
                    binaryData = HexBinary.Parse(reader.ReadElementContentAsString()).Value;
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.UnknownEncodingInBinarySecurityToken)));
                }

                return ReadBinaryCore(wsuId, valueTypeUri, binaryData);
            }
            public object ReadValue(XmlDictionaryReader reader)
            {
                object obj2;
                if (!this.isArray)
                {
                    switch (this.typeCode)
                    {
                        case TypeCode.Int32:
                            return reader.ReadElementContentAsInt();

                        case TypeCode.Int64:
                            return reader.ReadElementContentAsLong();

                        case TypeCode.Single:
                            return reader.ReadElementContentAsFloat();

                        case TypeCode.Double:
                            return reader.ReadElementContentAsDouble();

                        case TypeCode.Decimal:
                            return reader.ReadElementContentAsDecimal();

                        case TypeCode.DateTime:
                            return reader.ReadElementContentAsDateTime();

                        case TypeCode.String:
                            return reader.ReadElementContentAsString();

                        case TypeCode.Boolean:
                            return reader.ReadElementContentAsBoolean();
                    }
                    throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("SFxInvalidUseOfPrimitiveOperationFormatter")));
                }
                switch (this.typeCode)
                {
                    case TypeCode.Boolean:
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return new bool[0];
                        }
                        reader.ReadStartElement();
                        obj2 = reader.ReadBooleanArray(this.itemName, this.itemNamespace);
                        reader.ReadEndElement();
                        return obj2;

                    case TypeCode.Byte:
                        return reader.ReadElementContentAsBase64();

                    case TypeCode.Int32:
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return new int[0];
                        }
                        reader.ReadStartElement();
                        obj2 = reader.ReadInt32Array(this.itemName, this.itemNamespace);
                        reader.ReadEndElement();
                        return obj2;

                    case TypeCode.Int64:
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return new long[0];
                        }
                        reader.ReadStartElement();
                        obj2 = reader.ReadInt64Array(this.itemName, this.itemNamespace);
                        reader.ReadEndElement();
                        return obj2;

                    case TypeCode.Single:
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return new float[0];
                        }
                        reader.ReadStartElement();
                        obj2 = reader.ReadSingleArray(this.itemName, this.itemNamespace);
                        reader.ReadEndElement();
                        return obj2;

                    case TypeCode.Double:
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return new double[0];
                        }
                        reader.ReadStartElement();
                        obj2 = reader.ReadDoubleArray(this.itemName, this.itemNamespace);
                        reader.ReadEndElement();
                        return obj2;

                    case TypeCode.Decimal:
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return new decimal[0];
                        }
                        reader.ReadStartElement();
                        obj2 = reader.ReadDecimalArray(this.itemName, this.itemNamespace);
                        reader.ReadEndElement();
                        return obj2;

                    case TypeCode.DateTime:
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return new DateTime[0];
                        }
                        reader.ReadStartElement();
                        obj2 = reader.ReadDateTimeArray(this.itemName, this.itemNamespace);
                        reader.ReadEndElement();
                        return obj2;
                }
                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("SFxInvalidUseOfPrimitiveOperationFormatter")));
            }
            public override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore( XmlDictionaryReader reader )
            {
                SecurityKeyIdentifierClause ski = null;
                reader.ReadStartElement( XD.XmlSignatureDictionary.X509Data, NamespaceUri );
                while ( reader.IsStartElement() )
                {
                    if ( ski == null && reader.IsStartElement( XD.XmlSignatureDictionary.X509Certificate, NamespaceUri ) )
                    {
                        X509Certificate2 certificate = null;
                        if ( !SecurityUtils.TryCreateX509CertificateFromRawData( reader.ReadElementContentAsBase64(), out certificate ) )
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new SecurityMessageSerializationException( SR.GetString( SR.InvalidX509RawData ) ) );
                        }
                        ski = new X509RawDataKeyIdentifierClause( certificate );
                    }
                    else if ( ski == null && reader.IsStartElement( XmlSignatureStrings.X509Ski, NamespaceUri.ToString() ) )
                    {
                        ski = new X509SubjectKeyIdentifierClause( reader.ReadElementContentAsBase64() );
                    }
                    else if ( ( ski == null ) && reader.IsStartElement( XD.XmlSignatureDictionary.X509IssuerSerial, XD.XmlSignatureDictionary.Namespace ) )
                    {
                        reader.ReadStartElement( XD.XmlSignatureDictionary.X509IssuerSerial, XD.XmlSignatureDictionary.Namespace );
                        reader.ReadStartElement( XD.XmlSignatureDictionary.X509IssuerName, XD.XmlSignatureDictionary.Namespace );
                        string issuerName = reader.ReadContentAsString();
                        reader.ReadEndElement();
                        reader.ReadStartElement( XD.XmlSignatureDictionary.X509SerialNumber, XD.XmlSignatureDictionary.Namespace );
                        string serialNumber = reader.ReadContentAsString();
                        reader.ReadEndElement();
                        reader.ReadEndElement();

                        ski = new X509IssuerSerialKeyIdentifierClause( issuerName, serialNumber );
                    }
                    else
                    {
                        reader.Skip();
                    }
                }
                reader.ReadEndElement();
                return ski;
            }
        /// <summary>
        /// Parses the "X509Certificate" element and generates a corresponding <see cref="X509RawDataKeyIdentifierClause"/> instance.
        /// </summary>
        /// <param name="dictionaryReader">The <see cref="XmlDictionaryReader"/> currently positioning on the "X509Certificate" element. </param>
        /// <returns>An instance of <see cref="X509RawDataKeyIdentifierClause"/> created from the "X509Certificate" element.</returns>
        private static SecurityKeyIdentifierClause CreateRawDataKeyIdentifierClause(XmlDictionaryReader dictionaryReader)
        {
#if INCLUDE_CERT_CHAIN                
                List<byte[]> rawDatas = new List<byte[]>();
                while (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509Certificate, XmlSignatureConstants.Namespace))
                {
                    byte[] rawBuffer = dictionaryReader.ReadElementContentAsBase64();
                    if (rawBuffer == null || rawBuffer.Length == 0)
                    {
                        throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID4258, XmlSignatureConstants.Elements.X509Certificate, XmlSignatureConstants.Namespace));
                    }

                    rawDatas.Add(rawBuffer);
                }

                if (rawDatas.Count > 1)
                {
                    return new X509ChainRawDataKeyIdentifierClause(rawDatas);
                }
                else
                {
                    return new X509RawDataKeyIdentifierClause(rawDatas[0]);
                }
#else
            byte[] rawData = null;
            while (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509Certificate, XmlSignatureConstants.Namespace))
            {
                if (rawData == null)
                {
                    rawData = dictionaryReader.ReadElementContentAsBase64();
                    if ((rawData == null) || (rawData.Length == 0))
                    {
                        throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID4258, XmlSignatureConstants.Elements.X509Certificate, XmlSignatureConstants.Namespace));
                    }
                }
                else
                {
                    // We do not support reading intermediary certs.
                    dictionaryReader.Skip();
                }
            }

            return new X509RawDataKeyIdentifierClause(rawData);
#endif
        }
        /// <summary>
        /// Parses the "X509SKI" element and generates a corresponding <see cref="SecurityKeyIdentifierClause"/> instance.
        /// </summary>
        /// <param name="dictionaryReader">The <see cref="XmlDictionaryReader"/> currently positioning on the "X509SKI" element. </param>
        /// <returns>An instance of <see cref="X509SubjectKeyIdentifierClause"/> created from the "X509SKI" element.</returns>
        private static SecurityKeyIdentifierClause CreateSubjectKeyIdentifierClause(XmlDictionaryReader dictionaryReader)
        {
            byte[] ski = dictionaryReader.ReadElementContentAsBase64();
            if ((ski == null) || (ski.Length == 0))
            {
                throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID4258, XmlSignatureConstants.Elements.X509SKI, XmlSignatureConstants.Namespace));
            }

            return new X509SubjectKeyIdentifierClause(ski);
        }
        public static object Deserialize(XmlDictionaryReader reader, Type targetType)
        {
            object returnValue = null;

            if (reader.IsStartElement())
            {
                switch (reader.LocalName)
                {
                    case XmlRpcProtocol.Nil:
                        returnValue = null;
                        break;
                    case XmlRpcProtocol.Bool:
                        returnValue = Convert.ChangeType((reader.ReadElementContentAsInt()==1),targetType);
                        break;
                    case XmlRpcProtocol.ByteArray:
                        if (targetType == typeof(Stream))
                        {
                            returnValue = new MemoryStream(reader.ReadElementContentAsBase64());
                        }
                        else
                        {
                            returnValue = Convert.ChangeType(reader.ReadElementContentAsBase64(), targetType);
                        }
                        break;
                    case XmlRpcProtocol.DateTime:
                        returnValue = Convert.ChangeType(reader.ReadElementContentAsDateTime(),targetType);
                        break;
                    case XmlRpcProtocol.Double:
                        returnValue = Convert.ChangeType(reader.ReadElementContentAsDouble(),targetType);
                        break;
                    case XmlRpcProtocol.Int32:
                    case XmlRpcProtocol.Integer:
                        returnValue = Convert.ChangeType(reader.ReadElementContentAsString(),targetType);
                        break;
                    case XmlRpcProtocol.String:
                        if (targetType == typeof(Uri))
                        {
                            returnValue = new Uri(reader.ReadElementContentAsString());
                        }
                        else
                        {
                            returnValue = Convert.ChangeType(reader.ReadElementContentAsString(), targetType);
                        }
                        break;
                    case XmlRpcProtocol.Struct:
                        returnValue = DeserializeStruct(reader, targetType);
                        break;
                    case XmlRpcProtocol.Array:
                        if (targetType.IsArray || targetType is IEnumerable || targetType is IList || targetType is ICollection)
                        {
                            reader.ReadStartElement(XmlRpcProtocol.Array);
                            ArrayList arrayData = new ArrayList();
                            reader.ReadStartElement(XmlRpcProtocol.Data);
                            reader.MoveToContent();
                            while (reader.IsStartElement(XmlRpcProtocol.Value))
                            {
                                reader.ReadStartElement();
                                arrayData.Add(Deserialize(reader, targetType.GetElementType()));
                                reader.ReadEndElement();
                                reader.MoveToContent();
                            }
                            if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == XmlRpcProtocol.Data)
                            {
                                reader.ReadEndElement();
                            }
                            reader.ReadEndElement();

                            if (targetType is IEnumerable || targetType is IList || targetType is ICollection)
                            {
                                returnValue = arrayData;
                            }
                            else
                            {
                                returnValue = arrayData.ToArray(targetType.GetElementType());
                            }                            
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                        break;
                }
            }
            return returnValue;
        }
示例#10
0
 public override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlDictionaryReader reader)
 {
     byte[] secret = reader.ReadElementContentAsBase64();
     return new BinarySecretKeyIdentifierClause(secret, false);
 }
            public override SecurityToken ReadTokenCore(XmlDictionaryReader reader, SecurityTokenResolver tokenResolver)
            {
                byte[] buffer;
                string attribute = reader.GetAttribute(XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace);
                string valueTypeUri = reader.GetAttribute(ValueTypeAttribute, null);
                string str3 = reader.GetAttribute(EncodingTypeAttribute, null);
                switch (str3)
                {
                    case null:
                    case "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary":
                        buffer = reader.ReadElementContentAsBase64();
                        break;

                    default:
                        if (str3 != "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#HexBinary")
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(System.ServiceModel.SR.GetString("UnknownEncodingInBinarySecurityToken")));
                        }
                        buffer = SoapHexBinary.Parse(reader.ReadElementContentAsString()).Value;
                        break;
                }
                return this.ReadBinaryCore(attribute, valueTypeUri, buffer);
            }
            public override SecurityToken ReadTokenCore(XmlDictionaryReader reader, SecurityTokenResolver tokenResolver)
            {
                UniqueId contextId = null;
                byte[] encodedCookie = null;
                UniqueId generation = null;
                bool isCookieMode = false;

                Fx.Assert(reader.NodeType == XmlNodeType.Element, "");

                // check if there is an id
                string id = reader.GetAttribute(XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace);

                SecurityContextSecurityToken sct = null;

                // There needs to be at least a contextId in here.
                reader.ReadFullStartElement();
                reader.MoveToStartElement(parent.SerializerDictionary.Identifier, parent.SerializerDictionary.Namespace);
                contextId = reader.ReadElementContentAsUniqueId();
                if (CanReadGeneration(reader))
                {
                    generation = ReadGeneration(reader);
                }
                if (reader.IsStartElement(parent.SerializerDictionary.Cookie, XD.DotNetSecurityDictionary.Namespace))
                {
                    isCookieMode = true;
                    ISecurityContextSecurityTokenCache sctCache;
                    sct = TryResolveSecurityContextToken(contextId, generation, id, tokenResolver, out sctCache);
                    if (sct == null)
                    {
                        encodedCookie = reader.ReadElementContentAsBase64();
                        if (encodedCookie != null)
                        {
                            sct = cookieSerializer.CreateSecurityContextFromCookie(encodedCookie, contextId, generation, id, reader.Quotas);
                            if (sctCache != null)
                            {
                                sctCache.AddContext(sct);
                            }
                        }
                    }
                    else
                    {
                        reader.Skip();
                    }
                }
                reader.ReadEndElement();

                if (contextId == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.NoSecurityContextIdentifier)));
                }

                if (sct == null && !isCookieMode)
                {
                    ISecurityContextSecurityTokenCache sctCache;
                    sct = TryResolveSecurityContextToken(contextId, generation, id, tokenResolver, out sctCache);
                }
                if (sct == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new SecurityContextTokenValidationException(SR.GetString(SR.SecurityContextNotRegistered, contextId, generation)));
                }
                return sct;
            }
 public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
 {
     if (this.isCustomSerialization)
     {
         var result = Activator.CreateInstance(this.type);
         using (var ms = new MemoryStream(reader.ReadElementContentAsBase64()))
         {
             ((ICustomSerializable)result).InitializeFrom(ms);
         }
         return result;
     }
     else
     {
         return this.fallbackSerializer.ReadObject(reader, verifyObjectName);
     }
 }