1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.Collections.ObjectModel;
namespace System.IdentityModel.Tokens
{
/// <summary>
/// This class defines a TokenResolver that can wrap multiple Token Resolvers
/// and resolve tokens across all the wrapped token resolvers.
/// </summary>
public class AggregateTokenResolver : SecurityTokenResolver
{
List<SecurityTokenResolver> _tokenResolvers = new List<SecurityTokenResolver>();
/// <summary>
/// Initializes an instance of <see cref="AggregateTokenResolver"/>
/// </summary>
/// <param name="tokenResolvers">IEnumerable list of TokenResolvers to be wrapped.</param>
/// <exception cref="ArgumentNullException">The input argument 'tokenResolvers' is null.</exception>
/// <exception cref="ArgumentException">The input 'tokenResolver' list does not contain a valid
/// SecurityTokenResolver. At least one SecurityTokenResolver should be specified.</exception>
public AggregateTokenResolver( IEnumerable<SecurityTokenResolver> tokenResolvers )
{
if ( tokenResolvers == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "tokenResolvers" );
}
AddNonEmptyResolvers( tokenResolvers );
}
/// <summary>
/// Gets a read-only collection of TokenResolvers.
/// </summary>
public ReadOnlyCollection<SecurityTokenResolver> TokenResolvers
{
get
{
return _tokenResolvers.AsReadOnly();
}
}
/// <summary>
/// Override of the base class. Resolves the given SecurityKeyIdentifierClause to a
/// SecurityKey.
/// </summary>
/// <param name="keyIdentifierClause">The Clause to be resolved.</param>
/// <param name="key">The resolved SecurityKey</param>
/// <returns>True if successfully resolved.</returns>
/// <exception cref="ArgumentNullException">Input argument 'keyIdentifierClause' is null.</exception>
protected override bool TryResolveSecurityKeyCore( SecurityKeyIdentifierClause keyIdentifierClause, out SecurityKey key )
{
if ( keyIdentifierClause == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "keyIdentifierClause" );
}
key = null;
foreach ( SecurityTokenResolver tokenResolver in _tokenResolvers )
{
if ( tokenResolver.TryResolveSecurityKey( keyIdentifierClause, out key ) )
{
return true;
}
}
return false;
}
/// <summary>
/// Override of the base class. Resolves the given SecurityKeyIdentifier to a
/// SecurityToken.
/// </summary>
/// <param name="keyIdentifier">The KeyIdentifier to be resolved.</param>
/// <param name="token">The resolved SecurityToken</param>
/// <returns>True if successfully resolved.</returns>
/// <exception cref="ArgumentNullException">Input argument 'keyIdentifier' is null.</exception>
protected override bool TryResolveTokenCore( SecurityKeyIdentifier keyIdentifier, out SecurityToken token )
{
if ( keyIdentifier == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "keyIdentifer" );
}
token = null;
foreach ( SecurityTokenResolver tokenResolver in _tokenResolvers )
{
if ( tokenResolver.TryResolveToken( keyIdentifier, out token ) )
{
return true;
}
}
return false;
}
/// <summary>
/// Override of the base class. Resolves the given SecurityKeyIdentifierClause to a
/// SecurityToken.
/// </summary>
/// <param name="keyIdentifierClause">The KeyIdentifier to be resolved.</param>
/// <param name="token">The resolved SecurityToken</param>
/// <returns>True if successfully resolved.</returns>
/// <exception cref="ArgumentNullException">Input argument 'keyIdentifierClause' is null.</exception>
protected override bool TryResolveTokenCore( SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken token )
{
if ( keyIdentifierClause == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "keyIdentifierClause" );
}
token = null;
foreach ( SecurityTokenResolver tokenResolver in _tokenResolvers )
{
if ( tokenResolver.TryResolveToken( keyIdentifierClause, out token ) )
{
return true;
}
}
return false;
}
private void AddNonEmptyResolvers( IEnumerable<SecurityTokenResolver> resolvers )
{
foreach ( SecurityTokenResolver resolver in resolvers )
{
if ( resolver != null && resolver != EmptySecurityTokenResolver.Instance )
{
_tokenResolvers.Add( resolver );
}
}
}
}
}
|