SamlAttribute.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / IdentityModel / System / IdentityModel / Tokens / SamlAttribute.cs / 1305376 / SamlAttribute.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.IdentityModel.Tokens 
{
    using System.Collections; 
    using System.Collections.Generic; 
    using System.Collections.ObjectModel;
    using System.IdentityModel.Claims; 
    using System.IdentityModel.Selectors;
    using System.Globalization;
    using System.Xml;
    using System.Xml.Serialization; 

    public class SamlAttribute 
    { 
        string name;
        string nameSpace; 
        readonly ImmutableCollection attributeValues = new ImmutableCollection();

        List claims;
        string claimType; 
        bool isReadOnly = false;
 
        public SamlAttribute(string attributeNamespace, string attributeName, IEnumerable attributeValues) 
        {
            if (String.IsNullOrEmpty(attributeName)) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeNameAttributeRequired));

            if (String.IsNullOrEmpty(attributeNamespace))
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeNamespaceAttributeRequired)); 

            if (attributeValues == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("attributeValues"); 

            this.name = attributeName; 
            this.nameSpace = attributeNamespace;
            this.claimType = String.IsNullOrEmpty(this.nameSpace) ? this.name : this.nameSpace + "/" + this.name;

            foreach (string value in attributeValues) 
            {
                if (value == null) 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeValueCannotBeNull)); 

                this.attributeValues.Add(value); 
            }

            if (this.attributeValues.Count == 0)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeShouldHaveOneValue)); 
        }
 
        public SamlAttribute() 
        {
        } 

        public SamlAttribute(Claim claim)
        {
            if (claim == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("claim");
 
            if (!(claim.Resource is String)) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SamlAttributeClaimResourceShouldBeAString));
 
            if (claim.Right != Rights.PossessProperty)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SamlAttributeClaimRightShouldBePossessProperty));

#pragma warning suppress 56506 // claim.CalimType can never be null. 
            int lastSlashIndex = claim.ClaimType.LastIndexOf('/');
            if ((lastSlashIndex == -1) || (lastSlashIndex == 0) || (lastSlashIndex == claim.ClaimType.Length - 1)) 
            { 
                this.nameSpace = String.Empty;
                this.name = claim.ClaimType; 
            }
            else
            {
                this.nameSpace = claim.ClaimType.Substring(0, lastSlashIndex); 
                this.name = claim.ClaimType.Substring(lastSlashIndex + 1, claim.ClaimType.Length - (lastSlashIndex + 1));
            } 
            this.claimType = claim.ClaimType; 
            this.attributeValues.Add(claim.Resource as string);
        } 

        public string Name
        {
            get {return this.name; } 
            set
            { 
                if (isReadOnly) 
                    throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
 
                if (String.IsNullOrEmpty(value))
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeNameAttributeRequired));

                this.name = value; 
            }
        } 
 
        public string Namespace
        { 
            get {return this.nameSpace; }
            set
            {
                if (isReadOnly) 
                    throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
 
                if (String.IsNullOrEmpty(value)) 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeNamespaceAttributeRequired));
 
                this.nameSpace = value;
            }
        }
 
        public IList AttributeValues
        { 
            get {return this.attributeValues; } 
        }
 
        public bool IsReadOnly
        {
            get { return this.isReadOnly; }
        } 

        public void MakeReadOnly() 
        { 
            if (!this.isReadOnly)
            { 
                this.attributeValues.MakeReadOnly();

                this.isReadOnly = true;
            } 
        }
 
        public virtual ReadOnlyCollection ExtractClaims() 
        {
            if (this.claims == null) 
            {
                List tempClaims = new List(this.attributeValues.Count);

                for (int i = 0; i < this.attributeValues.Count; i++) 
                {
                    if (this.attributeValues[i] == null) 
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeValueCannotBeNull)); 

                    tempClaims.Add(new Claim(this.claimType, this.attributeValues[i], Rights.PossessProperty)); 
                }
                this.claims = tempClaims;
            }
 
            return this.claims.AsReadOnly();
        } 
 
        void CheckObjectValidity()
        { 
            if (String.IsNullOrEmpty(this.name))
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeNameAttributeRequired)));

            if (String.IsNullOrEmpty(this.nameSpace)) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeNamespaceAttributeRequired)));
 
            if (this.attributeValues.Count == 0) 
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeShouldHaveOneValue)));
        } 

        public virtual void ReadXml(XmlDictionaryReader reader, SamlSerializer samlSerializer, SecurityTokenSerializer keyInfoSerializer, SecurityTokenResolver outOfBandTokenResolver)
        {
            if (reader == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("reader"));
 
            if (samlSerializer == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("samlSerializer"));
 
#pragma warning suppress 56506 // samlSerializer.DictionaryManager is never null.
            SamlDictionary dictionary = samlSerializer.DictionaryManager.SamlDictionary;

            this.name = reader.GetAttribute(dictionary.AttributeName, null); 
            if (String.IsNullOrEmpty(this.name))
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeMissingNameAttributeOnRead))); 
 
            this.nameSpace = reader.GetAttribute(dictionary.AttributeNamespace, null);
            if (String.IsNullOrEmpty(this.nameSpace)) 
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeMissingNamespaceAttributeOnRead)));

            this.claimType = String.IsNullOrEmpty(this.nameSpace) ? this.name : this.nameSpace + "/" + this.name;
 
            reader.MoveToContent();
            reader.Read(); 
            while (reader.IsStartElement(dictionary.AttributeValue, dictionary.Namespace)) 
            {
                // We will load all Attributes as a string value by default. 
                string attrValue = reader.ReadString();
                this.attributeValues.Add(attrValue);

                reader.MoveToContent(); 
                reader.ReadEndElement();
            } 
 
            if (this.attributeValues.Count == 0)
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeShouldHaveOneValue))); 

            reader.MoveToContent();
            reader.ReadEndElement();
        } 

        public virtual void WriteXml(XmlDictionaryWriter writer, SamlSerializer samlSerializer, SecurityTokenSerializer keyInfoSerializer) 
        { 
            CheckObjectValidity();
 
            if (writer == null)
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("writer"));

            if (samlSerializer == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("samlSerializer"));
 
#pragma warning suppress 56506 // samlSerializer.DictionaryManager is never null. 
            SamlDictionary dictionary = samlSerializer.DictionaryManager.SamlDictionary;
 
            writer.WriteStartElement(dictionary.PreferredPrefix.Value, dictionary.Attribute, dictionary.Namespace);

            writer.WriteStartAttribute(dictionary.AttributeName, null);
            writer.WriteString(this.name); 
            writer.WriteEndAttribute();
            writer.WriteStartAttribute(dictionary.AttributeNamespace, null); 
            writer.WriteString(this.nameSpace); 
            writer.WriteEndAttribute();
 
            for (int i = 0; i < this.attributeValues.Count; i++)
            {
                if (this.attributeValues[i] == null)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeValueCannotBeNull)); 

                writer.WriteElementString(dictionary.PreferredPrefix.Value, dictionary.AttributeValue, dictionary.Namespace, this.attributeValues[i]); 
            } 

            writer.WriteEndElement(); 
        }

    }
 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.IdentityModel.Tokens 
{
    using System.Collections; 
    using System.Collections.Generic; 
    using System.Collections.ObjectModel;
    using System.IdentityModel.Claims; 
    using System.IdentityModel.Selectors;
    using System.Globalization;
    using System.Xml;
    using System.Xml.Serialization; 

    public class SamlAttribute 
    { 
        string name;
        string nameSpace; 
        readonly ImmutableCollection attributeValues = new ImmutableCollection();

        List claims;
        string claimType; 
        bool isReadOnly = false;
 
        public SamlAttribute(string attributeNamespace, string attributeName, IEnumerable attributeValues) 
        {
            if (String.IsNullOrEmpty(attributeName)) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeNameAttributeRequired));

            if (String.IsNullOrEmpty(attributeNamespace))
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeNamespaceAttributeRequired)); 

            if (attributeValues == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("attributeValues"); 

            this.name = attributeName; 
            this.nameSpace = attributeNamespace;
            this.claimType = String.IsNullOrEmpty(this.nameSpace) ? this.name : this.nameSpace + "/" + this.name;

            foreach (string value in attributeValues) 
            {
                if (value == null) 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeValueCannotBeNull)); 

                this.attributeValues.Add(value); 
            }

            if (this.attributeValues.Count == 0)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeShouldHaveOneValue)); 
        }
 
        public SamlAttribute() 
        {
        } 

        public SamlAttribute(Claim claim)
        {
            if (claim == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("claim");
 
            if (!(claim.Resource is String)) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SamlAttributeClaimResourceShouldBeAString));
 
            if (claim.Right != Rights.PossessProperty)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SamlAttributeClaimRightShouldBePossessProperty));

#pragma warning suppress 56506 // claim.CalimType can never be null. 
            int lastSlashIndex = claim.ClaimType.LastIndexOf('/');
            if ((lastSlashIndex == -1) || (lastSlashIndex == 0) || (lastSlashIndex == claim.ClaimType.Length - 1)) 
            { 
                this.nameSpace = String.Empty;
                this.name = claim.ClaimType; 
            }
            else
            {
                this.nameSpace = claim.ClaimType.Substring(0, lastSlashIndex); 
                this.name = claim.ClaimType.Substring(lastSlashIndex + 1, claim.ClaimType.Length - (lastSlashIndex + 1));
            } 
            this.claimType = claim.ClaimType; 
            this.attributeValues.Add(claim.Resource as string);
        } 

        public string Name
        {
            get {return this.name; } 
            set
            { 
                if (isReadOnly) 
                    throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
 
                if (String.IsNullOrEmpty(value))
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeNameAttributeRequired));

                this.name = value; 
            }
        } 
 
        public string Namespace
        { 
            get {return this.nameSpace; }
            set
            {
                if (isReadOnly) 
                    throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
 
                if (String.IsNullOrEmpty(value)) 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeNamespaceAttributeRequired));
 
                this.nameSpace = value;
            }
        }
 
        public IList AttributeValues
        { 
            get {return this.attributeValues; } 
        }
 
        public bool IsReadOnly
        {
            get { return this.isReadOnly; }
        } 

        public void MakeReadOnly() 
        { 
            if (!this.isReadOnly)
            { 
                this.attributeValues.MakeReadOnly();

                this.isReadOnly = true;
            } 
        }
 
        public virtual ReadOnlyCollection ExtractClaims() 
        {
            if (this.claims == null) 
            {
                List tempClaims = new List(this.attributeValues.Count);

                for (int i = 0; i < this.attributeValues.Count; i++) 
                {
                    if (this.attributeValues[i] == null) 
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeValueCannotBeNull)); 

                    tempClaims.Add(new Claim(this.claimType, this.attributeValues[i], Rights.PossessProperty)); 
                }
                this.claims = tempClaims;
            }
 
            return this.claims.AsReadOnly();
        } 
 
        void CheckObjectValidity()
        { 
            if (String.IsNullOrEmpty(this.name))
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeNameAttributeRequired)));

            if (String.IsNullOrEmpty(this.nameSpace)) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeNamespaceAttributeRequired)));
 
            if (this.attributeValues.Count == 0) 
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeShouldHaveOneValue)));
        } 

        public virtual void ReadXml(XmlDictionaryReader reader, SamlSerializer samlSerializer, SecurityTokenSerializer keyInfoSerializer, SecurityTokenResolver outOfBandTokenResolver)
        {
            if (reader == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("reader"));
 
            if (samlSerializer == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("samlSerializer"));
 
#pragma warning suppress 56506 // samlSerializer.DictionaryManager is never null.
            SamlDictionary dictionary = samlSerializer.DictionaryManager.SamlDictionary;

            this.name = reader.GetAttribute(dictionary.AttributeName, null); 
            if (String.IsNullOrEmpty(this.name))
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeMissingNameAttributeOnRead))); 
 
            this.nameSpace = reader.GetAttribute(dictionary.AttributeNamespace, null);
            if (String.IsNullOrEmpty(this.nameSpace)) 
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeMissingNamespaceAttributeOnRead)));

            this.claimType = String.IsNullOrEmpty(this.nameSpace) ? this.name : this.nameSpace + "/" + this.name;
 
            reader.MoveToContent();
            reader.Read(); 
            while (reader.IsStartElement(dictionary.AttributeValue, dictionary.Namespace)) 
            {
                // We will load all Attributes as a string value by default. 
                string attrValue = reader.ReadString();
                this.attributeValues.Add(attrValue);

                reader.MoveToContent(); 
                reader.ReadEndElement();
            } 
 
            if (this.attributeValues.Count == 0)
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.SAMLAttributeShouldHaveOneValue))); 

            reader.MoveToContent();
            reader.ReadEndElement();
        } 

        public virtual void WriteXml(XmlDictionaryWriter writer, SamlSerializer samlSerializer, SecurityTokenSerializer keyInfoSerializer) 
        { 
            CheckObjectValidity();
 
            if (writer == null)
                throw  DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("writer"));

            if (samlSerializer == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("samlSerializer"));
 
#pragma warning suppress 56506 // samlSerializer.DictionaryManager is never null. 
            SamlDictionary dictionary = samlSerializer.DictionaryManager.SamlDictionary;
 
            writer.WriteStartElement(dictionary.PreferredPrefix.Value, dictionary.Attribute, dictionary.Namespace);

            writer.WriteStartAttribute(dictionary.AttributeName, null);
            writer.WriteString(this.name); 
            writer.WriteEndAttribute();
            writer.WriteStartAttribute(dictionary.AttributeNamespace, null); 
            writer.WriteString(this.nameSpace); 
            writer.WriteEndAttribute();
 
            for (int i = 0; i < this.attributeValues.Count; i++)
            {
                if (this.attributeValues[i] == null)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SAMLAttributeValueCannotBeNull)); 

                writer.WriteElementString(dictionary.PreferredPrefix.Value, dictionary.AttributeValue, dictionary.Namespace, this.attributeValues[i]); 
            } 

            writer.WriteEndElement(); 
        }

    }
 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK