public void WriteXmlNoAudience () { SamlAudienceRestrictionCondition c = new SamlAudienceRestrictionCondition (); StringWriter sw = new StringWriter (); using (XmlDictionaryWriter dw = CreateWriter (sw)) { c.WriteXml (dw, new SamlSerializer (), null); } }
public void WriteXml1 () { SamlAudienceRestrictionCondition c = new SamlAudienceRestrictionCondition (new Uri [] {new Uri ("urn:myAudience")}); StringWriter sw = new StringWriter (); using (XmlDictionaryWriter dw = CreateWriter (sw)) { c.WriteXml (dw, new SamlSerializer (), null); } Assert.AreEqual (String.Format ("<?xml version=\"1.0\" encoding=\"utf-16\"?><saml:AudienceRestrictionCondition xmlns:saml=\"{0}\"><saml:Audience>urn:myAudience</saml:Audience></saml:AudienceRestrictionCondition>", SamlConstants.Namespace), sw.ToString ()); }
public virtual SamlCondition LoadCondition(XmlDictionaryReader reader, SecurityTokenSerializer keyInfoSerializer, SecurityTokenResolver outOfBandTokenResolver) { if (reader == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader"); } if (reader.IsStartElement(this.DictionaryManager.SamlDictionary.AudienceRestrictionCondition, this.DictionaryManager.SamlDictionary.Namespace)) { SamlAudienceRestrictionCondition condition = new SamlAudienceRestrictionCondition(); condition.ReadXml(reader, this, keyInfoSerializer, outOfBandTokenResolver); return condition; } if (!reader.IsStartElement(this.DictionaryManager.SamlDictionary.DoNotCacheCondition, this.DictionaryManager.SamlDictionary.Namespace)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.IdentityModel.SR.GetString("SAMLUnableToLoadUnknownElement", new object[] { reader.LocalName }))); } SamlDoNotCacheCondition condition2 = new SamlDoNotCacheCondition(); condition2.ReadXml(reader, this, keyInfoSerializer, outOfBandTokenResolver); return condition2; }
public virtual SamlCondition LoadCondition(XmlDictionaryReader reader, SecurityTokenSerializer keyInfoSerializer, SecurityTokenResolver outOfBandTokenResolver) { if (reader == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader"); } if (reader.IsStartElement(this.DictionaryManager.SamlDictionary.AudienceRestrictionCondition, this.DictionaryManager.SamlDictionary.Namespace)) { SamlAudienceRestrictionCondition condition = new SamlAudienceRestrictionCondition(); condition.ReadXml(reader, this, keyInfoSerializer, outOfBandTokenResolver); return(condition); } if (!reader.IsStartElement(this.DictionaryManager.SamlDictionary.DoNotCacheCondition, this.DictionaryManager.SamlDictionary.Namespace)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.IdentityModel.SR.GetString("SAMLUnableToLoadUnknownElement", new object[] { reader.LocalName }))); } SamlDoNotCacheCondition condition2 = new SamlDoNotCacheCondition(); condition2.ReadXml(reader, this, keyInfoSerializer, outOfBandTokenResolver); return(condition2); }
public virtual SamlCondition LoadCondition(XmlDictionaryReader reader, SecurityTokenSerializer keyInfoSerializer, SecurityTokenResolver outOfBandTokenResolver) { if (reader == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader"); } if (reader.IsStartElement(DictionaryManager.SamlDictionary.AudienceRestrictionCondition, DictionaryManager.SamlDictionary.Namespace)) { SamlAudienceRestrictionCondition audienceRestriction = new SamlAudienceRestrictionCondition(); audienceRestriction.ReadXml(reader, this, keyInfoSerializer, outOfBandTokenResolver); return(audienceRestriction); } else if (reader.IsStartElement(DictionaryManager.SamlDictionary.DoNotCacheCondition, DictionaryManager.SamlDictionary.Namespace)) { SamlDoNotCacheCondition doNotCacheCondition = new SamlDoNotCacheCondition(); doNotCacheCondition.ReadXml(reader, this, keyInfoSerializer, outOfBandTokenResolver); return(doNotCacheCondition); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.SAMLUnableToLoadUnknownElement, reader.LocalName))); } }
protected virtual bool ValidateAudienceRestriction(SamlAudienceRestrictionCondition audienceRestrictionCondition) { for (int i = 0; i < audienceRestrictionCondition.Audiences.Count; i++) { if (audienceRestrictionCondition.Audiences[i] == null) continue; for (int j = 0; j < this.allowedAudienceUris.Count; j++) { if (StringComparer.Ordinal.Compare(audienceRestrictionCondition.Audiences[i].AbsoluteUri, this.allowedAudienceUris[j]) == 0) return true; else if (Uri.IsWellFormedUriString(this.allowedAudienceUris[j], UriKind.Absolute)) { Uri uri = new Uri(this.allowedAudienceUris[j]); if (audienceRestrictionCondition.Audiences[i].Equals(uri)) return true; } } } return false; }
/// <summary> /// Serialize SamlAudienceRestrictionCondition to a XmlWriter. /// </summary> /// <param name="writer">XmlWriter to serialize the SamlAudienceRestrictionCondition.</param> /// <param name="condition">SamlAudienceRestrictionCondition to serialize.</param> /// <exception cref="ArgumentNullException">The parameter 'writer' or 'condition' is null.</exception> protected virtual void WriteAudienceRestrictionCondition(XmlWriter writer, SamlAudienceRestrictionCondition condition) { if (writer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer"); } if (condition == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("condition"); } // Schema requires at least one audience. if (condition.Audiences == null || condition.Audiences.Count == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException(SR.GetString(SR.ID4269))); } writer.WriteStartElement(SamlConstants.Prefix, SamlConstants.ElementNames.AudienceRestrictionCondition, SamlConstants.Namespace); for (int i = 0; i < condition.Audiences.Count; i++) { // When writing out the audience uri we use the OriginalString property to preserve the value that was initially passed down during token creation as-is. writer.WriteElementString(SamlConstants.ElementNames.Audience, SamlConstants.Namespace, condition.Audiences[i].OriginalString); } writer.WriteEndElement(); }
/// <summary> /// Read saml:AudienceRestrictionCondition from the given XmlReader. /// </summary> /// <param name="reader">XmlReader positioned at a saml:AudienceRestrictionCondition.</param> /// <returns>SamlAudienceRestrictionCondition</returns> /// <exception cref="ArgumentNullException">The inpur parameter 'reader' is null.</exception> /// <exception cref="XmlException">The XmlReader is not positioned at saml:AudienceRestrictionCondition.</exception> protected virtual SamlAudienceRestrictionCondition ReadAudienceRestrictionCondition(XmlReader reader) { if (reader == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader"); } if (!reader.IsStartElement(SamlConstants.ElementNames.AudienceRestrictionCondition, SamlConstants.Namespace)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.ID4082, SamlConstants.ElementNames.AudienceRestrictionCondition, SamlConstants.Namespace, reader.LocalName, reader.NamespaceURI))); } reader.ReadStartElement(); SamlAudienceRestrictionCondition audienceRestrictionCondition = new SamlAudienceRestrictionCondition(); while (reader.IsStartElement()) { if (reader.IsStartElement(SamlConstants.ElementNames.Audience, SamlConstants.Namespace)) { string audience = reader.ReadString(); if (string.IsNullOrEmpty(audience)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.ID4083))); } audienceRestrictionCondition.Audiences.Add(new Uri(audience, UriKind.RelativeOrAbsolute)); reader.MoveToContent(); reader.ReadEndElement(); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.ID4082, SamlConstants.ElementNames.Audience, SamlConstants.Namespace, reader.LocalName, reader.NamespaceURI))); } } if (audienceRestrictionCondition.Audiences.Count == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.ID4084))); } reader.MoveToContent(); reader.ReadEndElement(); return audienceRestrictionCondition; }
public virtual SamlCondition LoadCondition(XmlDictionaryReader reader, SecurityTokenSerializer keyInfoSerializer, SecurityTokenResolver outOfBandTokenResolver) { if (reader == null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader"); if (reader.IsStartElement(DictionaryManager.SamlDictionary.AudienceRestrictionCondition, DictionaryManager.SamlDictionary.Namespace)) { SamlAudienceRestrictionCondition audienceRestriction = new SamlAudienceRestrictionCondition(); audienceRestriction.ReadXml(reader, this, keyInfoSerializer, outOfBandTokenResolver); return audienceRestriction; } else if (reader.IsStartElement(DictionaryManager.SamlDictionary.DoNotCacheCondition, DictionaryManager.SamlDictionary.Namespace)) { SamlDoNotCacheCondition doNotCacheCondition = new SamlDoNotCacheCondition(); doNotCacheCondition.ReadXml(reader, this, keyInfoSerializer, outOfBandTokenResolver); return doNotCacheCondition; } else throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.SAMLUnableToLoadUnknownElement, reader.LocalName))); }