< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.SecurityTokenHandlerCollection
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SecurityTokenHandlerCollection.cs
Line coverage
33%
Covered lines: 67
Uncovered lines: 133
Coverable lines: 200
Total lines: 672
Line coverage: 33.5%
Branch coverage
28%
Covered branches: 39
Total branches: 138
Branch coverage: 28.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SecurityTokenHandlerCollection.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Collections.ObjectModel;
 7using System.Security.Claims;
 8using System.Xml;
 9using CoreWCF.IdentityModel.Selectors;
 10
 11namespace CoreWCF.IdentityModel.Tokens
 12{
 13    /// <summary>
 14    /// Defines a collection of SecurityTokenHandlers.
 15    /// </summary>
 16    public class SecurityTokenHandlerCollection : Collection<SecurityTokenHandler>
 17    {
 118        internal static int s_defaultHandlerCollectionCount = 8;
 119        internal static SecurityTokenHandlerCollection s_defaultSecurityTokenHandlerCollection = null; //TODO if better 
 220        private readonly Dictionary<string, SecurityTokenHandler> _handlersByIdentifier = new Dictionary<string, Securit
 221        private readonly Dictionary<Type, SecurityTokenHandler> _handlersByType = new Dictionary<Type, SecurityTokenHand
 22        private readonly KeyInfoSerializer _keyInfoSerializer;
 23
 24        /// <summary>
 25        /// Creates an instance of <see cref="SecurityTokenHandlerCollection"/>.
 26        /// Creates an empty set.
 27        /// </summary>
 28        public SecurityTokenHandlerCollection()
 029            : this(new SecurityTokenHandlerConfiguration())
 30        {
 031        }
 32
 33        /// <summary>
 34        /// Creates an instance of <see cref="SecurityTokenHandlerCollection"/>.
 35        /// Creates an empty set.
 36        /// </summary>
 37        /// <param name="configuration">The configuration to associate with the collection.</param>
 238        public SecurityTokenHandlerCollection(SecurityTokenHandlerConfiguration configuration)
 39        {
 240            Configuration = configuration ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(con
 241            _keyInfoSerializer = new KeyInfoSerializer(true);
 242        }
 43
 44        /// <summary>
 45        /// Creates an instance of <see cref="SecurityTokenHandlerCollection"/>
 46        /// </summary>
 47        /// <param name="handlers">List of SecurityTokenHandlers to initialize from.</param>
 48        /// <remarks>
 49        /// Do not use this constructor to attempt to clone an instance of a SecurityTokenHandlerCollection,
 50        /// use the Clone method instead.
 51        /// </remarks>
 52        public SecurityTokenHandlerCollection(IEnumerable<SecurityTokenHandler> handlers)
 053            : this(handlers, new SecurityTokenHandlerConfiguration())
 54        {
 055        }
 56
 57        /// <summary>
 58        /// Creates an instance of <see cref="SecurityTokenHandlerCollection"/>
 59        /// </summary>
 60        /// <param name="handlers">List of SecurityTokenHandlers to initialize from.</param>
 61        /// <param name="configuration">The <see cref="SecurityTokenHandlerConfiguration"/> in effect.</param>
 62        /// <remarks>
 63        /// Do not use this constructor to attempt to clone an instance of a SecurityTokenHandlerCollection,
 64        /// use the Clone method instead.
 65        /// </remarks>
 66        public SecurityTokenHandlerCollection(IEnumerable<SecurityTokenHandler> handlers, SecurityTokenHandlerConfigurat
 067            : this(configuration)
 68        {
 069            if (handlers == null)
 70            {
 071                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(handlers));
 72            }
 73
 074            foreach (SecurityTokenHandler handler in handlers)
 75            {
 076                Add(handler);
 77            }
 078        }
 79
 80        /// <summary>
 81        /// Gets an instance of <see cref="SecurityTokenHandlerConfiguration"/>
 82        /// </summary>
 2283        public SecurityTokenHandlerConfiguration Configuration { get; }
 84
 85        /// <summary>
 86        /// Gets the List of System.Type of the Token Handlers in this collection.
 87        /// </summary>
 88        public IEnumerable<Type> TokenTypes
 89        {
 090            get { return _handlersByType.Keys; }
 91        }
 92
 93        /// <summary>
 94        /// Gets the list of Token type Identifier of the Token Handlers.
 95        /// </summary>
 96        public IEnumerable<string> TokenTypeIdentifiers
 97        {
 98            get
 99            {
 0100                return _handlersByIdentifier.Keys;
 101            }
 102        }
 103
 104        /// <summary>
 105        /// Gets a Token Handler by its Token Type Identifier.
 106        /// </summary>
 107        /// <param name="tokenTypeIdentifier">The Token Type Identfier string to search for.</param>
 108        /// <returns>Instance of a SecurityTokenHandler.</returns>
 109        public SecurityTokenHandler this[string tokenTypeIdentifier]
 110        {
 111            get
 112            {
 4113                if (string.IsNullOrEmpty(tokenTypeIdentifier))
 114                {
 0115                    return null;
 116                }
 117
 4118                _handlersByIdentifier.TryGetValue(tokenTypeIdentifier, out SecurityTokenHandler handler);
 4119                return handler;
 120            }
 121        }
 122
 123        /// <summary>
 124        /// Gets a Token Handler that can handle a given SecurityToken.
 125        /// </summary>
 126        /// <param name=nameof(token)>SecurityToken for which a Token Handler is requested.</param>
 127        /// <returns>Instance of SecurityTokenHandler.</returns>
 128        public SecurityTokenHandler this[SecurityToken token]
 129        {
 130            get
 131            {
 0132                if (null == token)
 133                {
 0134                    return null;
 135                }
 136
 0137                return this[token.GetType()];
 138            }
 139        }
 140
 141        /// <summary>
 142        /// Gets a Token Handler based on the System.Type of the token.
 143        /// </summary>
 144        /// <param name="tokenType">System.Type of the Token that needs to be handled.</param>
 145        /// <returns>Instance of SecurityTokenHandler.</returns>
 146        public SecurityTokenHandler this[Type tokenType]
 147        {
 148            get
 149            {
 10150                SecurityTokenHandler handler = null;
 10151                if (tokenType != null)
 152                {
 10153                    _handlersByType.TryGetValue(tokenType, out handler);
 154                }
 155
 10156                return handler;
 157            }
 158        }
 159
 160
 161        //TODO - Check which one is important apart from Saml and port accordingly..
 162
 163        /// <summary>
 164        /// Creates a system default collection of basic SecurityTokenHandlers, each of which has the system default con
 165        /// The SecurityTokenHandlers in this collection must be configured with service specific data before they can b
 166        /// </summary>
 167        /// <param name="configuration">The configuration to associate with the collection.</param>
 168        /// <returns>A SecurityTokenHandlerCollection with default basic SecurityTokenHandlers.</returns>
 169        internal static SecurityTokenHandlerCollection CreateDefaultSecurityTokenHandlerCollection(SecurityTokenHandlerC
 170            IEnumerable<SecurityTokenHandler> securityTokenHandlers)
 171        {
 2172            SecurityTokenHandlerCollection collection = new SecurityTokenHandlerCollection(configuration);
 44173            foreach(SecurityTokenHandler handler in securityTokenHandlers)
 174            {
 20175                collection.AddOrReplace(handler);
 176            }
 2177            s_defaultHandlerCollectionCount = collection.Count;
 2178            s_defaultSecurityTokenHandlerCollection = collection;
 2179            return collection;
 180        }
 181
 182        internal SecurityTokenSerializer KeyInfoSerializer
 183        {
 0184            get { return _keyInfoSerializer; }
 185        }
 186
 187        /// <summary>
 188        /// Adds a new handler or replace the existing handler with the same token type identifier
 189        /// with with the new handler.
 190        /// </summary>
 191        /// <param name="handler">The SecurityTokenHandler to add or replace</param>
 192        /// <exception cref="ArgumentNullException">When the input parameter is null.</exception>
 193        public void AddOrReplace(SecurityTokenHandler handler)
 194        {
 20195            if (handler == null)
 196            {
 0197                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(handler));
 198            }
 199
 200            // Remove the old one if it exists
 20201            Type tokenType = handler.TokenType;
 20202            if (tokenType != null && _handlersByType.ContainsKey(tokenType))
 203            {
 10204                Remove(this[tokenType]);
 205            }
 206            else
 207            {
 10208                string[] identifiers = handler.GetTokenTypeIdentifiers();
 10209                if (identifiers != null)
 210                {
 56211                    foreach (string tokenTypeIdentifier in identifiers)
 212                    {
 18213                        if (tokenTypeIdentifier != null && _handlersByIdentifier.ContainsKey(tokenTypeIdentifier))
 214                        {
 0215                            Remove(this[tokenTypeIdentifier]);
 0216                            break;
 217                        }
 218                    }
 219                }
 220            }
 221
 222            // Add the new handler in the collection
 20223            Add(handler);
 20224        }
 225
 226        /// <summary>
 227        /// Checks if a token can be read using the SecurityTokenHandlers.
 228        /// </summary>
 229        /// <param name=nameof(reader)>XmlReader pointing at token.</param>
 230        /// <returns>True if the token can be read, false otherwise</returns>
 231        /// <exception cref="ArgumentNullException">The input argument 'reader' is null.</exception>
 232        public bool CanReadToken(XmlReader reader)
 233        {
 0234            if (reader == null)
 235            {
 0236                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 237            }
 238
 0239            foreach (SecurityTokenHandler handler in this)
 240            {
 0241                if (null != handler && handler.CanReadToken(reader))
 242                {
 0243                    return true;
 244                }
 245            }
 246
 0247            return false;
 0248        }
 249
 250        /// <summary>
 251        /// Checks if a token can be read using the SecurityTokenHandlers.
 252        /// </summary>
 253        /// <param name="tokenString">The token string thats needs to be read.</param>
 254        /// <returns>True if the token can be read, false otherwise</returns>
 255        /// <exception cref="ArgumentException">The input argument 'tokenString' is null or empty.</exception>
 256        public bool CanReadToken(string tokenString)
 257        {
 0258            if (string.IsNullOrEmpty(tokenString))
 259            {
 0260                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenString));
 261            }
 262
 0263            foreach (SecurityTokenHandler handler in this)
 264            {
 0265                if (null != handler && handler.CanReadToken(tokenString))
 266                {
 0267                    return true;
 268                }
 269            }
 270
 0271            return false;
 0272        }
 273
 274        /// <summary>
 275        /// Checks if a token can be written using the SecurityTokenHandlers.
 276        /// </summary>
 277        /// <param name=token>SecurityToken to be written out.</param>
 278        /// <returns>True if the token can be written, false otherwise</returns>
 279        /// <exception cref="ArgumentNullException">The input argument 'token' is null.</exception>
 280        public bool CanWriteToken(SecurityToken token)
 281        {
 0282            if (token == null)
 283            {
 0284                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token));
 285            }
 286
 0287            SecurityTokenHandler handler = this[token];
 0288            if (null != handler && handler.CanWriteToken)
 289            {
 0290                return true;
 291            }
 292
 0293            return false;
 294        }
 295
 296        /// <summary>
 297        /// Validates a given token using the SecurityTokenHandlers.
 298        /// </summary>
 299        /// <param name=nameof(token)>The SecurityToken to be validated.</param>
 300        /// <returns>A <see cref="ReadOnlyCollection{T}"/> of <see cref="ClaimsIdentity"/> representing the identities c
 301        /// <exception cref="ArgumentNullException">The input argument 'token' is null.</exception>
 302        /// <exception cref="InvalidOperationException">A <see cref="SecurityTokenHandler"/> cannot be found that can va
 303        public ReadOnlyCollection<ClaimsIdentity> ValidateToken(SecurityToken token)
 304        {
 0305            if (token == null)
 306            {
 0307                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token));
 308            }
 309
 0310            SecurityTokenHandler handler = this[token];
 0311            if (null == handler || !handler.CanValidateToken)
 312            {
 0313                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4
 314            }
 315
 0316            return handler.ValidateToken(token);
 317        }
 318
 319        /// <summary>
 320        /// Reads a token using the TokenHandlers.
 321        /// </summary>
 322        /// <param name=nameof(reader)>XmlReader pointing at token.</param>
 323        /// <returns>Instance of <see cref="SecurityToken"/></returns>
 324        /// <exception cref="ArgumentNullException">The input argument 'reader' is null.</exception>
 325        public SecurityToken ReadToken(XmlReader reader)
 326        {
 0327            if (reader == null)
 328            {
 0329                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 330            }
 331
 0332            foreach (SecurityTokenHandler handler in this)
 333            {
 0334                if (null != handler && handler.CanReadToken(reader))
 335                {
 0336                    return handler.ReadToken(reader);
 337                }
 338            }
 339
 0340            return null;
 0341        }
 342
 343        /// <summary>
 344        /// Reads a token using the TokenHandlers.
 345        /// </summary>
 346        /// <param name="tokenString">The token string to be deserialized.</param>
 347        /// <returns>Instance of <see cref="SecurityToken"/></returns>
 348        /// <exception cref="ArgumentException">The input argument 'tokenString' is null or empty.</exception>
 349        public SecurityToken ReadToken(string tokenString)
 350        {
 0351            if (string.IsNullOrEmpty(tokenString))
 352            {
 0353                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenString));
 354            }
 355
 0356            foreach (SecurityTokenHandler handler in this)
 357            {
 0358                if (null != handler && handler.CanReadToken(tokenString))
 359                {
 0360                    return handler.ReadToken(tokenString);
 361                }
 362            }
 363
 0364            return null;
 0365        }
 366
 367        /// <summary>
 368        /// Writes a given SecurityToken to the XmlWriter.
 369        /// </summary>
 370        /// <param name="writer">XmlWriter to write the token into.</param>
 371        /// <param name=nameof(token)>SecurityToken to be written out.</param>
 372        /// <exception cref="ArgumentNullException">The input argument 'writer' or 'token' is null.</exception>
 373        public void WriteToken(XmlWriter writer, SecurityToken token)
 374        {
 0375            if (writer == null)
 376            {
 0377                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 378            }
 379
 0380            if (token == null)
 381            {
 0382                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token));
 383            }
 384
 0385            SecurityTokenHandler handler = this[token];
 0386            if (null == handler || !handler.CanWriteToken)
 387            {
 0388                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4
 389            }
 390
 0391            handler.WriteToken(writer, token);
 0392        }
 393
 394        /// <summary>
 395        /// Writes a given SecurityToken to a string.
 396        /// </summary>
 397        /// <param name=nameof(token)>SecurityToken to be written out.</param>
 398        /// <returns>The serialized token.</returns>
 399        /// <exception cref="ArgumentNullException">The input argument 'token' is null.</exception>
 400        public string WriteToken(SecurityToken token)
 401        {
 0402            if (token == null)
 403            {
 0404                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token));
 405            }
 406
 0407            SecurityTokenHandler handler = this[token];
 0408            if (null == handler || !handler.CanWriteToken)
 409            {
 0410                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4
 411            }
 412
 0413            return handler.WriteToken(token);
 414        }
 415
 416        /// <summary>
 417        /// Override. (Inherited from Collection&lt;T>"/>
 418        /// </summary>
 419        protected override void ClearItems()
 420        {
 0421            base.ClearItems();
 0422            _handlersByIdentifier.Clear();
 0423            _handlersByType.Clear();
 0424        }
 425
 426        /// <summary>
 427        /// Override. (Inherited from Collection&lt;T&gt;"/>
 428        /// </summary>
 429        /// <param name="index">The zero-based index at which item should be inserted.</param>
 430        /// <param name="item">The object to insert. The value can be null for reference types.</param>
 431        protected override void InsertItem(int index, SecurityTokenHandler item)
 432        {
 20433            base.InsertItem(index, item);
 434
 435            try
 436            {
 20437                AddToDictionaries(item);
 20438            }
 0439            catch
 440            {
 0441                base.RemoveItem(index);
 0442                throw;
 443            }
 20444        }
 445
 446        /// <summary>
 447        /// Override. (Inherited from Collection&lt;T>"/>
 448        /// </summary>
 449        /// <param name="index">The zero-based index of the element to remove.</param>
 450        protected override void RemoveItem(int index)
 451        {
 10452            SecurityTokenHandler removedItem = Items[index];
 10453            base.RemoveItem(index);
 10454            RemoveFromDictionaries(removedItem);
 10455        }
 456
 457        /// <summary>
 458        /// Override. (Inherited from Collection&lt;T>"/>
 459        /// </summary>
 460        /// <param name="index">The zero-based index of the element to replace.</param>
 461        /// <param name="item">The new value for the element at the specified index. The value can be null for reference
 462        protected override void SetItem(int index, SecurityTokenHandler item)
 463        {
 0464            SecurityTokenHandler replaced = Items[index];
 0465            base.SetItem(index, item);
 466
 0467            RemoveFromDictionaries(replaced);
 468
 469            try
 470            {
 0471                AddToDictionaries(item);
 0472            }
 0473            catch
 474            {
 0475                base.SetItem(index, replaced);
 0476                AddToDictionaries(replaced);
 0477                throw;
 478            }
 0479        }
 480
 481        public bool CanReadKeyIdentifierClause(XmlReader reader)
 482        {
 0483            if (reader == null)
 484            {
 0485                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 486            }
 487
 0488            return CanReadKeyIdentifierClauseCore(reader);
 489        }
 490
 491        /// <summary>
 492        /// Checks if the wrapped SecurityTokenHandler or the base WSSecurityTokenSerializer can read the
 493        /// SecurityKeyIdentifierClause.
 494        /// </summary>
 495        /// <param name=nameof(reader)>Reader to a SecurityKeyIdentifierClause.</param>
 496        /// <returns>'True' if the SecurityKeyIdentifierCause can be read.</returns>
 497        /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception>
 498        protected virtual bool CanReadKeyIdentifierClauseCore(XmlReader reader)
 499        {
 0500            if (reader == null)
 501            {
 0502                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 503            }
 504
 0505            foreach (SecurityTokenHandler securityTokenHandler in this)
 506            {
 0507                if (securityTokenHandler.CanReadKeyIdentifierClause(reader))
 508                {
 0509                    return true;
 510                }
 511            }
 512
 0513            return false;
 0514        }
 515
 516        public SecurityKeyIdentifierClause ReadKeyIdentifierClause(XmlReader reader)
 517        {
 0518            if (reader == null)
 519            {
 0520                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 521            }
 522
 0523            return ReadKeyIdentifierClauseCore(reader);
 524        }
 525
 526        /// <summary>
 527        /// Deserializes a SecurityKeyIdentifierClause from the given reader.
 528        /// </summary>
 529        /// <param name=nameof(reader)>XmlReader to a SecurityKeyIdentifierClause.</param>
 530        /// <returns>The deserialized SecurityKeyIdentifierClause.</returns>
 531        /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception>
 532        protected virtual SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlReader reader)
 533        {
 0534            if (reader == null)
 535            {
 0536                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 537            }
 538
 0539            foreach (SecurityTokenHandler securityTokenHandler in this)
 540            {
 0541                if (securityTokenHandler.CanReadKeyIdentifierClause(reader))
 542                {
 0543                    return securityTokenHandler.ReadKeyIdentifierClause(reader);
 544                }
 545            }
 546
 0547            return _keyInfoSerializer.ReadKeyIdentifierClause(reader);
 0548        }
 549
 550        public void WriteKeyIdentifierClause(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifierClause)
 551        {
 0552            if (writer == null)
 553            {
 0554                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 555            }
 556
 0557            if (keyIdentifierClause == null)
 558            {
 0559                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 560            }
 561
 0562            WriteKeyIdentifierClauseCore(writer, keyIdentifierClause);
 0563        }
 564
 565        /// <summary>
 566        /// Serializes the given SecurityKeyIdentifierClause in a XmlWriter.
 567        /// </summary>
 568        /// <param name="writer">XmlWriter to write into.</param>
 569        /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to be written.</param>
 570        /// <exception cref="ArgumentNullException">The input parameter 'writer' or 'keyIdentifierClause' is null.</exce
 571        protected virtual void WriteKeyIdentifierClauseCore(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifierC
 572        {
 0573            if (writer == null)
 574            {
 0575                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 576            }
 577
 0578            if (keyIdentifierClause == null)
 579            {
 0580                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 581            }
 582
 0583            foreach (SecurityTokenHandler securityTokenHandler in this)
 584            {
 0585                if (securityTokenHandler.CanWriteKeyIdentifierClause(keyIdentifierClause))
 586                {
 0587                    securityTokenHandler.WriteKeyIdentifierClause(writer, keyIdentifierClause);
 0588                    return;
 589                }
 590            }
 591
 0592            _keyInfoSerializer.WriteKeyIdentifierClause(writer, keyIdentifierClause);
 0593        }
 594
 595        private void AddToDictionaries(SecurityTokenHandler handler)
 596        {
 20597            if (handler == null)
 598            {
 0599                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("handler");
 600            }
 601
 20602            bool firstSucceeded = false;
 603
 20604            string[] identifiers = handler.GetTokenTypeIdentifiers();
 20605            if (identifiers != null)
 606            {
 112607                foreach (string typeId in identifiers)
 608                {
 36609                    if (typeId != null)
 610                    {
 32611                        _handlersByIdentifier.Add(typeId, handler);
 32612                        firstSucceeded = true;
 613                    }
 614                }
 615            }
 616
 20617            Type type = handler.TokenType;
 20618            if (handler.TokenType != null)
 619            {
 620                try
 621                {
 20622                    _handlersByType.Add(type, handler);
 20623                }
 0624                catch
 625                {
 0626                    if (firstSucceeded)
 627                    {
 0628                        RemoveFromDictionaries(handler);
 629                    }
 630
 0631                    throw;
 632                }
 633            }
 634
 635            // Ensure that the handler knows which collection it is in.
 20636            handler.ContainingCollection = this;
 637
 638            // Propagate this collection's STH configuration to the handler
 639            // if the handler's configuration is unset.
 20640            if (handler.Configuration == null)
 641            {
 20642                handler.Configuration = Configuration;
 643            }
 20644        }
 645
 646        private void RemoveFromDictionaries(SecurityTokenHandler handler)
 647        {
 10648            string[] identifiers = handler.GetTokenTypeIdentifiers();
 10649            if (identifiers != null)
 650            {
 56651                foreach (string typeId in identifiers)
 652                {
 18653                    if (typeId != null)
 654                    {
 16655                        _handlersByIdentifier.Remove(typeId);
 656                    }
 657                }
 658            }
 659
 10660            Type type = handler.TokenType;
 10661            if (type != null && _handlersByType.ContainsKey(type))
 662            {
 10663                _handlersByType.Remove(type);
 664            }
 665
 666            // Ensure that the handler knows that it is no longer
 667            // in a collection.
 10668            handler.ContainingCollection = null;
 10669            handler.Configuration = null;
 10670        }
 671    }
 672}

Methods/Properties

.cctor()
.ctor(CoreWCF.IdentityModel.Tokens.SecurityTokenHandlerConfiguration)
.ctor()
.ctor(System.Collections.Generic.IEnumerable`1<CoreWCF.IdentityModel.Tokens.SecurityTokenHandler>)
.ctor(System.Collections.Generic.IEnumerable`1<CoreWCF.IdentityModel.Tokens.SecurityTokenHandler>,CoreWCF.IdentityModel.Tokens.SecurityTokenHandlerConfiguration)
Configuration()
TokenTypes()
TokenTypeIdentifiers()
Item(System.String)
Item(CoreWCF.IdentityModel.Tokens.SecurityToken)
Item(System.Type)
CreateDefaultSecurityTokenHandlerCollection(CoreWCF.IdentityModel.Tokens.SecurityTokenHandlerConfiguration,System.Collections.Generic.IEnumerable`1<CoreWCF.IdentityModel.Tokens.SecurityTokenHandler>)
KeyInfoSerializer()
AddOrReplace(CoreWCF.IdentityModel.Tokens.SecurityTokenHandler)
CanReadToken(System.Xml.XmlReader)
CanReadToken(System.String)
CanWriteToken(CoreWCF.IdentityModel.Tokens.SecurityToken)
ValidateToken(CoreWCF.IdentityModel.Tokens.SecurityToken)
ReadToken(System.Xml.XmlReader)
ReadToken(System.String)
WriteToken(System.Xml.XmlWriter,CoreWCF.IdentityModel.Tokens.SecurityToken)
WriteToken(CoreWCF.IdentityModel.Tokens.SecurityToken)
ClearItems()
InsertItem(System.Int32,CoreWCF.IdentityModel.Tokens.SecurityTokenHandler)
RemoveItem(System.Int32)
SetItem(System.Int32,CoreWCF.IdentityModel.Tokens.SecurityTokenHandler)
CanReadKeyIdentifierClause(System.Xml.XmlReader)
CanReadKeyIdentifierClauseCore(System.Xml.XmlReader)
ReadKeyIdentifierClause(System.Xml.XmlReader)
ReadKeyIdentifierClauseCore(System.Xml.XmlReader)
WriteKeyIdentifierClause(System.Xml.XmlWriter,CoreWCF.IdentityModel.SecurityKeyIdentifierClause)
WriteKeyIdentifierClauseCore(System.Xml.XmlWriter,CoreWCF.IdentityModel.SecurityKeyIdentifierClause)
AddToDictionaries(CoreWCF.IdentityModel.Tokens.SecurityTokenHandler)
RemoveFromDictionaries(CoreWCF.IdentityModel.Tokens.SecurityTokenHandler)