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 144 145 146 147 148 149
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.IdentityModel.Tokens
{
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Globalization;
public class SecurityKeyIdentifier : IEnumerable<SecurityKeyIdentifierClause>
{
const int InitialSize = 2;
readonly List<SecurityKeyIdentifierClause> clauses;
bool isReadOnly;
public SecurityKeyIdentifier()
{
this.clauses = new List<SecurityKeyIdentifierClause>(InitialSize);
}
public SecurityKeyIdentifier(params SecurityKeyIdentifierClause[] clauses)
{
if (clauses == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("clauses");
}
this.clauses = new List<SecurityKeyIdentifierClause>(clauses.Length);
for (int i = 0; i < clauses.Length; i++)
{
Add(clauses[i]);
}
}
public SecurityKeyIdentifierClause this[int index]
{
get { return this.clauses[index]; }
}
public bool CanCreateKey
{
get
{
for (int i = 0; i < this.Count; i++)
{
if (this[i].CanCreateKey)
{
return true;
}
}
return false;
}
}
public int Count
{
get { return this.clauses.Count; }
}
public bool IsReadOnly
{
get { return this.isReadOnly; }
}
public void Add(SecurityKeyIdentifierClause clause)
{
if (this.isReadOnly)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
}
if (clause == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("clause"));
}
this.clauses.Add(clause);
}
public SecurityKey CreateKey()
{
for (int i = 0; i < this.Count; i++)
{
if (this[i].CanCreateKey)
{
return this[i].CreateKey();
}
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.KeyIdentifierCannotCreateKey)));
}
public TClause Find<TClause>() where TClause : SecurityKeyIdentifierClause
{
TClause clause;
if (!TryFind<TClause>(out clause))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.NoKeyIdentifierClauseFound, typeof(TClause)), "TClause"));
}
return clause;
}
public IEnumerator<SecurityKeyIdentifierClause> GetEnumerator()
{
return this.clauses.GetEnumerator();
}
public void MakeReadOnly()
{
this.isReadOnly = true;
}
public override string ToString()
{
using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
{
writer.WriteLine("SecurityKeyIdentifier");
writer.WriteLine(" (");
writer.WriteLine(" IsReadOnly = {0},", this.IsReadOnly);
writer.WriteLine(" Count = {0}{1}", this.Count, this.Count > 0 ? "," : "");
for (int i = 0; i < this.Count; i++)
{
writer.WriteLine(" Clause[{0}] = {1}{2}", i, this[i], i < this.Count - 1 ? "," : "");
}
writer.WriteLine(" )");
return writer.ToString();
}
}
public bool TryFind<TClause>(out TClause clause) where TClause : SecurityKeyIdentifierClause
{
for (int i = 0; i < this.clauses.Count; i++)
{
TClause c = this.clauses[i] as TClause;
if (c != null)
{
clause = c;
return true;
}
}
clause = null;
return false;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}
|