5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
31 using System.IdentityModel.Selectors;
33 namespace System.IdentityModel.Tokens
35 public class SamlAdvice
37 List<string> idrefs = new List<string> ();
38 List<SamlAssertion> assertions = new List<SamlAssertion> ();
45 public SamlAdvice (IEnumerable<SamlAssertion> assertions)
46 : this (new string [0], assertions)
50 public SamlAdvice (IEnumerable<string> references)
51 : this (references, new SamlAssertion [0])
55 public SamlAdvice (IEnumerable<string> references, IEnumerable<SamlAssertion> assertions)
57 if (references == null)
58 throw new ArgumentException ("references are null.");
59 if (assertions == null)
60 throw new ArgumentException ("assertions are null.");
61 foreach (string r in references) {
63 throw new ArgumentException ("references contain null item.");
66 foreach (SamlAssertion a in assertions) {
68 throw new ArgumentException ("assertions contain null item.");
69 this.assertions.Add (a);
73 public bool IsReadOnly {
74 get { return is_readonly; }
77 public IList<SamlAssertion> Assertions {
78 get { return assertions; }
81 public IList<string> AssertionIdReferences {
82 get { return idrefs; }
85 public void MakeReadOnly ()
90 public virtual void ReadXml (XmlDictionaryReader reader,
91 SamlSerializer samlSerializer,
92 SecurityTokenSerializer keyInfoSerializer,
93 SecurityTokenResolver outOfBandTokenResolver)
96 throw new ArgumentNullException ("reader");
97 if (samlSerializer == null)
98 throw new ArgumentNullException ("samlSerializer");
99 reader.ReadStartElement ("Advice", SamlConstants.Namespace);
100 for (reader.MoveToContent ();
101 reader.NodeType == XmlNodeType.Element;
102 reader.MoveToContent ()) {
103 if (reader.NamespaceURI != SamlConstants.Namespace)
104 throw new SecurityTokenException (String.Format ("Invalid SAML Advice element: element '{0}' in namespace '{1}' is unexpected.", reader.LocalName, reader.NamespaceURI));
105 switch (reader.LocalName) {
107 SamlAssertion a = new SamlAssertion ();
108 a.ReadXml (reader, samlSerializer, keyInfoSerializer, outOfBandTokenResolver);
111 case "AssertionIDReference":
112 idrefs.Add (reader.ReadElementContentAsString ());
115 throw new SecurityTokenException (String.Format ("Invalid SAML Advice element: SAML element '{0}' is unexpected.", reader.LocalName));
118 reader.ReadEndElement ();
121 public virtual void WriteXml (XmlDictionaryWriter writer,
122 SamlSerializer samlSerializer,
123 SecurityTokenSerializer keyInfoSerializer)
126 throw new ArgumentNullException ("writer");
127 if (samlSerializer == null)
128 throw new ArgumentNullException ("samlSerializer");
129 writer.WriteStartElement ("saml", "Advice", SamlConstants.Namespace);
130 foreach (string idref in AssertionIdReferences)
131 writer.WriteElementString ("saml", "AssertionIDReference", SamlConstants.Namespace, idref);
132 foreach (SamlAssertion assertion in Assertions)
133 assertion.WriteXml (writer, samlSerializer, keyInfoSerializer);
134 writer.WriteEndElement ();