| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Runtime.Serialization; |
| | | 9 | | using System.Runtime.Serialization.Formatters.Binary; |
| | | 10 | | using System.Security.Claims; |
| | | 11 | | using System.Security.Cryptography; |
| | | 12 | | using System.Security.Cryptography.X509Certificates; |
| | | 13 | | using System.Security.Principal; |
| | | 14 | | using System.Xml; |
| | | 15 | | using CoreWCF.IdentityModel.Claims; |
| | | 16 | | using CoreWCF.IdentityModel.Policy; |
| | | 17 | | using Claim = System.Security.Claims.Claim; |
| | | 18 | | using ClaimTypes = System.Security.Claims.ClaimTypes; |
| | | 19 | | using SysClaim = CoreWCF.IdentityModel.Claims.Claim; |
| | | 20 | | using SysClaimTypes = CoreWCF.IdentityModel.Claims.ClaimTypes; |
| | | 21 | | using SysUniqueId = System.Xml.UniqueId; |
| | | 22 | | |
| | | 23 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Defines a SessionSecurityToken that contains data associated with a session. |
| | | 27 | | /// </summary> |
| | | 28 | | [Serializable] |
| | | 29 | | public class SessionSecurityToken : SecurityToken, ISerializable |
| | | 30 | | { |
| | | 31 | | private const string SupportedVersion = "1"; |
| | | 32 | | private const string TokenKey = "SessionToken"; |
| | 0 | 33 | | private static readonly Dictionary<string, string> DomainNameMap = new Dictionary<string, string>(MaxDomainNameM |
| | | 34 | | private const int MaxDomainNameMapSize = 50; |
| | | 35 | | private readonly string _context; |
| | | 36 | | private bool _isPersistent; |
| | | 37 | | private readonly ClaimsPrincipal _claimsPrincipal; |
| | | 38 | | private readonly SctAuthorizationPolicy _sctAuthorizationPolicy; |
| | | 39 | | private readonly string _endpointId; |
| | | 40 | | private bool _isReferenceMode; |
| | | 41 | | private readonly bool _isSecurityContextSecurityTokenWrapper; |
| | | 42 | | private readonly string _id; |
| | | 43 | | private readonly SysUniqueId _contextId; |
| | | 44 | | private readonly SysUniqueId _keyGeneration; |
| | | 45 | | private readonly DateTime _keyEffectiveTime; |
| | | 46 | | private readonly DateTime _keyExpirationTime; |
| | | 47 | | private readonly Uri _secureConversationVersion; |
| | | 48 | | private readonly DateTime _validFrom; |
| | | 49 | | private readonly DateTime _validTo; |
| | | 50 | | private readonly ReadOnlyCollection<SecurityKey> _securityKeys; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Create session security token from a principal. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="claimsPrincipal">The <see cref="ClaimsPrincipal"/>.</param> |
| | | 56 | | /// <returns></returns> |
| | | 57 | | public SessionSecurityToken(ClaimsPrincipal claimsPrincipal) |
| | 0 | 58 | | : this(claimsPrincipal, null) |
| | 0 | 59 | | { } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Create session security token from a principal. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="claimsPrincipal">The <see cref="ClaimsPrincipal"/>.</param> |
| | | 65 | | /// <param name="lifetime">The Timespan the token is valid.</param> |
| | | 66 | | public SessionSecurityToken(ClaimsPrincipal claimsPrincipal, TimeSpan lifetime) |
| | 0 | 67 | | : this(claimsPrincipal, null, DateTime.UtcNow, DateTimeUtil.AddNonNegative(DateTime.UtcNow, lifetime)) |
| | | 68 | | { |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Create session security token from principal and bootstrap token. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="claimsPrincipal">The <see cref="ClaimsPrincipal"/> that generated from the bootstrap token.</pa |
| | | 75 | | /// <param name="context">Session specific context string</param> |
| | | 76 | | public SessionSecurityToken(ClaimsPrincipal claimsPrincipal, string context) |
| | 0 | 77 | | : this(claimsPrincipal, context, DateTime.UtcNow, DateTimeUtil.AddNonNegative(DateTime.UtcNow, SessionSecuri |
| | 0 | 78 | | { } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Create session security token from principal and bootstrap token. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="claimsPrincipal">The <see cref="ClaimsPrincipal"/> that generated from the bootstrap token.</pa |
| | | 84 | | /// <param name="context">Session specific context string</param> |
| | | 85 | | /// <param name="validFrom">DateTime specifying the time the token becomes valid.</param> |
| | | 86 | | /// <param name="validTo">DateTime specifying the time the token becomes invalid.</param> |
| | | 87 | | public SessionSecurityToken(ClaimsPrincipal claimsPrincipal, string context, DateTime? validFrom, DateTime? vali |
| | 0 | 88 | | : this(claimsPrincipal, new SysUniqueId(), context, string.Empty, validFrom, validTo, null) |
| | 0 | 89 | | { } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Create session security token from principal and bootstrap token. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="claimsPrincipal">The <see cref="ClaimsPrincipal"/> that generated from the bootstrap token.</pa |
| | | 95 | | /// <param name="context">Session specific context string</param> |
| | | 96 | | /// <param name="endpointId">The endpoint to which this token is bound. String.Empty would create a unscoped tok |
| | | 97 | | /// <param name="validFrom">DateTime specifying the time the token becomes valid.</param> |
| | | 98 | | /// <param name="validTo">DateTime specifying the time the token becomes invalid.</param> |
| | | 99 | | public SessionSecurityToken(ClaimsPrincipal claimsPrincipal, string context, string endpointId, DateTime? validF |
| | 0 | 100 | | : this(claimsPrincipal, new SysUniqueId(), context, endpointId, validFrom, validTo, null) |
| | 0 | 101 | | { } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Initializes a new instance of the <see cref="SessionSecurityToken"/> class. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="claimsPrincipal"><see cref="ClaimsPrincipal"/> associated with this session.</param> |
| | | 107 | | /// <param name="contextId">Optional context identifier associated with this token. If null a new identifier wi |
| | | 108 | | /// <param name="context">Optional context information associated with the session.</param> |
| | | 109 | | /// <param name="endpointId">The endpoint to which this token is bound. String.Empty would create a unscoped tok |
| | | 110 | | /// <param name="lifetime">The lifetime of the session token. ValidFrom will be set to DateTime.UtcNow, ValidTo |
| | | 111 | | /// <param name="key">Optional symmetric session key.</param> |
| | | 112 | | /// <exception cref="InvalidOperationException">The value of lifetime <= TimeSpan.Zero."</exception> |
| | | 113 | | public SessionSecurityToken(ClaimsPrincipal claimsPrincipal, |
| | | 114 | | SysUniqueId contextId, |
| | | 115 | | string context, |
| | | 116 | | string endpointId, |
| | | 117 | | TimeSpan lifetime, |
| | | 118 | | SymmetricSecurityKey key) |
| | 0 | 119 | | : this(claimsPrincipal, contextId, context, endpointId, DateTime.UtcNow, lifetime, key) |
| | | 120 | | { |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Initializes a new instance of the <see cref="SessionSecurityToken"/> class. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <param name="claimsPrincipal"><see cref="ClaimsPrincipal"/> associated with this session.</param> |
| | | 127 | | /// <param name="contextId">Optional context identifier associated with this token. If null a new identifier wi |
| | | 128 | | /// <param name="context">Optional context information associated with the session.</param> |
| | | 129 | | /// <param name="endpointId">The endpoint to which this token is bound. String.Empty would create a unscoped tok |
| | | 130 | | /// <param name="validFrom">DateTime specifying the time the token becomes valid.</param> |
| | | 131 | | /// <param name="lifetime">The lifetime of the session token. ValidTo will be set to ValidFrom + lifetime.</par |
| | | 132 | | /// <param name="key">Optional symmetric session key.</param> |
| | | 133 | | /// <exception cref="InvalidOperationException">The value of lifetime <= TimeSpan.Zero."</exception> |
| | | 134 | | public SessionSecurityToken(ClaimsPrincipal claimsPrincipal, |
| | | 135 | | SysUniqueId contextId, |
| | | 136 | | string context, |
| | | 137 | | string endpointId, |
| | | 138 | | DateTime validFrom, |
| | | 139 | | TimeSpan lifetime, |
| | | 140 | | SymmetricSecurityKey key) |
| | 0 | 141 | | : this(claimsPrincipal, contextId, context, endpointId, validFrom, DateTimeUtil.AddNonNegative(validFrom, li |
| | | 142 | | { |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Initializes a new instance of the <see cref="SessionSecurityToken"/> class. |
| | | 147 | | /// </summary> |
| | | 148 | | /// <param name="claimsPrincipal"><see cref="ClaimsPrincipal"/> associated with this session.</param> |
| | | 149 | | /// <param name="contextId">Context Identifier that identifies the session</param> |
| | | 150 | | /// <param name="context">Optional context information associated with the session.</param> |
| | | 151 | | /// <param name="endpointId">The endpoint to which this token is bound. String.Empty would create a unscoped tok |
| | | 152 | | /// <param name="validFrom">DateTime specifying the time the token becomes valid.</param> |
| | | 153 | | /// <param name="validTo">DateTime specifying the time the token becomes invalid.</param> |
| | | 154 | | /// <param name="key">Optional symmetric session key.</param> |
| | | 155 | | /// <exception cref="ArgumentNullException">The input parameter 'claimsPrincipal' is null.</exception> |
| | | 156 | | /// <exception cref="ArgumentNullException">The input parameter 'contextId' is null.</exception> |
| | | 157 | | /// <exception cref="ArgumentOutOfRangeException">validFrom is greater than or equal to validTo.</exception> |
| | | 158 | | /// <exception cref="ArgumentOutOfRangeException">validTo is less than current time.</exception> |
| | | 159 | | /// <remarks> |
| | | 160 | | /// If no key is supplied, a 128bit key is generated. KeyEffectiveTime is set to validFrom, KeyExpirationTime is |
| | | 161 | | /// A key generation identifier is created. |
| | | 162 | | /// </remarks> |
| | | 163 | | public SessionSecurityToken(ClaimsPrincipal claimsPrincipal, |
| | | 164 | | SysUniqueId contextId, |
| | | 165 | | string context, |
| | | 166 | | string endpointId, |
| | | 167 | | DateTime? validFrom, |
| | | 168 | | DateTime? validTo, |
| | | 169 | | SymmetricSecurityKey key) |
| | 0 | 170 | | : this(claimsPrincipal, contextId, SecurityUniqueId.Create().Value, context, key?.GetSymmetricKey(), endpoin |
| | | 171 | | { |
| | 0 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Core ctor with all parameters in their most primitive form. This constructor is used in deserialization and |
| | | 176 | | /// when generating a wrapper SessionSecurityToken from a SecurityContextSecurityToken. |
| | | 177 | | /// </summary> |
| | | 178 | | /// <param name="contextId">Context identifier</param> |
| | | 179 | | /// <param name="id">Token identifier</param> |
| | | 180 | | /// <param name="context">Session context data</param> |
| | | 181 | | /// <param name="endpointId">The endpoint to which this token is bound. String.Empty would create a unscoped tok |
| | | 182 | | /// <param name="key">Key material</param> |
| | | 183 | | /// <param name="validFrom">Start time</param> |
| | | 184 | | /// <param name="validTo">End time</param> |
| | | 185 | | /// <param name="keyGeneration">Key Generation identifier</param> |
| | | 186 | | /// <param name="keyEffectiveTime">Key start time</param> |
| | | 187 | | /// <param name="keyExpirationTime">Key end time</param> |
| | | 188 | | /// <param name="authorizationPolicies">Authorization policies</param> |
| | | 189 | | /// <param name="securityContextSecurityTokenWrapperSecureConversationVersion">The version of |
| | | 190 | | /// WS-SecureConversation used to generate this SCT. This should be null if the token is not an SCT wrapper.</p |
| | | 191 | | internal SessionSecurityToken(ClaimsPrincipal claimsPrincipal, |
| | | 192 | | SysUniqueId contextId, |
| | | 193 | | string id, |
| | | 194 | | string context, |
| | | 195 | | byte[] key, |
| | | 196 | | string endpointId, |
| | | 197 | | DateTime? validFrom, |
| | | 198 | | DateTime? validTo, |
| | | 199 | | SysUniqueId keyGeneration, |
| | | 200 | | DateTime? keyEffectiveTime, |
| | | 201 | | DateTime? keyExpirationTime, |
| | | 202 | | SctAuthorizationPolicy sctAuthorizationPolicy, |
| | | 203 | | Uri securityContextSecurityTokenWrapperSecureConversationVersion) |
| | 0 | 204 | | : base() |
| | | 205 | | { |
| | 0 | 206 | | if (claimsPrincipal == null || claimsPrincipal.Identities == null) |
| | | 207 | | { |
| | 0 | 208 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(claimsPrincipal)); |
| | | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | if (contextId == null) |
| | | 212 | | { |
| | 0 | 213 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contextId)); |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | // |
| | | 217 | | // validFrom and validTo may not have values. |
| | | 218 | | // need to set them to reasonable defaults before moving forward. |
| | | 219 | | // SecurityContextSecurityToken will check the values, but I choose to check |
| | | 220 | | // here to keep the exception in our stack. |
| | | 221 | | // |
| | | 222 | | |
| | | 223 | | DateTime validFromEffective; |
| | | 224 | | DateTime validToEffective; |
| | | 225 | | |
| | 0 | 226 | | if (validFrom.HasValue) |
| | | 227 | | { |
| | 0 | 228 | | validFromEffective = validFrom.Value.ToUniversalTime(); |
| | | 229 | | } |
| | | 230 | | else |
| | | 231 | | { |
| | 0 | 232 | | validFromEffective = DateTime.UtcNow; |
| | | 233 | | } |
| | | 234 | | |
| | 0 | 235 | | if (validTo.HasValue) |
| | | 236 | | { |
| | 0 | 237 | | validToEffective = validTo.Value.ToUniversalTime(); |
| | | 238 | | } |
| | | 239 | | else |
| | | 240 | | { |
| | 0 | 241 | | validToEffective = validFromEffective.Add(SessionSecurityTokenHandler.DefaultTokenLifetime); |
| | | 242 | | } |
| | | 243 | | |
| | 0 | 244 | | if (validFromEffective >= validToEffective) |
| | | 245 | | { |
| | 0 | 246 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(validFr |
| | | 247 | | } |
| | | 248 | | |
| | 0 | 249 | | if (validToEffective < DateTime.UtcNow) |
| | | 250 | | { |
| | 0 | 251 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(validTo |
| | | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | if (endpointId == null) |
| | | 255 | | { |
| | 0 | 256 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpointId)); |
| | | 257 | | } |
| | | 258 | | |
| | 0 | 259 | | if (!keyEffectiveTime.HasValue) |
| | | 260 | | { |
| | 0 | 261 | | keyEffectiveTime = (DateTime?)validFromEffective; |
| | | 262 | | } |
| | | 263 | | |
| | 0 | 264 | | if (!keyExpirationTime.HasValue) |
| | | 265 | | { |
| | 0 | 266 | | keyExpirationTime = (DateTime?)validToEffective; |
| | | 267 | | } |
| | | 268 | | |
| | 0 | 269 | | if (keyEffectiveTime.Value > keyExpirationTime.Value || keyEffectiveTime.Value < validFromEffective) |
| | | 270 | | { |
| | 0 | 271 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(keyEffe |
| | | 272 | | } |
| | | 273 | | |
| | 0 | 274 | | if (keyExpirationTime.Value > validToEffective) |
| | | 275 | | { |
| | 0 | 276 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(keyExpi |
| | | 277 | | } |
| | | 278 | | |
| | 0 | 279 | | if (securityContextSecurityTokenWrapperSecureConversationVersion == null) |
| | | 280 | | { |
| | | 281 | | // Not an SCT wrapper: use the default namespace. |
| | 0 | 282 | | _secureConversationVersion = WSSecureConversation13Constants.NamespaceUri; |
| | | 283 | | } |
| | | 284 | | else |
| | | 285 | | { |
| | | 286 | | // SCT wrapper: use the provided namespace. |
| | 0 | 287 | | _isSecurityContextSecurityTokenWrapper = true; |
| | 0 | 288 | | _secureConversationVersion = securityContextSecurityTokenWrapperSecureConversationVersion; |
| | | 289 | | } |
| | | 290 | | |
| | 0 | 291 | | if (key == null) |
| | | 292 | | { |
| | | 293 | | // We have to create a dummy key here. We are not in WCF and we will |
| | | 294 | | // never use this key. But this is created only to satisfy WCF's |
| | | 295 | | // SecurityContextSecurityToken constructor. |
| | 0 | 296 | | key = CryptoHelper.GenerateSymmetricKey(128); |
| | | 297 | | } |
| | | 298 | | |
| | 0 | 299 | | if (endpointId == null) |
| | | 300 | | { |
| | 0 | 301 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpointId)); |
| | | 302 | | } |
| | | 303 | | |
| | 0 | 304 | | _claimsPrincipal = claimsPrincipal; |
| | 0 | 305 | | _contextId = contextId; |
| | 0 | 306 | | _id = id; |
| | 0 | 307 | | _context = context; |
| | 0 | 308 | | _securityKeys = new ReadOnlyCollection<SecurityKey>(new SecurityKey[] { new InMemorySymmetricSecurityKey(key |
| | 0 | 309 | | _endpointId = endpointId; |
| | 0 | 310 | | _validFrom = validFrom.Value; |
| | 0 | 311 | | _validTo = validTo.Value; |
| | 0 | 312 | | _keyGeneration = keyGeneration; |
| | 0 | 313 | | _keyEffectiveTime = keyEffectiveTime.Value; |
| | 0 | 314 | | _keyExpirationTime = keyExpirationTime.Value; |
| | 0 | 315 | | _sctAuthorizationPolicy = sctAuthorizationPolicy; |
| | 0 | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | protected SessionSecurityToken(SerializationInfo info, StreamingContext context) |
| | | 319 | | { |
| | 0 | 320 | | if (info == null) |
| | 0 | 321 | | return; |
| | | 322 | | |
| | 0 | 323 | | byte[] cookie = (byte[])info.GetValue(TokenKey, typeof(byte[])); |
| | | 324 | | |
| | 0 | 325 | | if (null == cookie || 0 == cookie.Length) |
| | | 326 | | { |
| | 0 | 327 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperInvalidOperation(SR.Format(SR.ID4272)); |
| | | 328 | | } |
| | | 329 | | |
| | 0 | 330 | | SessionDictionary dictionary = SessionDictionary.Instance; |
| | | 331 | | // |
| | | 332 | | // We are creating a reader over the decrypted form of the cookie that is in memory. |
| | | 333 | | // Passing Max for the XmlDictionaryReaderQuotas is safe. |
| | | 334 | | // |
| | 0 | 335 | | using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(cookie, 0, cookie.Length, diction |
| | | 336 | | { |
| | | 337 | | // |
| | | 338 | | // Layout is strict, must be in following order: |
| | | 339 | | // |
| | | 340 | | // Version |
| | | 341 | | // SecureConversationVersion |
| | | 342 | | // ID |
| | | 343 | | // ContextID |
| | | 344 | | // Key |
| | | 345 | | // Generation {optional} |
| | | 346 | | // EffectiveTime |
| | | 347 | | // ExpiryTime |
| | | 348 | | // KeyEffectiveTime |
| | | 349 | | // KeyExpiryTime |
| | | 350 | | // |
| | | 351 | | // SessionSecurityToken data may follow, in the order: |
| | | 352 | | // |
| | 0 | 353 | | bool isSecurityContextSecurityTokenWrapper = false; |
| | 0 | 354 | | bool isPersistent = true; |
| | 0 | 355 | | bool isReferenceMode = false; |
| | 0 | 356 | | string cookieContext = string.Empty; |
| | | 357 | | |
| | 0 | 358 | | if (reader.IsStartElement(dictionary.SecurityContextToken, dictionary.EmptyString)) |
| | | 359 | | { |
| | 0 | 360 | | isSecurityContextSecurityTokenWrapper = true; |
| | | 361 | | } |
| | 0 | 362 | | else if (reader.IsStartElement(dictionary.SessionToken, dictionary.EmptyString)) |
| | | 363 | | { |
| | | 364 | | //@PersistentTrue |
| | 0 | 365 | | if (reader.GetAttribute(dictionary.PersistentTrue, dictionary.EmptyString) == null) |
| | | 366 | | { |
| | 0 | 367 | | isPersistent = false; |
| | | 368 | | } |
| | | 369 | | |
| | 0 | 370 | | if (reader.GetAttribute(dictionary.ReferenceModeTrue, dictionary.EmptyString) != null) |
| | | 371 | | { |
| | 0 | 372 | | isReferenceMode = true; |
| | | 373 | | } |
| | | 374 | | |
| | 0 | 375 | | reader.ReadFullStartElement(); |
| | 0 | 376 | | reader.MoveToContent(); |
| | | 377 | | |
| | | 378 | | // <Context> |
| | 0 | 379 | | if (reader.IsStartElement(dictionary.Context, dictionary.EmptyString)) |
| | | 380 | | { |
| | 0 | 381 | | cookieContext = reader.ReadElementContentAsString(); |
| | | 382 | | } |
| | | 383 | | } |
| | | 384 | | else |
| | | 385 | | { |
| | 0 | 386 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID |
| | | 387 | | } |
| | | 388 | | |
| | 0 | 389 | | string version = reader.ReadElementString(); |
| | 0 | 390 | | if (version != SupportedVersion) |
| | | 391 | | { |
| | 0 | 392 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID |
| | | 393 | | } |
| | | 394 | | |
| | | 395 | | // |
| | | 396 | | // SecureConversation Version |
| | | 397 | | // |
| | 0 | 398 | | string scNamespace = reader.ReadElementString(); |
| | | 399 | | Uri scVersion; |
| | | 400 | | |
| | 0 | 401 | | if (scNamespace == WSSecureConversationFeb2005Constants.Namespace) |
| | | 402 | | { |
| | 0 | 403 | | scVersion = WSSecureConversationFeb2005Constants.NamespaceUri; |
| | | 404 | | } |
| | 0 | 405 | | else if (scNamespace == WSSecureConversation13Constants.Namespace) |
| | | 406 | | { |
| | 0 | 407 | | scVersion = WSSecureConversation13Constants.NamespaceUri; |
| | | 408 | | } |
| | | 409 | | else |
| | | 410 | | { |
| | 0 | 411 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID |
| | | 412 | | } |
| | | 413 | | |
| | 0 | 414 | | string instanceIdentifier = null; |
| | 0 | 415 | | if (reader.IsStartElement(dictionary.Id, dictionary.EmptyString)) |
| | | 416 | | { |
| | 0 | 417 | | instanceIdentifier = reader.ReadElementString(); |
| | | 418 | | } |
| | | 419 | | |
| | 0 | 420 | | if (string.IsNullOrEmpty(instanceIdentifier)) |
| | | 421 | | { |
| | 0 | 422 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 423 | | } |
| | | 424 | | |
| | 0 | 425 | | if (!reader.IsStartElement(dictionary.ContextId, dictionary.EmptyString)) |
| | | 426 | | { |
| | 0 | 427 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID |
| | | 428 | | } |
| | | 429 | | |
| | 0 | 430 | | SysUniqueId contextIdentifier = reader.ReadElementContentAsUniqueId(); |
| | | 431 | | |
| | 0 | 432 | | if (!reader.IsStartElement(dictionary.Key, dictionary.EmptyString)) |
| | | 433 | | { |
| | 0 | 434 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID |
| | | 435 | | } |
| | 0 | 436 | | byte[] key = reader.ReadElementContentAsBase64(); |
| | | 437 | | |
| | | 438 | | // optional |
| | 0 | 439 | | SysUniqueId generation = null; |
| | 0 | 440 | | if (reader.IsStartElement(dictionary.KeyGeneration, dictionary.EmptyString)) |
| | | 441 | | { |
| | 0 | 442 | | generation = reader.ReadElementContentAsUniqueId(); |
| | | 443 | | } |
| | | 444 | | |
| | 0 | 445 | | if (!reader.IsStartElement(dictionary.EffectiveTime, dictionary.EmptyString)) |
| | | 446 | | { |
| | 0 | 447 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID |
| | | 448 | | } |
| | 0 | 449 | | DateTime effectiveTime = new DateTime(XmlUtil.ReadElementContentAsInt64(reader), DateTimeKind.Utc); |
| | | 450 | | |
| | 0 | 451 | | if (!reader.IsStartElement(dictionary.ExpiryTime, dictionary.EmptyString)) |
| | | 452 | | { |
| | 0 | 453 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID |
| | | 454 | | } |
| | 0 | 455 | | DateTime expiryTime = new DateTime(XmlUtil.ReadElementContentAsInt64(reader), DateTimeKind.Utc); |
| | | 456 | | |
| | 0 | 457 | | if (!reader.IsStartElement(dictionary.KeyEffectiveTime, dictionary.EmptyString)) |
| | | 458 | | { |
| | 0 | 459 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID |
| | | 460 | | } |
| | 0 | 461 | | DateTime keyEffectiveTime = new DateTime(XmlUtil.ReadElementContentAsInt64(reader), DateTimeKind.Utc); |
| | | 462 | | |
| | 0 | 463 | | if (!reader.IsStartElement(dictionary.KeyExpiryTime, dictionary.EmptyString)) |
| | | 464 | | { |
| | 0 | 465 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID |
| | | 466 | | } |
| | 0 | 467 | | DateTime keyExpiryTime = new DateTime(XmlUtil.ReadElementContentAsInt64(reader), DateTimeKind.Utc); |
| | | 468 | | |
| | 0 | 469 | | ClaimsPrincipal principal = null; |
| | | 470 | | |
| | 0 | 471 | | if (reader.IsStartElement(dictionary.ClaimsPrincipal, dictionary.EmptyString)) |
| | | 472 | | { |
| | 0 | 473 | | principal = ReadPrincipal(reader, dictionary); |
| | | 474 | | } |
| | | 475 | | |
| | 0 | 476 | | SctAuthorizationPolicy sctAuthorizationPolicy = null; |
| | 0 | 477 | | if (reader.IsStartElement(dictionary.SctAuthorizationPolicy, dictionary.EmptyString)) |
| | | 478 | | { |
| | 0 | 479 | | reader.ReadStartElement(dictionary.SctAuthorizationPolicy, dictionary.EmptyString); |
| | 0 | 480 | | SysClaim sysClaim = DeserializeSysClaim(reader); |
| | 0 | 481 | | reader.ReadEndElement(); |
| | 0 | 482 | | sctAuthorizationPolicy = new SctAuthorizationPolicy(sysClaim); |
| | | 483 | | } |
| | | 484 | | |
| | 0 | 485 | | string endpointId = null; |
| | 0 | 486 | | if (reader.IsStartElement(dictionary.EndpointId, dictionary.EmptyString)) |
| | | 487 | | { |
| | 0 | 488 | | endpointId = reader.ReadElementContentAsString(); |
| | | 489 | | } |
| | | 490 | | |
| | 0 | 491 | | reader.ReadEndElement(); |
| | | 492 | | |
| | 0 | 493 | | _claimsPrincipal = principal; |
| | 0 | 494 | | _contextId = contextIdentifier; |
| | 0 | 495 | | _id = instanceIdentifier; |
| | 0 | 496 | | _context = cookieContext; |
| | 0 | 497 | | _securityKeys = new ReadOnlyCollection<SecurityKey>(new SecurityKey[] { new InMemorySymmetricSecurityKey |
| | 0 | 498 | | _endpointId = endpointId; |
| | 0 | 499 | | _validFrom = effectiveTime; |
| | 0 | 500 | | _validTo = expiryTime; |
| | 0 | 501 | | _keyGeneration = generation; |
| | 0 | 502 | | _keyEffectiveTime = keyEffectiveTime; |
| | 0 | 503 | | _keyExpirationTime = keyExpiryTime; |
| | 0 | 504 | | _isSecurityContextSecurityTokenWrapper = isSecurityContextSecurityTokenWrapper; |
| | 0 | 505 | | _secureConversationVersion = scVersion; |
| | 0 | 506 | | _sctAuthorizationPolicy = sctAuthorizationPolicy; |
| | 0 | 507 | | _isPersistent = isPersistent; |
| | 0 | 508 | | _isReferenceMode = isReferenceMode; |
| | | 509 | | |
| | 0 | 510 | | } |
| | 0 | 511 | | } |
| | | 512 | | |
| | | 513 | | /// <summary> |
| | | 514 | | /// The <see cref="ClaimsPrincipal"/> associated with the session. |
| | | 515 | | /// </summary> |
| | 0 | 516 | | public ClaimsPrincipal ClaimsPrincipal => _claimsPrincipal; |
| | | 517 | | |
| | | 518 | | /// <summary> |
| | | 519 | | /// Gets the user specified value. |
| | | 520 | | /// </summary> |
| | 0 | 521 | | public string Context => _context; |
| | | 522 | | |
| | | 523 | | /// <summary> |
| | | 524 | | /// The Session Context Identifier |
| | | 525 | | /// </summary> |
| | 0 | 526 | | public SysUniqueId ContextId => _contextId; |
| | | 527 | | |
| | | 528 | | /// <summary> |
| | | 529 | | /// Gets the Id of the endpoint to which this token is scoped. |
| | | 530 | | /// </summary> |
| | 0 | 531 | | public string EndpointId => _endpointId; |
| | | 532 | | |
| | | 533 | | /// <summary> |
| | | 534 | | /// Gets a value indicating whether this token is wrapping an SCT. |
| | | 535 | | /// </summary> |
| | 0 | 536 | | internal bool IsSecurityContextSecurityTokenWrapper => _isSecurityContextSecurityTokenWrapper; |
| | | 537 | | |
| | | 538 | | /// <summary> |
| | | 539 | | /// The effective date/time of the key in this token |
| | | 540 | | /// </summary> |
| | 0 | 541 | | public DateTime KeyEffectiveTime => _keyEffectiveTime; |
| | | 542 | | |
| | | 543 | | /// <summary> |
| | | 544 | | /// The expiration date/time of the key in this token |
| | | 545 | | /// </summary> |
| | 0 | 546 | | public DateTime KeyExpirationTime => _keyExpirationTime; |
| | | 547 | | |
| | | 548 | | /// <summary> |
| | | 549 | | /// The identifier for the key generation in this token |
| | | 550 | | /// </summary> |
| | 0 | 551 | | public SysUniqueId KeyGeneration => _keyGeneration; |
| | | 552 | | |
| | | 553 | | /// <summary> |
| | | 554 | | /// Gets the id of this token. |
| | | 555 | | /// </summary> |
| | 0 | 556 | | public override string Id => _id; |
| | | 557 | | |
| | | 558 | | /// <summary> |
| | | 559 | | /// If true, cookie is written as a persistent cookie. |
| | | 560 | | /// </summary> |
| | | 561 | | public bool IsPersistent |
| | | 562 | | { |
| | 0 | 563 | | get { return _isPersistent; } |
| | 0 | 564 | | set { _isPersistent = value; } |
| | | 565 | | } |
| | | 566 | | |
| | | 567 | | /// <summary> |
| | | 568 | | /// If true, the SessionSecurityToken is operating in reference mode. |
| | | 569 | | /// </summary> |
| | | 570 | | /// <remarks> |
| | | 571 | | /// In reference mode, a simple artifact is produced during serialization |
| | | 572 | | /// and the real token is stored in the token cache associated with the |
| | | 573 | | /// token handler. For Web Farm scenarios, the token cache must operate |
| | | 574 | | /// across all nodes in teh farm. |
| | | 575 | | /// </remarks> |
| | | 576 | | public bool IsReferenceMode |
| | | 577 | | { |
| | | 578 | | get |
| | | 579 | | { |
| | 0 | 580 | | return _isReferenceMode; |
| | | 581 | | } |
| | | 582 | | set |
| | | 583 | | { |
| | 0 | 584 | | _isReferenceMode = value; |
| | 0 | 585 | | } |
| | | 586 | | } |
| | | 587 | | |
| | | 588 | | /// <summary> |
| | | 589 | | /// Gets the authorization policies associated with the session. Only has meaning if this is wrapping an SCT |
| | | 590 | | /// token. |
| | | 591 | | /// </summary> |
| | 0 | 592 | | internal SctAuthorizationPolicy SctAuthorizationPolicy => _sctAuthorizationPolicy; |
| | | 593 | | |
| | | 594 | | /// <summary> |
| | | 595 | | /// Gets the SecureConversationVersion used for this token. |
| | | 596 | | /// </summary> |
| | 0 | 597 | | public Uri SecureConversationVersion => _secureConversationVersion; |
| | | 598 | | |
| | | 599 | | /// <summary> |
| | | 600 | | /// Gets the keys associated with this session, usually a single key |
| | | 601 | | /// </summary> |
| | 0 | 602 | | public override ReadOnlyCollection<SecurityKey> SecurityKeys => _securityKeys; |
| | | 603 | | |
| | | 604 | | /// <summary> |
| | | 605 | | /// Gets the begining DateTime before which token is invalid. |
| | | 606 | | /// </summary> |
| | 0 | 607 | | public override DateTime ValidFrom => _validFrom; |
| | | 608 | | |
| | | 609 | | /// <summary> |
| | | 610 | | /// Gets the ending DateTime after which the token is invalid. |
| | | 611 | | /// </summary> |
| | 0 | 612 | | public override DateTime ValidTo => _validTo; |
| | | 613 | | |
| | | 614 | | #region ISerializable Members |
| | | 615 | | |
| | | 616 | | public virtual void GetObjectData(SerializationInfo info, StreamingContext context) |
| | | 617 | | { |
| | 0 | 618 | | MemoryStream stream = new MemoryStream(); |
| | 0 | 619 | | SessionDictionary dictionary = SessionDictionary.Instance; |
| | | 620 | | |
| | | 621 | | // |
| | | 622 | | // XmlDictionaryWriter.CreateBinaryWriter() defaults to ownsStream=true. |
| | | 623 | | // So, the XmlWriter returned by the below code owns the memory stream, and will dispose it. |
| | | 624 | | // |
| | 0 | 625 | | using (XmlDictionaryWriter dicWriter = XmlDictionaryWriter.CreateBinaryWriter(stream, dictionary)) |
| | | 626 | | { |
| | | 627 | | //<SecurityContextSecurityToken> or <SessionSecurityToken> |
| | 0 | 628 | | if (IsSecurityContextSecurityTokenWrapper) |
| | | 629 | | { |
| | 0 | 630 | | dicWriter.WriteStartElement(dictionary.SecurityContextToken, dictionary.EmptyString); |
| | | 631 | | } |
| | | 632 | | else |
| | | 633 | | { |
| | 0 | 634 | | dicWriter.WriteStartElement(dictionary.SessionToken, dictionary.EmptyString); |
| | | 635 | | |
| | | 636 | | // @PersistentTrue |
| | 0 | 637 | | if (IsPersistent) |
| | | 638 | | { |
| | 0 | 639 | | dicWriter.WriteAttributeString(dictionary.PersistentTrue, dictionary.EmptyString, ""); |
| | | 640 | | } |
| | | 641 | | |
| | | 642 | | // @ReferenceModeTrue |
| | 0 | 643 | | if (IsReferenceMode) |
| | | 644 | | { |
| | 0 | 645 | | dicWriter.WriteAttributeString(dictionary.ReferenceModeTrue, dictionary.EmptyString, ""); |
| | | 646 | | } |
| | | 647 | | |
| | | 648 | | // <Context> |
| | 0 | 649 | | if (!string.IsNullOrEmpty(Context)) |
| | | 650 | | { |
| | 0 | 651 | | dicWriter.WriteElementString(dictionary.Context, dictionary.EmptyString, Context); |
| | | 652 | | } |
| | | 653 | | } |
| | | 654 | | |
| | | 655 | | // Serialization Format Version |
| | | 656 | | // <Version>1</Version> |
| | 0 | 657 | | dicWriter.WriteStartElement(dictionary.Version, dictionary.EmptyString); |
| | 0 | 658 | | dicWriter.WriteValue(SupportedVersion); |
| | 0 | 659 | | dicWriter.WriteEndElement(); |
| | | 660 | | |
| | | 661 | | // |
| | | 662 | | // SecureConversation Version |
| | | 663 | | // |
| | 0 | 664 | | dicWriter.WriteElementString(dictionary.SecureConversationVersion, dictionary.EmptyString, SecureConvers |
| | | 665 | | |
| | | 666 | | // |
| | | 667 | | // ID and ContextId |
| | | 668 | | // |
| | 0 | 669 | | dicWriter.WriteElementString(dictionary.Id, dictionary.EmptyString, Id); |
| | 0 | 670 | | XmlUtil.WriteElementStringAsUniqueId(dicWriter, dictionary.ContextId, dictionary.EmptyString, ContextId. |
| | | 671 | | |
| | | 672 | | // |
| | | 673 | | // Key material |
| | | 674 | | // |
| | 0 | 675 | | byte[] key = ((SymmetricSecurityKey)SecurityKeys[0]).GetSymmetricKey(); |
| | | 676 | | |
| | 0 | 677 | | dicWriter.WriteStartElement(dictionary.Key, dictionary.EmptyString); |
| | 0 | 678 | | dicWriter.WriteBase64(key, 0, key.Length); |
| | 0 | 679 | | dicWriter.WriteEndElement(); |
| | | 680 | | |
| | | 681 | | // |
| | | 682 | | // Key Generation |
| | | 683 | | // |
| | 0 | 684 | | if (KeyGeneration != null) |
| | | 685 | | { |
| | 0 | 686 | | XmlUtil.WriteElementStringAsUniqueId(dicWriter, dictionary.KeyGeneration, dictionary.EmptyString, Ke |
| | | 687 | | } |
| | | 688 | | |
| | | 689 | | // |
| | | 690 | | // Effective and Expiry dates |
| | | 691 | | // |
| | 0 | 692 | | XmlUtil.WriteElementContentAsInt64(dicWriter, dictionary.EffectiveTime, dictionary.EmptyString, ValidFro |
| | 0 | 693 | | XmlUtil.WriteElementContentAsInt64(dicWriter, dictionary.ExpiryTime, dictionary.EmptyString, ValidTo.ToU |
| | 0 | 694 | | XmlUtil.WriteElementContentAsInt64(dicWriter, dictionary.KeyEffectiveTime, dictionary.EmptyString, KeyEf |
| | 0 | 695 | | XmlUtil.WriteElementContentAsInt64(dicWriter, dictionary.KeyExpiryTime, dictionary.EmptyString, KeyExpir |
| | | 696 | | |
| | | 697 | | // |
| | | 698 | | // Claims Principal |
| | | 699 | | // |
| | 0 | 700 | | WritePrincipal(dicWriter, dictionary, ClaimsPrincipal); |
| | | 701 | | |
| | | 702 | | // The WCF SCT will have a SctAuthorizationPolicy that wraps the Primary Identity |
| | | 703 | | // of the bootstrap token. This is required for SCT renewal scenarios. Write the |
| | | 704 | | // SctAuthorizationPolicy if one is available. |
| | 0 | 705 | | if (SctAuthorizationPolicy != null) |
| | | 706 | | { |
| | 0 | 707 | | dicWriter.WriteStartElement(dictionary.SctAuthorizationPolicy, dictionary.EmptyString); |
| | 0 | 708 | | SysClaim identityClaim = ((CoreWCF.IdentityModel.Claims.DefaultClaimSet)((IAuthorizationPolicy)SctAu |
| | 0 | 709 | | SerializeSysClaim(identityClaim, dicWriter); |
| | 0 | 710 | | dicWriter.WriteEndElement(); |
| | | 711 | | } |
| | | 712 | | |
| | 0 | 713 | | dicWriter.WriteElementString(dictionary.EndpointId, dictionary.EmptyString, EndpointId); |
| | 0 | 714 | | dicWriter.WriteEndElement(); |
| | 0 | 715 | | dicWriter.Flush(); |
| | | 716 | | |
| | 0 | 717 | | info.AddValue(TokenKey, stream.ToArray()); |
| | 0 | 718 | | } |
| | 0 | 719 | | } |
| | | 720 | | |
| | | 721 | | #endregion |
| | | 722 | | |
| | | 723 | | /// <summary> |
| | | 724 | | /// Reads a ClaimsPrincipal from a XmlDictionaryReader. |
| | | 725 | | /// </summary> |
| | | 726 | | /// <param name=nameof(dictionaryReader)>XmlDictionaryReader positioned at dictionary.ClaimsPrincipal.</param> |
| | | 727 | | /// <param name=nameof(dictionary)>SessionDictionary to provide dictionary strings.</param> |
| | | 728 | | /// <exception cref="ArgumentNullException">The input argument 'dictionaryReader' or 'dictionary' is null.</exce |
| | | 729 | | /// <returns>ClaimsPrincipal</returns> |
| | | 730 | | private ClaimsPrincipal ReadPrincipal(XmlDictionaryReader dictionaryReader, SessionDictionary dictionary) |
| | | 731 | | { |
| | 0 | 732 | | if (dictionaryReader == null) |
| | | 733 | | { |
| | 0 | 734 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionaryReader)); |
| | | 735 | | } |
| | | 736 | | |
| | 0 | 737 | | if (dictionary == null) |
| | | 738 | | { |
| | 0 | 739 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionary)); |
| | | 740 | | } |
| | | 741 | | |
| | 0 | 742 | | ClaimsPrincipal principal = null; |
| | | 743 | | |
| | 0 | 744 | | Collection<ClaimsIdentity> identities = new Collection<ClaimsIdentity>(); |
| | | 745 | | |
| | 0 | 746 | | dictionaryReader.MoveToContent(); |
| | | 747 | | |
| | 0 | 748 | | if (dictionaryReader.IsStartElement(dictionary.ClaimsPrincipal, dictionary.EmptyString)) |
| | | 749 | | { |
| | 0 | 750 | | dictionaryReader.ReadFullStartElement(); |
| | | 751 | | |
| | 0 | 752 | | ReadIdentities(dictionaryReader, dictionary, identities); |
| | | 753 | | |
| | 0 | 754 | | dictionaryReader.ReadEndElement(); |
| | | 755 | | } |
| | | 756 | | |
| | | 757 | | // If we find a WindowsIdentity in the identities we just read, we should be creating a WindowsPrincipal usi |
| | 0 | 758 | | WindowsIdentity wi = null; |
| | 0 | 759 | | foreach (ClaimsIdentity identity in identities) |
| | | 760 | | { |
| | 0 | 761 | | wi = identity as WindowsIdentity; |
| | 0 | 762 | | if (wi != null) |
| | | 763 | | { |
| | 0 | 764 | | principal = new WindowsPrincipal(wi); |
| | 0 | 765 | | break; |
| | | 766 | | } |
| | | 767 | | } |
| | | 768 | | |
| | | 769 | | // If we did create a WindowsPrincipal we can remove the associated WindowsIdentity from the identities coll |
| | | 770 | | // so that we dont add it twice in the subsequent step |
| | 0 | 771 | | if (principal != null) |
| | | 772 | | { |
| | 0 | 773 | | identities.Remove(wi); |
| | | 774 | | } |
| | 0 | 775 | | else if (identities.Count > 0) |
| | | 776 | | { |
| | | 777 | | // If we did not create a WindowsPrincipal, default to a ClaimsPrincipal |
| | 0 | 778 | | principal = new ClaimsPrincipal(); |
| | | 779 | | } |
| | | 780 | | |
| | 0 | 781 | | if (principal != null) |
| | | 782 | | { |
| | | 783 | | // Add the identities we just read to the principal |
| | 0 | 784 | | principal.AddIdentities(identities); |
| | | 785 | | } |
| | | 786 | | |
| | 0 | 787 | | return principal; |
| | | 788 | | } |
| | | 789 | | |
| | | 790 | | /// <summary> |
| | | 791 | | /// Reads the ClaimsIdentites from a XmlDictionaryReader and adds them to a ClaimIdentityColleciton. |
| | | 792 | | /// </summary> |
| | | 793 | | /// <param name=nameof(dictionaryReader)>XmlDictionaryReader positioned at dictionary.Identities</param> |
| | | 794 | | /// <param name=nameof(dictionary)>SessionDictionary to provide dictionary strings.</param> |
| | | 795 | | /// <param name=nameof(identities)>A collection of <see cref="ClaimsIdentity"/> to populate.</param> |
| | | 796 | | /// <exception cref="ArgumentNullException">The input argument 'dictionaryReader', 'dictionary' or 'identities' |
| | | 797 | | /// <remarks>Reads 'n' identies and adds them to identies.</remarks> |
| | | 798 | | private void ReadIdentities(XmlDictionaryReader dictionaryReader, SessionDictionary dictionary, Collection<Claim |
| | | 799 | | { |
| | 0 | 800 | | if (dictionaryReader == null) |
| | | 801 | | { |
| | 0 | 802 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionaryReader)); |
| | | 803 | | } |
| | | 804 | | |
| | 0 | 805 | | if (dictionary == null) |
| | | 806 | | { |
| | 0 | 807 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionary)); |
| | | 808 | | } |
| | | 809 | | |
| | 0 | 810 | | if (identities == null) |
| | | 811 | | { |
| | 0 | 812 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identities)); |
| | | 813 | | } |
| | | 814 | | |
| | 0 | 815 | | dictionaryReader.MoveToContent(); |
| | | 816 | | |
| | 0 | 817 | | if (dictionaryReader.IsStartElement(dictionary.Identities, dictionary.EmptyString)) |
| | | 818 | | { |
| | 0 | 819 | | dictionaryReader.ReadFullStartElement(); |
| | | 820 | | |
| | 0 | 821 | | while (dictionaryReader.IsStartElement(dictionary.Identity, dictionary.EmptyString)) |
| | | 822 | | { |
| | 0 | 823 | | identities.Add(ReadIdentity(dictionaryReader, dictionary)); |
| | | 824 | | } |
| | | 825 | | |
| | 0 | 826 | | dictionaryReader.ReadEndElement(); |
| | | 827 | | } |
| | 0 | 828 | | } |
| | | 829 | | |
| | | 830 | | /// <summary> |
| | | 831 | | /// Reads a single ClaimsIdentity from a XmlDictionaryReader. |
| | | 832 | | /// </summary> |
| | | 833 | | /// <param name=nameof(dictionaryReader)>XmlDictionaryReader positioned at dictionary.Identity.</param> |
| | | 834 | | /// <param name=nameof(dictionary)>SessionDictionary to provide dictionary strings.</param> |
| | | 835 | | /// <exception cref="ArgumentNullException">The input argument 'dictionaryReader' or 'dictionary' is null.</exce |
| | | 836 | | /// <exception cref="SecurityTokenException">The dictionaryReader is not positioned a SessionDictionary.Identity |
| | | 837 | | /// <returns>ClaimsIdentity</returns> |
| | | 838 | | private ClaimsIdentity ReadIdentity(XmlDictionaryReader dictionaryReader, SessionDictionary dictionary) |
| | | 839 | | { |
| | 0 | 840 | | if (dictionaryReader == null) |
| | | 841 | | { |
| | 0 | 842 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionaryReader)); |
| | | 843 | | } |
| | | 844 | | |
| | 0 | 845 | | if (dictionary == null) |
| | | 846 | | { |
| | 0 | 847 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionary)); |
| | | 848 | | } |
| | | 849 | | |
| | 0 | 850 | | dictionaryReader.MoveToContent(); |
| | | 851 | | |
| | 0 | 852 | | ClaimsIdentity identity = null; |
| | | 853 | | |
| | 0 | 854 | | if (!dictionaryReader.IsStartElement(dictionary.Identity, dictionary.EmptyString)) |
| | | 855 | | { |
| | 0 | 856 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID3007 |
| | | 857 | | } |
| | | 858 | | |
| | | 859 | | // @NameClaimType |
| | 0 | 860 | | string nameClaimType = dictionaryReader.GetAttribute(dictionary.NameClaimType, dictionary.EmptyString); |
| | | 861 | | |
| | | 862 | | // @RoleClaimType |
| | 0 | 863 | | string roleClaimType = dictionaryReader.GetAttribute(dictionary.RoleClaimType, dictionary.EmptyString); |
| | | 864 | | |
| | | 865 | | // @WindowsLogonName (optional) => windows claims identity |
| | 0 | 866 | | string logonName = dictionaryReader.GetAttribute(dictionary.WindowsLogonName, dictionary.EmptyString); |
| | 0 | 867 | | string authenticationType = dictionaryReader.GetAttribute(dictionary.AuthenticationType, dictionary.EmptyStr |
| | | 868 | | |
| | 0 | 869 | | if (string.IsNullOrEmpty(logonName)) |
| | | 870 | | { |
| | 0 | 871 | | identity = new ClaimsIdentity(authenticationType, nameClaimType, roleClaimType); |
| | | 872 | | } |
| | | 873 | | else |
| | | 874 | | { |
| | | 875 | | // The WindowsIdentity(string, string) c'tor does not set the Auth type. Hence we use that c'tor to get |
| | | 876 | | // call the other c'tor that actually sets the authType passed in. |
| | | 877 | | // DevDiv 279196 tracks the issue and in WindowsIdentity c'tor. Its too late to fix it in 4.5 cycle as w |
| | | 878 | | // able to complete the analysis of the change for the current release. This should be investigated in 5 |
| | 0 | 879 | | WindowsIdentity winId = new WindowsIdentity(GetUpn(logonName)); |
| | 0 | 880 | | identity = new WindowsIdentity(winId.Token, authenticationType); |
| | | 881 | | } |
| | | 882 | | |
| | | 883 | | // @Label |
| | 0 | 884 | | identity.Label = dictionaryReader.GetAttribute(dictionary.Label, dictionary.EmptyString); |
| | | 885 | | |
| | | 886 | | |
| | 0 | 887 | | dictionaryReader.ReadFullStartElement(); |
| | | 888 | | |
| | | 889 | | // <ClaimCollection> |
| | 0 | 890 | | if (dictionaryReader.IsStartElement(dictionary.ClaimCollection, dictionary.EmptyString)) |
| | | 891 | | { |
| | 0 | 892 | | dictionaryReader.ReadStartElement(); |
| | | 893 | | |
| | 0 | 894 | | Collection<Claim> claims = new Collection<Claim>(); |
| | 0 | 895 | | ReadClaims(dictionaryReader, dictionary, claims); |
| | 0 | 896 | | identity.AddClaims(claims); |
| | | 897 | | |
| | 0 | 898 | | dictionaryReader.ReadEndElement(); |
| | | 899 | | } |
| | | 900 | | |
| | | 901 | | // <Actor> |
| | 0 | 902 | | if (dictionaryReader.IsStartElement(dictionary.Actor, dictionary.EmptyString)) |
| | | 903 | | { |
| | 0 | 904 | | dictionaryReader.ReadStartElement(); |
| | | 905 | | |
| | 0 | 906 | | identity.Actor = ReadIdentity(dictionaryReader, dictionary); |
| | | 907 | | |
| | 0 | 908 | | dictionaryReader.ReadEndElement(); |
| | | 909 | | } |
| | | 910 | | |
| | 0 | 911 | | if (dictionaryReader.IsStartElement(dictionary.BootstrapToken, dictionary.EmptyString)) |
| | | 912 | | { |
| | 0 | 913 | | dictionaryReader.ReadStartElement(); |
| | | 914 | | |
| | 0 | 915 | | byte[] bytes = dictionaryReader.ReadContentAsBase64(); |
| | 0 | 916 | | using (MemoryStream ms = new MemoryStream(bytes)) |
| | | 917 | | { |
| | 0 | 918 | | BinaryFormatter formatter = new BinaryFormatter(); |
| | 0 | 919 | | identity.BootstrapContext = (BootstrapContext)formatter.Deserialize(ms); |
| | 0 | 920 | | } |
| | | 921 | | |
| | 0 | 922 | | dictionaryReader.ReadEndElement(); |
| | | 923 | | } |
| | | 924 | | |
| | 0 | 925 | | dictionaryReader.ReadEndElement(); // Identity |
| | | 926 | | |
| | 0 | 927 | | return identity; |
| | | 928 | | } |
| | | 929 | | |
| | | 930 | | /// <summary> |
| | | 931 | | /// Returns a User Principal Name from a windows logon name. |
| | | 932 | | /// </summary> |
| | | 933 | | /// <param name="windowsLogonName">Name to translate into the UPN</param> |
| | | 934 | | /// <exception cref="ArgumentNullException">The input argument 'windowsLogonName' is null or empty.</exception> |
| | | 935 | | /// <exception cref="InvalidOperationException">If 'windowsLogonName' is not of the form domain\\user or user@do |
| | | 936 | | /// <returns>A User Principal Name of the form 'user@domain'</returns> |
| | | 937 | | private string GetUpn(string windowsLogonName) |
| | | 938 | | { |
| | 0 | 939 | | if (string.IsNullOrEmpty(windowsLogonName)) |
| | | 940 | | { |
| | 0 | 941 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(windowsLogonName)); |
| | | 942 | | } |
| | 0 | 943 | | int delimiterPos = windowsLogonName.IndexOf('\\'); |
| | | 944 | | |
| | 0 | 945 | | if ((delimiterPos < 0) || (delimiterPos == 0) || (delimiterPos == windowsLogonName.Length - 1)) |
| | | 946 | | { |
| | 0 | 947 | | if (IsPossibleUpn(windowsLogonName)) |
| | | 948 | | { |
| | 0 | 949 | | return windowsLogonName; |
| | | 950 | | } |
| | | 951 | | |
| | 0 | 952 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4 |
| | | 953 | | } |
| | | 954 | | |
| | 0 | 955 | | string shortDomainName = windowsLogonName.Substring(0, delimiterPos + 1); |
| | 0 | 956 | | string userName = windowsLogonName.Substring(delimiterPos + 1); |
| | | 957 | | string fullDomainName; |
| | | 958 | | bool found; |
| | | 959 | | |
| | | 960 | | // 1) Read from cache |
| | 0 | 961 | | lock (DomainNameMap) |
| | | 962 | | { |
| | 0 | 963 | | found = DomainNameMap.TryGetValue(shortDomainName, out fullDomainName); |
| | 0 | 964 | | } |
| | | 965 | | |
| | 0 | 966 | | return userName + "@" + fullDomainName; |
| | | 967 | | } |
| | | 968 | | |
| | | 969 | | /// <summary> |
| | | 970 | | /// Reads Claims from a XmlDictionaryReader and adds them to a ClaimCollection. |
| | | 971 | | /// </summary> |
| | | 972 | | /// <param name=nameof(dictionaryReader)>XmlDictionaryReader positioned at dictionary.Claim.</param> |
| | | 973 | | /// <param name=nameof(dictionary)>SessionDictionary to provide dictionary strings.</param> |
| | | 974 | | /// <param name="claims">ClaimCollection to add the claims to.</param> |
| | | 975 | | /// <exception cref="ArgumentNullException">The input argument 'dictionaryReader', 'dictionary' or 'claims' is |
| | | 976 | | /// <remarks>Reads 'n' claims and adds them to claims.</remarks> |
| | | 977 | | private void ReadClaims(XmlDictionaryReader dictionaryReader, SessionDictionary dictionary, Collection<Claim> cl |
| | | 978 | | { |
| | 0 | 979 | | if (dictionaryReader == null) |
| | | 980 | | { |
| | 0 | 981 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionaryReader)); |
| | | 982 | | } |
| | | 983 | | |
| | 0 | 984 | | if (dictionary == null) |
| | | 985 | | { |
| | 0 | 986 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionary)); |
| | | 987 | | } |
| | | 988 | | |
| | 0 | 989 | | if (claims == null) |
| | | 990 | | { |
| | 0 | 991 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("claims"); |
| | | 992 | | } |
| | | 993 | | |
| | 0 | 994 | | while (dictionaryReader.IsStartElement(dictionary.Claim, dictionary.EmptyString)) |
| | | 995 | | { |
| | | 996 | | // @Issuer (optional), @OriginalIssuer (optional), @Type, @Value, @ValueType |
| | | 997 | | |
| | 0 | 998 | | Claim claim = new Claim(dictionaryReader.GetAttribute(dictionary.Type, dictionary.EmptyString), |
| | 0 | 999 | | dictionaryReader.GetAttribute(dictionary.Value, dictionary.EmptyString), |
| | 0 | 1000 | | dictionaryReader.GetAttribute(dictionary.ValueType, dictionary.EmptyString), |
| | 0 | 1001 | | dictionaryReader.GetAttribute(dictionary.Issuer, dictionary.EmptyString), |
| | 0 | 1002 | | dictionaryReader.GetAttribute(dictionary.OriginalIssuer, dictionary.EmptyString |
| | | 1003 | | |
| | 0 | 1004 | | dictionaryReader.ReadFullStartElement(); |
| | | 1005 | | |
| | | 1006 | | // <Properties> (optional) |
| | 0 | 1007 | | if (dictionaryReader.IsStartElement(dictionary.ClaimProperties, dictionary.EmptyString)) |
| | | 1008 | | { |
| | 0 | 1009 | | ReadClaimProperties(dictionaryReader, dictionary, claim.Properties); |
| | | 1010 | | } |
| | | 1011 | | |
| | 0 | 1012 | | dictionaryReader.ReadEndElement(); |
| | | 1013 | | |
| | 0 | 1014 | | claims.Add(claim); |
| | | 1015 | | } |
| | 0 | 1016 | | } |
| | | 1017 | | |
| | | 1018 | | /// <summary> |
| | | 1019 | | /// Reads ClaimProperties from a XmlDictionaryReader and adds them to a Dictionary. |
| | | 1020 | | /// </summary> |
| | | 1021 | | /// <param name=nameof(dictionaryReader)>XmlDictionaryReader positioned at the element dictionary.ClaimPropertie |
| | | 1022 | | /// <param name=nameof(dictionary)>SessionDictionary to provide dictionary strings.</param> |
| | | 1023 | | /// <param name=nameof(properties)>Dictionary to add properties to.</param> |
| | | 1024 | | /// <exception cref="ArgumentNullException">The input argument 'dictionaryReader', 'dictionary' or 'properties' |
| | | 1025 | | /// <exception cref="SecurityTokenException">Is thrown if the 'name' of a property is null or an empty string.</ |
| | | 1026 | | /// <exception cref="SecurityTokenException">Is thrown if the 'value' of a property is null.</exception> |
| | | 1027 | | /// <remarks>Reads 'n' properties.</remarks> |
| | | 1028 | | private void ReadClaimProperties(XmlDictionaryReader dictionaryReader, SessionDictionary dictionary, IDictionary |
| | | 1029 | | { |
| | 0 | 1030 | | if (dictionaryReader == null) |
| | | 1031 | | { |
| | 0 | 1032 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionaryReader)); |
| | | 1033 | | } |
| | | 1034 | | |
| | 0 | 1035 | | if (dictionary == null) |
| | | 1036 | | { |
| | 0 | 1037 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionary)); |
| | | 1038 | | } |
| | | 1039 | | |
| | 0 | 1040 | | if (properties == null) |
| | | 1041 | | { |
| | 0 | 1042 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(properties)); |
| | | 1043 | | } |
| | | 1044 | | |
| | 0 | 1045 | | dictionaryReader.ReadStartElement(); |
| | | 1046 | | |
| | | 1047 | | // <Property> |
| | 0 | 1048 | | while (dictionaryReader.IsStartElement(dictionary.ClaimProperty, dictionary.EmptyString)) |
| | | 1049 | | { |
| | | 1050 | | // @Name, @Value |
| | | 1051 | | |
| | 0 | 1052 | | string name = dictionaryReader.GetAttribute(dictionary.ClaimPropertyName, dictionary.EmptyString); |
| | 0 | 1053 | | string value = dictionaryReader.GetAttribute(dictionary.ClaimPropertyValue, dictionary.EmptyString); |
| | | 1054 | | |
| | 0 | 1055 | | if (string.IsNullOrEmpty(name)) |
| | | 1056 | | { |
| | 0 | 1057 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID |
| | | 1058 | | } |
| | | 1059 | | |
| | 0 | 1060 | | if (string.IsNullOrEmpty(value)) |
| | | 1061 | | { |
| | 0 | 1062 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID |
| | | 1063 | | } |
| | | 1064 | | |
| | 0 | 1065 | | properties.Add(new KeyValuePair<string, string>(name, value)); |
| | | 1066 | | |
| | 0 | 1067 | | dictionaryReader.ReadFullStartElement(); |
| | 0 | 1068 | | dictionaryReader.ReadEndElement(); |
| | | 1069 | | } |
| | | 1070 | | |
| | 0 | 1071 | | dictionaryReader.ReadEndElement(); |
| | 0 | 1072 | | } |
| | | 1073 | | |
| | | 1074 | | /// <summary> |
| | | 1075 | | /// Writes out a ClaimsPrincipal using a XmlDictionaryWriter. |
| | | 1076 | | /// </summary> |
| | | 1077 | | /// <param name="dictionaryWriter">XmlDictionaryWriter to write to.</param> |
| | | 1078 | | /// <param name=nameof(dictionary)>SessionDictionary to provide dictionary strings.</param> |
| | | 1079 | | /// <param name="principal">ClaimsPrincipal to write.</param> |
| | | 1080 | | /// <exception cref="ArgumentNullException">The input argument 'dictionaryWriter', 'dictionary' or 'principal' i |
| | | 1081 | | private void WritePrincipal(XmlDictionaryWriter dictionaryWriter, SessionDictionary dictionary, ClaimsPrincipal |
| | | 1082 | | { |
| | 0 | 1083 | | if (dictionaryWriter == null) |
| | | 1084 | | { |
| | 0 | 1085 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionaryWriter)); |
| | | 1086 | | } |
| | | 1087 | | |
| | 0 | 1088 | | if (dictionary == null) |
| | | 1089 | | { |
| | 0 | 1090 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionary)); |
| | | 1091 | | } |
| | | 1092 | | |
| | 0 | 1093 | | if (principal == null) |
| | | 1094 | | { |
| | 0 | 1095 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(principal)); |
| | | 1096 | | } |
| | | 1097 | | |
| | | 1098 | | // <ClaimsPrincipal> |
| | 0 | 1099 | | dictionaryWriter.WriteStartElement(dictionary.ClaimsPrincipal, dictionary.EmptyString); |
| | | 1100 | | |
| | 0 | 1101 | | if (principal.Identities != null) |
| | | 1102 | | { |
| | 0 | 1103 | | WriteIdentities(dictionaryWriter, dictionary, principal.Identities); |
| | | 1104 | | } |
| | | 1105 | | |
| | 0 | 1106 | | dictionaryWriter.WriteEndElement(); |
| | 0 | 1107 | | } |
| | | 1108 | | |
| | | 1109 | | /// <summary> |
| | | 1110 | | /// Writes a collection of ClaimsIdentity using a XmlDictionaryWriter. |
| | | 1111 | | /// </summary> |
| | | 1112 | | /// <param name="dictionaryWriter">XmlDictionaryWriter to write to.</param> |
| | | 1113 | | /// <param name=nameof(dictionary)>SessionDictionary to provide dictionary strings.</param> |
| | | 1114 | | /// <param name=nameof(identities)>The collection of ClaimsIdentity to write.</param> |
| | | 1115 | | /// <exception cref="ArgumentNullException">The input argument 'dictionaryWriter', 'dictionary' or 'identities' |
| | | 1116 | | private void WriteIdentities(XmlDictionaryWriter dictionaryWriter, SessionDictionary dictionary, IEnumerable<Cla |
| | | 1117 | | { |
| | 0 | 1118 | | if (dictionaryWriter == null) |
| | | 1119 | | { |
| | 0 | 1120 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionaryWriter)); |
| | | 1121 | | } |
| | | 1122 | | |
| | 0 | 1123 | | if (dictionary == null) |
| | | 1124 | | { |
| | 0 | 1125 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionary)); |
| | | 1126 | | } |
| | | 1127 | | |
| | 0 | 1128 | | if (identities == null) |
| | | 1129 | | { |
| | 0 | 1130 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identities)); |
| | | 1131 | | } |
| | | 1132 | | |
| | | 1133 | | // <Identities> |
| | 0 | 1134 | | dictionaryWriter.WriteStartElement(dictionary.Identities, dictionary.EmptyString); |
| | | 1135 | | |
| | 0 | 1136 | | foreach (ClaimsIdentity ci in identities) |
| | | 1137 | | { |
| | 0 | 1138 | | WriteIdentity(dictionaryWriter, dictionary, ci); |
| | | 1139 | | } |
| | | 1140 | | |
| | 0 | 1141 | | dictionaryWriter.WriteEndElement(); |
| | 0 | 1142 | | } |
| | | 1143 | | |
| | | 1144 | | /// <summary> |
| | | 1145 | | /// Writes a single ClaimsIdentity using a XmlDictionaryWriter. |
| | | 1146 | | /// </summary> |
| | | 1147 | | /// <param name="dictionaryWriter">XmlDictionaryWriter to write to.</param> |
| | | 1148 | | /// <param name=nameof(dictionary)>SessionDictionary to provide dictionary strings.</param> |
| | | 1149 | | /// <param name="identity">ClaimsIdentiy to write.</param> |
| | | 1150 | | /// <exception cref="ArgumentNullException">The input argument 'dictionaryWriter', 'dictionary' or 'identity' is |
| | | 1151 | | private void WriteIdentity(XmlDictionaryWriter dictionaryWriter, SessionDictionary dictionary, ClaimsIdentity id |
| | | 1152 | | { |
| | 0 | 1153 | | if (dictionaryWriter == null) |
| | | 1154 | | { |
| | 0 | 1155 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionaryWriter)); |
| | | 1156 | | } |
| | | 1157 | | |
| | 0 | 1158 | | if (dictionary == null) |
| | | 1159 | | { |
| | 0 | 1160 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionary)); |
| | | 1161 | | } |
| | | 1162 | | |
| | 0 | 1163 | | if (identity == null) |
| | | 1164 | | { |
| | 0 | 1165 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identity)); |
| | | 1166 | | } |
| | | 1167 | | |
| | | 1168 | | // |
| | | 1169 | | // WindowsIdentity needs special handling |
| | | 1170 | | // |
| | | 1171 | | |
| | | 1172 | | // <Identity> |
| | 0 | 1173 | | dictionaryWriter.WriteStartElement(dictionary.Identity, dictionary.EmptyString); |
| | | 1174 | | |
| | 0 | 1175 | | WindowsIdentity wci = identity as WindowsIdentity; |
| | 0 | 1176 | | if (wci != null) |
| | | 1177 | | { |
| | | 1178 | | // @WindowsLogonName (optional) |
| | 0 | 1179 | | dictionaryWriter.WriteAttributeString(dictionary.WindowsLogonName, dictionary.EmptyString, wci.Name); |
| | | 1180 | | } |
| | | 1181 | | |
| | | 1182 | | // @AuthenticationType (optional) |
| | 0 | 1183 | | if (!string.IsNullOrEmpty(identity.AuthenticationType)) |
| | | 1184 | | { |
| | 0 | 1185 | | dictionaryWriter.WriteAttributeString(dictionary.AuthenticationType, dictionary.EmptyString, identity.Au |
| | | 1186 | | } |
| | | 1187 | | |
| | | 1188 | | // @LabelWrite (optional) |
| | 0 | 1189 | | if (!string.IsNullOrEmpty(identity.Label)) |
| | | 1190 | | { |
| | 0 | 1191 | | dictionaryWriter.WriteAttributeString(dictionary.Label, dictionary.EmptyString, identity.Label); |
| | | 1192 | | } |
| | | 1193 | | |
| | | 1194 | | // @NameClaimType (optional) |
| | 0 | 1195 | | if (identity.NameClaimType != null) |
| | | 1196 | | { |
| | 0 | 1197 | | dictionaryWriter.WriteAttributeString(dictionary.NameClaimType, dictionary.EmptyString, identity.NameCla |
| | | 1198 | | } |
| | | 1199 | | |
| | | 1200 | | // @RoleClaimType (optional) |
| | 0 | 1201 | | if (identity.RoleClaimType != null) |
| | | 1202 | | { |
| | 0 | 1203 | | dictionaryWriter.WriteAttributeString(dictionary.RoleClaimType, dictionary.EmptyString, identity.RoleCla |
| | | 1204 | | } |
| | | 1205 | | |
| | | 1206 | | // <ClaimCollection> (optional) |
| | 0 | 1207 | | if (identity.Claims != null) |
| | | 1208 | | { |
| | 0 | 1209 | | dictionaryWriter.WriteStartElement(dictionary.ClaimCollection, dictionary.EmptyString); |
| | | 1210 | | |
| | 0 | 1211 | | WriteClaims(dictionaryWriter, dictionary, identity.Claims, (wci == null) ? |
| | 0 | 1212 | | (OutboundClaimsFilter)null |
| | 0 | 1213 | | : |
| | 0 | 1214 | | // do not serialize SID claims for WindowsIdentities as they will be created when the |
| | 0 | 1215 | | // windows identity is recreated. |
| | 0 | 1216 | | delegate(Claim c) |
| | 0 | 1217 | | { |
| | 0 | 1218 | | if (c.Type == ClaimTypes.GroupSid |
| | 0 | 1219 | | || c.Type == ClaimTypes.PrimaryGroupSid |
| | 0 | 1220 | | || c.Type == ClaimTypes.PrimarySid |
| | 0 | 1221 | | || c.Type == ClaimTypes.DenyOnlyPrimaryGroupSid |
| | 0 | 1222 | | || c.Type == ClaimTypes.DenyOnlyPrimarySid |
| | 0 | 1223 | | || c.Type == ClaimTypes.Name && c.Issuer == ClaimsIdentity.DefaultIssuer && c.ValueType == Cla |
| | 0 | 1224 | | { |
| | 0 | 1225 | | return true; |
| | 0 | 1226 | | } |
| | 0 | 1227 | | |
| | 0 | 1228 | | return false; |
| | 0 | 1229 | | } |
| | 0 | 1230 | | ); |
| | | 1231 | | |
| | 0 | 1232 | | dictionaryWriter.WriteEndElement(); |
| | | 1233 | | } |
| | | 1234 | | |
| | | 1235 | | // <Actor> (optional) |
| | 0 | 1236 | | if (identity.Actor != null) |
| | | 1237 | | { |
| | 0 | 1238 | | dictionaryWriter.WriteStartElement(dictionary.Actor, dictionary.EmptyString); |
| | | 1239 | | |
| | 0 | 1240 | | WriteIdentity(dictionaryWriter, dictionary, identity.Actor); |
| | | 1241 | | |
| | 0 | 1242 | | dictionaryWriter.WriteEndElement(); |
| | | 1243 | | } |
| | | 1244 | | |
| | 0 | 1245 | | if (identity.BootstrapContext != null) |
| | | 1246 | | { |
| | 0 | 1247 | | dictionaryWriter.WriteStartElement(dictionary.BootstrapToken, dictionary.EmptyString); |
| | | 1248 | | |
| | 0 | 1249 | | using (MemoryStream ms = new MemoryStream()) |
| | | 1250 | | { |
| | 0 | 1251 | | BinaryFormatter formatter = new BinaryFormatter(); |
| | 0 | 1252 | | formatter.Serialize(ms, identity.BootstrapContext); |
| | 0 | 1253 | | byte[] bootstrapArray = ms.ToArray(); |
| | 0 | 1254 | | dictionaryWriter.WriteBase64(bootstrapArray, 0, bootstrapArray.Length); |
| | 0 | 1255 | | } |
| | | 1256 | | |
| | 0 | 1257 | | dictionaryWriter.WriteEndElement(); // </BootstrapToken> |
| | | 1258 | | } |
| | | 1259 | | |
| | 0 | 1260 | | dictionaryWriter.WriteEndElement(); |
| | | 1261 | | |
| | 0 | 1262 | | } |
| | | 1263 | | |
| | | 1264 | | /// <summary> |
| | | 1265 | | /// Actor that returns true if a claim should be filtered. |
| | | 1266 | | /// </summary> |
| | | 1267 | | /// <param name="claim">Claim to check.</param> |
| | | 1268 | | /// <returns></returns> |
| | | 1269 | | private delegate bool OutboundClaimsFilter(Claim claim); |
| | | 1270 | | |
| | | 1271 | | /// <summary> |
| | | 1272 | | /// Writes a collection of claims using a XmlDictionaryWriter. |
| | | 1273 | | /// </summary> |
| | | 1274 | | /// <param name="dictionaryWriter">XmlDictionaryWriter to write to.</param> |
| | | 1275 | | /// <param name=nameof(dictionary)>SessionDictionary to provide dictionary strings.</param> |
| | | 1276 | | /// <param name="claims">ClaimCollection to write.</param> |
| | | 1277 | | /// <param name="outboundClaimsFilter">Filter to apply when writing claims. If parameter is not null and filter |
| | | 1278 | | /// <exception cref="ArgumentNullException">The input argument 'dictionaryWriter', 'dictionary' or 'claims' is n |
| | | 1279 | | private void WriteClaims(XmlDictionaryWriter dictionaryWriter, SessionDictionary dictionary, IEnumerable<Claim> |
| | | 1280 | | { |
| | 0 | 1281 | | if (dictionaryWriter == null) |
| | | 1282 | | { |
| | 0 | 1283 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionaryWriter)); |
| | | 1284 | | } |
| | | 1285 | | |
| | 0 | 1286 | | if (dictionary == null) |
| | | 1287 | | { |
| | 0 | 1288 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionary)); |
| | | 1289 | | } |
| | | 1290 | | |
| | 0 | 1291 | | if (claims == null) |
| | | 1292 | | { |
| | 0 | 1293 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("claims"); |
| | | 1294 | | } |
| | | 1295 | | |
| | 0 | 1296 | | foreach (Claim claim in claims) |
| | | 1297 | | { |
| | 0 | 1298 | | if (claim == null) |
| | | 1299 | | { |
| | | 1300 | | continue; |
| | | 1301 | | } |
| | | 1302 | | |
| | 0 | 1303 | | if (outboundClaimsFilter != null && outboundClaimsFilter(claim)) |
| | | 1304 | | { |
| | | 1305 | | continue; |
| | | 1306 | | } |
| | | 1307 | | |
| | | 1308 | | // <Claim> |
| | 0 | 1309 | | dictionaryWriter.WriteStartElement(dictionary.Claim, dictionary.EmptyString); |
| | | 1310 | | |
| | | 1311 | | // @Issuer |
| | 0 | 1312 | | if (!string.IsNullOrEmpty(claim.Issuer)) |
| | | 1313 | | { |
| | 0 | 1314 | | dictionaryWriter.WriteAttributeString(dictionary.Issuer, dictionary.EmptyString, claim.Issuer); |
| | | 1315 | | } |
| | | 1316 | | |
| | | 1317 | | // @OriginalIssuer |
| | 0 | 1318 | | if (!string.IsNullOrEmpty(claim.OriginalIssuer)) |
| | | 1319 | | { |
| | 0 | 1320 | | dictionaryWriter.WriteAttributeString(dictionary.OriginalIssuer, dictionary.EmptyString, claim.Origi |
| | | 1321 | | } |
| | | 1322 | | |
| | | 1323 | | // @Type |
| | 0 | 1324 | | dictionaryWriter.WriteAttributeString(dictionary.Type, dictionary.EmptyString, claim.Type); |
| | | 1325 | | |
| | | 1326 | | // @Value |
| | 0 | 1327 | | dictionaryWriter.WriteAttributeString(dictionary.Value, dictionary.EmptyString, claim.Value); |
| | | 1328 | | |
| | | 1329 | | // @ValueType |
| | 0 | 1330 | | dictionaryWriter.WriteAttributeString(dictionary.ValueType, dictionary.EmptyString, claim.ValueType); |
| | | 1331 | | |
| | | 1332 | | // <Properties> |
| | 0 | 1333 | | if (claim.Properties != null && claim.Properties.Count > 0) |
| | | 1334 | | { |
| | 0 | 1335 | | WriteClaimProperties(dictionaryWriter, dictionary, claim.Properties); |
| | | 1336 | | } |
| | | 1337 | | |
| | 0 | 1338 | | dictionaryWriter.WriteEndElement(); |
| | | 1339 | | } |
| | 0 | 1340 | | } |
| | | 1341 | | |
| | | 1342 | | /// <summary> |
| | | 1343 | | /// Writes a collection of ClaimProperties using a XmlDictionaryWriter. |
| | | 1344 | | /// </summary> |
| | | 1345 | | /// <param name="dictionaryWriter">XmlDictionaryWriter to write to.</param> |
| | | 1346 | | /// <param name=nameof(dictionary)>SessionDictionary to provide dictionary strings.</param> |
| | | 1347 | | /// <param name=nameof(properties)>ClaimProperties to write.</param> |
| | | 1348 | | /// <exception cref="ArgumentNullException">The input argument 'dictionaryWriter', 'dictionary' or 'properties' |
| | | 1349 | | private void WriteClaimProperties(XmlDictionaryWriter dictionaryWriter, SessionDictionary dictionary, IDictionar |
| | | 1350 | | { |
| | 0 | 1351 | | if (dictionaryWriter == null) |
| | | 1352 | | { |
| | 0 | 1353 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionaryWriter)); |
| | | 1354 | | } |
| | | 1355 | | |
| | 0 | 1356 | | if (dictionary == null) |
| | | 1357 | | { |
| | 0 | 1358 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dictionary)); |
| | | 1359 | | } |
| | | 1360 | | |
| | 0 | 1361 | | if (properties == null) |
| | | 1362 | | { |
| | 0 | 1363 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(properties)); |
| | | 1364 | | } |
| | | 1365 | | |
| | 0 | 1366 | | if (properties.Count > 0) |
| | | 1367 | | { |
| | 0 | 1368 | | dictionaryWriter.WriteStartElement(dictionary.ClaimProperties, dictionary.EmptyString); |
| | | 1369 | | |
| | 0 | 1370 | | foreach (KeyValuePair<string, string> property in properties) |
| | | 1371 | | { |
| | | 1372 | | // <ClaimProperty> |
| | 0 | 1373 | | if (!string.IsNullOrEmpty(property.Key) && !string.IsNullOrEmpty(property.Value)) |
| | | 1374 | | { |
| | 0 | 1375 | | dictionaryWriter.WriteStartElement(dictionary.ClaimProperty, dictionary.EmptyString); |
| | | 1376 | | // @Name |
| | | 1377 | | |
| | 0 | 1378 | | dictionaryWriter.WriteAttributeString(dictionary.ClaimPropertyName, dictionary.EmptyString, prop |
| | | 1379 | | |
| | | 1380 | | // @Value |
| | 0 | 1381 | | dictionaryWriter.WriteAttributeString(dictionary.ClaimPropertyValue, dictionary.EmptyString, pro |
| | | 1382 | | |
| | 0 | 1383 | | dictionaryWriter.WriteEndElement(); |
| | | 1384 | | } |
| | | 1385 | | } |
| | | 1386 | | |
| | 0 | 1387 | | dictionaryWriter.WriteEndElement(); |
| | | 1388 | | } |
| | 0 | 1389 | | } |
| | | 1390 | | |
| | | 1391 | | /// <summary> |
| | | 1392 | | /// Serializes the given <see cref="CoreWCF.IdentityModel.Claims.Claim"/> to the given XmlDictionaryWriter. |
| | | 1393 | | /// </summary> |
| | | 1394 | | /// <param name="claim">The claim to be serialized.</param> |
| | | 1395 | | /// <param name="writer">The XmlDictionaryWriter to which to serialize the claim.</param> |
| | | 1396 | | private void SerializeSysClaim(SysClaim claim, XmlDictionaryWriter writer) |
| | | 1397 | | { |
| | 0 | 1398 | | SessionDictionary dictionary = SessionDictionary.Instance; |
| | | 1399 | | |
| | | 1400 | | // the order in which known claim types are checked is optimized for use patterns |
| | 0 | 1401 | | if (claim == null) |
| | | 1402 | | { |
| | 0 | 1403 | | writer.WriteElementString(dictionary.NullValue, dictionary.EmptyString, string.Empty); |
| | 0 | 1404 | | return; |
| | | 1405 | | } |
| | 0 | 1406 | | else if (SysClaimTypes.Sid.Equals(claim.ClaimType)) |
| | | 1407 | | { |
| | 0 | 1408 | | writer.WriteStartElement(dictionary.WindowsSidClaim, dictionary.EmptyString); |
| | 0 | 1409 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1410 | | SerializeSid((SecurityIdentifier)claim.Resource, dictionary, writer); |
| | 0 | 1411 | | writer.WriteEndElement(); |
| | 0 | 1412 | | return; |
| | | 1413 | | } |
| | 0 | 1414 | | else if (SysClaimTypes.DenyOnlySid.Equals(claim.ClaimType)) |
| | | 1415 | | { |
| | 0 | 1416 | | writer.WriteStartElement(dictionary.DenyOnlySidClaim, dictionary.EmptyString); |
| | 0 | 1417 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1418 | | SerializeSid((SecurityIdentifier)claim.Resource, dictionary, writer); |
| | 0 | 1419 | | writer.WriteEndElement(); |
| | 0 | 1420 | | return; |
| | | 1421 | | } |
| | 0 | 1422 | | else if (SysClaimTypes.X500DistinguishedName.Equals(claim.ClaimType)) |
| | | 1423 | | { |
| | 0 | 1424 | | writer.WriteStartElement(dictionary.X500DistinguishedNameClaim, dictionary.EmptyString); |
| | 0 | 1425 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1426 | | byte[] rawData = ((X500DistinguishedName)claim.Resource).RawData; |
| | 0 | 1427 | | writer.WriteBase64(rawData, 0, rawData.Length); |
| | 0 | 1428 | | writer.WriteEndElement(); |
| | 0 | 1429 | | return; |
| | | 1430 | | } |
| | 0 | 1431 | | else if (SysClaimTypes.Thumbprint.Equals(claim.ClaimType)) |
| | | 1432 | | { |
| | 0 | 1433 | | writer.WriteStartElement(dictionary.X509ThumbprintClaim, dictionary.EmptyString); |
| | 0 | 1434 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1435 | | byte[] thumbprint = (byte[])claim.Resource; |
| | 0 | 1436 | | writer.WriteBase64(thumbprint, 0, thumbprint.Length); |
| | 0 | 1437 | | writer.WriteEndElement(); |
| | 0 | 1438 | | return; |
| | | 1439 | | } |
| | 0 | 1440 | | else if (SysClaimTypes.Name.Equals(claim.ClaimType)) |
| | | 1441 | | { |
| | 0 | 1442 | | writer.WriteStartElement(dictionary.NameClaim, dictionary.EmptyString); |
| | 0 | 1443 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1444 | | writer.WriteString((string)claim.Resource); |
| | 0 | 1445 | | writer.WriteEndElement(); |
| | 0 | 1446 | | return; |
| | | 1447 | | } |
| | 0 | 1448 | | else if (SysClaimTypes.Dns.Equals(claim.ClaimType)) |
| | | 1449 | | { |
| | 0 | 1450 | | writer.WriteStartElement(dictionary.DnsClaim, dictionary.EmptyString); |
| | 0 | 1451 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1452 | | writer.WriteString((string)claim.Resource); |
| | 0 | 1453 | | writer.WriteEndElement(); |
| | 0 | 1454 | | return; |
| | | 1455 | | } |
| | 0 | 1456 | | else if (SysClaimTypes.Rsa.Equals(claim.ClaimType)) |
| | | 1457 | | { |
| | 0 | 1458 | | writer.WriteStartElement(dictionary.RsaClaim, dictionary.EmptyString); |
| | 0 | 1459 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1460 | | writer.WriteString(((RSA)claim.Resource).ToXmlString(false)); |
| | 0 | 1461 | | writer.WriteEndElement(); |
| | 0 | 1462 | | return; |
| | | 1463 | | } |
| | 0 | 1464 | | else if (SysClaimTypes.Email.Equals(claim.ClaimType)) |
| | | 1465 | | { |
| | 0 | 1466 | | writer.WriteStartElement(dictionary.MailAddressClaim, dictionary.EmptyString); |
| | 0 | 1467 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1468 | | writer.WriteString(((System.Net.Mail.MailAddress)claim.Resource).Address); |
| | 0 | 1469 | | writer.WriteEndElement(); |
| | 0 | 1470 | | return; |
| | | 1471 | | } |
| | 0 | 1472 | | else if (claim == SysClaim.System) |
| | | 1473 | | { |
| | 0 | 1474 | | writer.WriteElementString(dictionary.SystemClaim, dictionary.EmptyString, string.Empty); |
| | 0 | 1475 | | return; |
| | | 1476 | | } |
| | 0 | 1477 | | else if (SysClaimTypes.Hash.Equals(claim.ClaimType)) |
| | | 1478 | | { |
| | 0 | 1479 | | writer.WriteStartElement(dictionary.HashClaim, dictionary.EmptyString); |
| | 0 | 1480 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1481 | | byte[] hash = (byte[])claim.Resource; |
| | 0 | 1482 | | writer.WriteBase64(hash, 0, hash.Length); |
| | 0 | 1483 | | writer.WriteEndElement(); |
| | 0 | 1484 | | return; |
| | | 1485 | | } |
| | 0 | 1486 | | else if (SysClaimTypes.Spn.Equals(claim.ClaimType)) |
| | | 1487 | | { |
| | 0 | 1488 | | writer.WriteStartElement(dictionary.SpnClaim, dictionary.EmptyString); |
| | 0 | 1489 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1490 | | writer.WriteString((string)claim.Resource); |
| | 0 | 1491 | | writer.WriteEndElement(); |
| | 0 | 1492 | | return; |
| | | 1493 | | } |
| | 0 | 1494 | | else if (SysClaimTypes.Upn.Equals(claim.ClaimType)) |
| | | 1495 | | { |
| | 0 | 1496 | | writer.WriteStartElement(dictionary.UpnClaim, dictionary.EmptyString); |
| | 0 | 1497 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1498 | | writer.WriteString((string)claim.Resource); |
| | 0 | 1499 | | writer.WriteEndElement(); |
| | 0 | 1500 | | return; |
| | | 1501 | | } |
| | 0 | 1502 | | else if (SysClaimTypes.Uri.Equals(claim.ClaimType)) |
| | | 1503 | | { |
| | 0 | 1504 | | writer.WriteStartElement(dictionary.UrlClaim, dictionary.EmptyString); |
| | 0 | 1505 | | WriteRightAttribute(claim, dictionary, writer); |
| | 0 | 1506 | | writer.WriteString(((Uri)claim.Resource).AbsoluteUri); |
| | 0 | 1507 | | writer.WriteEndElement(); |
| | 0 | 1508 | | return; |
| | | 1509 | | } |
| | | 1510 | | else |
| | | 1511 | | { |
| | 0 | 1512 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID4290 |
| | | 1513 | | } |
| | | 1514 | | } |
| | | 1515 | | |
| | | 1516 | | /// <summary> |
| | | 1517 | | /// Deserializes a WCF claim. |
| | | 1518 | | /// </summary> |
| | | 1519 | | /// <param name="reader">XmlReader to the WCF Claim.</param> |
| | | 1520 | | /// <returns>Instance of <see cref="CoreWCF.IdentityModel.Claims.Claim"/></returns> |
| | | 1521 | | private SysClaim DeserializeSysClaim(XmlDictionaryReader reader) |
| | | 1522 | | { |
| | 0 | 1523 | | SessionDictionary dictionary = SessionDictionary.Instance; |
| | | 1524 | | |
| | 0 | 1525 | | if (reader.IsStartElement(dictionary.NullValue, dictionary.EmptyString)) |
| | | 1526 | | { |
| | 0 | 1527 | | reader.ReadElementString(); |
| | 0 | 1528 | | return null; |
| | | 1529 | | } |
| | 0 | 1530 | | else if (reader.IsStartElement(dictionary.WindowsSidClaim, dictionary.EmptyString)) |
| | | 1531 | | { |
| | 0 | 1532 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1533 | | reader.ReadStartElement(); |
| | 0 | 1534 | | byte[] sidBytes = reader.ReadContentAsBase64(); |
| | 0 | 1535 | | reader.ReadEndElement(); |
| | 0 | 1536 | | return new SysClaim(SysClaimTypes.Sid, new SecurityIdentifier(sidBytes, 0), right); |
| | | 1537 | | } |
| | 0 | 1538 | | else if (reader.IsStartElement(dictionary.DenyOnlySidClaim, dictionary.EmptyString)) |
| | | 1539 | | { |
| | 0 | 1540 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1541 | | reader.ReadStartElement(); |
| | 0 | 1542 | | byte[] sidBytes = reader.ReadContentAsBase64(); |
| | 0 | 1543 | | reader.ReadEndElement(); |
| | 0 | 1544 | | return new SysClaim(SysClaimTypes.DenyOnlySid, new SecurityIdentifier(sidBytes, 0), right); |
| | | 1545 | | } |
| | 0 | 1546 | | else if (reader.IsStartElement(dictionary.X500DistinguishedNameClaim, dictionary.EmptyString)) |
| | | 1547 | | { |
| | 0 | 1548 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1549 | | reader.ReadStartElement(); |
| | 0 | 1550 | | byte[] rawData = reader.ReadContentAsBase64(); |
| | 0 | 1551 | | reader.ReadEndElement(); |
| | 0 | 1552 | | return new SysClaim(SysClaimTypes.X500DistinguishedName, new X500DistinguishedName(rawData), right); |
| | | 1553 | | } |
| | 0 | 1554 | | else if (reader.IsStartElement(dictionary.X509ThumbprintClaim, dictionary.EmptyString)) |
| | | 1555 | | { |
| | 0 | 1556 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1557 | | reader.ReadStartElement(); |
| | 0 | 1558 | | byte[] thumbprint = reader.ReadContentAsBase64(); |
| | 0 | 1559 | | reader.ReadEndElement(); |
| | 0 | 1560 | | return new SysClaim(SysClaimTypes.Thumbprint, thumbprint, right); |
| | | 1561 | | } |
| | 0 | 1562 | | else if (reader.IsStartElement(dictionary.NameClaim, dictionary.EmptyString)) |
| | | 1563 | | { |
| | 0 | 1564 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1565 | | reader.ReadStartElement(); |
| | 0 | 1566 | | string name = reader.ReadString(); |
| | 0 | 1567 | | reader.ReadEndElement(); |
| | 0 | 1568 | | return new SysClaim(SysClaimTypes.Name, name, right); |
| | | 1569 | | } |
| | 0 | 1570 | | else if (reader.IsStartElement(dictionary.DnsClaim, dictionary.EmptyString)) |
| | | 1571 | | { |
| | 0 | 1572 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1573 | | reader.ReadStartElement(); |
| | 0 | 1574 | | string dns = reader.ReadString(); |
| | 0 | 1575 | | reader.ReadEndElement(); |
| | 0 | 1576 | | return new SysClaim(SysClaimTypes.Dns, dns, right); |
| | | 1577 | | } |
| | 0 | 1578 | | else if (reader.IsStartElement(dictionary.RsaClaim, dictionary.EmptyString)) |
| | | 1579 | | { |
| | 0 | 1580 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1581 | | reader.ReadStartElement(); |
| | 0 | 1582 | | string rsaXml = reader.ReadString(); |
| | 0 | 1583 | | reader.ReadEndElement(); |
| | | 1584 | | |
| | 0 | 1585 | | RSA rsa = RSA.Create(); |
| | 0 | 1586 | | rsa.FromXmlString(rsaXml); |
| | 0 | 1587 | | return new SysClaim(SysClaimTypes.Rsa, rsa, right); |
| | | 1588 | | } |
| | 0 | 1589 | | else if (reader.IsStartElement(dictionary.MailAddressClaim, dictionary.EmptyString)) |
| | | 1590 | | { |
| | 0 | 1591 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1592 | | reader.ReadStartElement(); |
| | 0 | 1593 | | string address = reader.ReadString(); |
| | 0 | 1594 | | reader.ReadEndElement(); |
| | 0 | 1595 | | return new SysClaim(SysClaimTypes.Email, new System.Net.Mail.MailAddress(address), right); |
| | | 1596 | | } |
| | 0 | 1597 | | else if (reader.IsStartElement(dictionary.SystemClaim, dictionary.EmptyString)) |
| | | 1598 | | { |
| | 0 | 1599 | | reader.ReadElementString(); |
| | 0 | 1600 | | return SysClaim.System; |
| | | 1601 | | } |
| | 0 | 1602 | | else if (reader.IsStartElement(dictionary.HashClaim, dictionary.EmptyString)) |
| | | 1603 | | { |
| | 0 | 1604 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1605 | | reader.ReadStartElement(); |
| | 0 | 1606 | | byte[] hash = reader.ReadContentAsBase64(); |
| | 0 | 1607 | | reader.ReadEndElement(); |
| | 0 | 1608 | | return new SysClaim(SysClaimTypes.Hash, hash, right); |
| | | 1609 | | } |
| | 0 | 1610 | | else if (reader.IsStartElement(dictionary.SpnClaim, dictionary.EmptyString)) |
| | | 1611 | | { |
| | 0 | 1612 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1613 | | reader.ReadStartElement(); |
| | 0 | 1614 | | string spn = reader.ReadString(); |
| | 0 | 1615 | | reader.ReadEndElement(); |
| | 0 | 1616 | | return new SysClaim(SysClaimTypes.Spn, spn, right); |
| | | 1617 | | } |
| | 0 | 1618 | | else if (reader.IsStartElement(dictionary.UpnClaim, dictionary.EmptyString)) |
| | | 1619 | | { |
| | 0 | 1620 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1621 | | reader.ReadStartElement(); |
| | 0 | 1622 | | string upn = reader.ReadString(); |
| | 0 | 1623 | | reader.ReadEndElement(); |
| | 0 | 1624 | | return new SysClaim(SysClaimTypes.Upn, upn, right); |
| | | 1625 | | } |
| | 0 | 1626 | | else if (reader.IsStartElement(dictionary.UrlClaim, dictionary.EmptyString)) |
| | | 1627 | | { |
| | 0 | 1628 | | string right = ReadRightAttribute(reader, dictionary); |
| | 0 | 1629 | | reader.ReadStartElement(); |
| | 0 | 1630 | | string url = reader.ReadString(); |
| | 0 | 1631 | | reader.ReadEndElement(); |
| | 0 | 1632 | | return new SysClaim(SysClaimTypes.Uri, new Uri(url), right); |
| | | 1633 | | } |
| | | 1634 | | else |
| | | 1635 | | { |
| | 0 | 1636 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.ID4289 |
| | | 1637 | | } |
| | | 1638 | | } |
| | | 1639 | | |
| | | 1640 | | private static void SerializeSid(SecurityIdentifier sid, SessionDictionary dictionary, XmlDictionaryWriter write |
| | | 1641 | | { |
| | 0 | 1642 | | byte[] sidBytes = new byte[sid.BinaryLength]; |
| | 0 | 1643 | | sid.GetBinaryForm(sidBytes, 0); |
| | 0 | 1644 | | writer.WriteBase64(sidBytes, 0, sidBytes.Length); |
| | 0 | 1645 | | } |
| | | 1646 | | |
| | | 1647 | | private static string ReadRightAttribute(XmlDictionaryReader reader, SessionDictionary dictionary) |
| | | 1648 | | { |
| | 0 | 1649 | | string right = reader.GetAttribute(dictionary.Right, dictionary.EmptyString); |
| | 0 | 1650 | | return string.IsNullOrEmpty(right) ? CoreWCF.IdentityModel.Claims.Rights.PossessProperty : right; |
| | | 1651 | | } |
| | | 1652 | | |
| | | 1653 | | private static void WriteRightAttribute(SysClaim claim, SessionDictionary dictionary, XmlDictionaryWriter writer |
| | | 1654 | | { |
| | 0 | 1655 | | if (CoreWCF.IdentityModel.Claims.Rights.PossessProperty.Equals(claim.Right)) |
| | 0 | 1656 | | return; |
| | 0 | 1657 | | writer.WriteAttributeString(dictionary.Right, dictionary.EmptyString, claim.Right); |
| | 0 | 1658 | | } |
| | | 1659 | | |
| | | 1660 | | // As name says, certainly not a complete test, but it will allow us to move forward on |
| | | 1661 | | // strings that are possible UPN's |
| | | 1662 | | // a@b will succeed |
| | | 1663 | | // @a, a@, @ will fail |
| | | 1664 | | private static bool IsPossibleUpn(string name) |
| | | 1665 | | { |
| | 0 | 1666 | | int delimiterPos = name.IndexOf('@'); |
| | | 1667 | | |
| | | 1668 | | // if it is the first of last character |
| | 0 | 1669 | | if ((name.Length < 3) || (delimiterPos < 0) || (delimiterPos == 0) || (delimiterPos == name.Length - 1)) |
| | | 1670 | | { |
| | 0 | 1671 | | return false; |
| | | 1672 | | } |
| | | 1673 | | |
| | 0 | 1674 | | return true; |
| | | 1675 | | } |
| | | 1676 | | } |
| | | 1677 | | } |