| | | 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.Globalization; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using System.Xml; |
| | | 11 | | using CoreWCF.Channels; |
| | | 12 | | using CoreWCF.Description; |
| | | 13 | | using CoreWCF.Diagnostics; |
| | | 14 | | using CoreWCF.Dispatcher; |
| | | 15 | | using CoreWCF.IdentityModel.Policy; |
| | | 16 | | using CoreWCF.IdentityModel.Tokens; |
| | | 17 | | using CoreWCF.Runtime; |
| | | 18 | | using CoreWCF.Security.Tokens; |
| | | 19 | | |
| | | 20 | | namespace CoreWCF.Security |
| | | 21 | | { |
| | | 22 | | internal abstract class NegotiationTokenAuthenticator<T> : CommunicationObjectSecurityTokenAuthenticator, IIssuanceS |
| | | 23 | | where T : NegotiationTokenAuthenticatorState |
| | | 24 | | { |
| | | 25 | | internal const string DefaultServerMaxNegotiationLifetimeString = "00:01:00"; |
| | | 26 | | internal const string DefaultServerIssuedTokenLifetimeString = "10:00:00"; |
| | | 27 | | internal const string DefaultServerIssuedTransitionTokenLifetimeString = "00:15:00"; |
| | | 28 | | internal const int DefaultServerMaxActiveNegotiations = 128; |
| | 1 | 29 | | internal static readonly TimeSpan s_defaultServerMaxNegotiationLifetime = TimeSpan.Parse(DefaultServerMaxNegotia |
| | 1 | 30 | | internal static readonly TimeSpan s_defaultServerIssuedTransitionTokenLifetime = TimeSpan.Parse(DefaultServerIss |
| | | 31 | | internal const int DefaultServerMaxCachedTokens = 1000; |
| | | 32 | | internal const bool DefaultServerMaintainState = true; |
| | 1 | 33 | | internal static readonly SecurityStandardsManager s_defaultStandardsManager = SecurityStandardsManager.DefaultIn |
| | 1 | 34 | | internal static readonly SecurityStateEncoder s_defaultSecurityStateEncoder = new DataProtectionSecurityStateEnc |
| | | 35 | | private NegotiationTokenAuthenticatorStateCache<T> _stateCache; |
| | | 36 | | private NegotiationHost _negotiationHost; |
| | | 37 | | private bool _encryptStateInServiceToken; |
| | | 38 | | private TimeSpan _serviceTokenLifetime; |
| | | 39 | | private int _maximumCachedNegotiationState; |
| | | 40 | | private TimeSpan _negotiationTimeout; |
| | | 41 | | private bool _isClientAnonymous; |
| | | 42 | | private SecurityStandardsManager _standardsManager; |
| | | 43 | | private SecurityAlgorithmSuite _securityAlgorithmSuite; |
| | | 44 | | private SecurityTokenParameters _issuedSecurityTokenParameters; |
| | | 45 | | private ISecurityContextSecurityTokenCache _issuedTokenCache; |
| | | 46 | | private BindingContext _issuerBindingContext; |
| | | 47 | | private Uri _listenUri; |
| | | 48 | | |
| | | 49 | | // AuditLogLocation auditLogLocation; |
| | | 50 | | private bool _suppressAuditFailure; |
| | | 51 | | |
| | | 52 | | // AuditLevel messageAuthenticationAuditLevel; |
| | | 53 | | private SecurityStateEncoder _securityStateEncoder; |
| | | 54 | | private SecurityContextCookieSerializer _cookieSerializer; |
| | | 55 | | private IMessageFilterTable<EndpointAddress> _endpointFilterTable; |
| | | 56 | | private int _maxMessageSize; |
| | | 57 | | private IList<Type> _knownTypes; |
| | | 58 | | private int _maximumConcurrentNegotiations; |
| | | 59 | | private List<IChannel> _activeNegotiationChannels1; |
| | | 60 | | private List<IChannel> _activeNegotiationChannels2; |
| | | 61 | | private IOThreadTimer _idlingNegotiationSessionTimer; |
| | | 62 | | private bool _isTimerCancelled; |
| | | 63 | | |
| | 2 | 64 | | protected NegotiationTokenAuthenticator() : base() => InitializeDefaults(); |
| | | 65 | | |
| | 0 | 66 | | public IssuedSecurityTokenHandler IssuedSecurityTokenHandler { get; set; } |
| | | 67 | | |
| | 0 | 68 | | public RenewedSecurityTokenHandler RenewedSecurityTokenHandler { get; set; } |
| | | 69 | | |
| | | 70 | | // settings |
| | | 71 | | public bool EncryptStateInServiceToken |
| | | 72 | | { |
| | 0 | 73 | | get => _encryptStateInServiceToken; |
| | | 74 | | set |
| | | 75 | | { |
| | 1 | 76 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 77 | | _encryptStateInServiceToken = value; |
| | 1 | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public TimeSpan ServiceTokenLifetime |
| | | 82 | | { |
| | 0 | 83 | | get => _serviceTokenLifetime; |
| | | 84 | | set |
| | | 85 | | { |
| | 1 | 86 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 87 | | if (value <= TimeSpan.Zero) |
| | | 88 | | { |
| | 0 | 89 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 90 | | } |
| | | 91 | | |
| | 1 | 92 | | if (TimeoutHelper.IsTooLarge(value)) |
| | | 93 | | { |
| | 0 | 94 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 95 | | SR.Format(SRCommon.SFxTimeoutOutOfRangeTooBig))); |
| | | 96 | | } |
| | 1 | 97 | | _serviceTokenLifetime = value; |
| | 1 | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | public int MaximumCachedNegotiationState |
| | | 102 | | { |
| | 1 | 103 | | get => _maximumCachedNegotiationState; |
| | | 104 | | set |
| | | 105 | | { |
| | 1 | 106 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 107 | | if (value < 0) |
| | | 108 | | { |
| | 0 | 109 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 110 | | } |
| | 1 | 111 | | _maximumCachedNegotiationState = value; |
| | 1 | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public int MaximumConcurrentNegotiations |
| | | 116 | | { |
| | 0 | 117 | | get => _maximumConcurrentNegotiations; |
| | | 118 | | set |
| | | 119 | | { |
| | 1 | 120 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 121 | | if (value < 0) |
| | | 122 | | { |
| | 0 | 123 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 124 | | } |
| | 1 | 125 | | _maximumConcurrentNegotiations = value; |
| | 1 | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | public TimeSpan NegotiationTimeout |
| | | 130 | | { |
| | 1 | 131 | | get => _negotiationTimeout; |
| | | 132 | | set |
| | | 133 | | { |
| | 1 | 134 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 135 | | if (value <= TimeSpan.Zero) |
| | | 136 | | { |
| | 0 | 137 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 138 | | } |
| | | 139 | | |
| | 1 | 140 | | if (TimeoutHelper.IsTooLarge(value)) |
| | | 141 | | { |
| | 0 | 142 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | 0 | 143 | | SR.Format(SRCommon.SFxTimeoutOutOfRangeTooBig))); |
| | | 144 | | } |
| | 1 | 145 | | _negotiationTimeout = value; |
| | 1 | 146 | | } |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | public bool IsClientAnonymous |
| | | 150 | | { |
| | 0 | 151 | | get => _isClientAnonymous; |
| | | 152 | | set |
| | | 153 | | { |
| | 1 | 154 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 155 | | _isClientAnonymous = value; |
| | 1 | 156 | | } |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | public SecurityAlgorithmSuite SecurityAlgorithmSuite |
| | | 160 | | { |
| | 1 | 161 | | get => _securityAlgorithmSuite; |
| | | 162 | | set |
| | | 163 | | { |
| | 1 | 164 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 165 | | _securityAlgorithmSuite = value; |
| | 1 | 166 | | } |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | public IMessageFilterTable<EndpointAddress> EndpointFilterTable |
| | | 170 | | { |
| | 0 | 171 | | get => _endpointFilterTable; |
| | | 172 | | set |
| | | 173 | | { |
| | 0 | 174 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 0 | 175 | | _endpointFilterTable = value; |
| | 0 | 176 | | } |
| | | 177 | | } |
| | | 178 | | |
| | 0 | 179 | | ISecurityContextSecurityTokenCache ISecurityContextSecurityTokenCacheProvider.TokenCache => IssuedTokenCache; |
| | | 180 | | |
| | 0 | 181 | | public virtual XmlDictionaryString RequestSecurityTokenAction => StandardsManager.TrustDriver.RequestSecurityTok |
| | | 182 | | |
| | 0 | 183 | | public virtual XmlDictionaryString RequestSecurityTokenResponseAction => StandardsManager.TrustDriver.RequestSec |
| | | 184 | | |
| | 0 | 185 | | public virtual XmlDictionaryString RequestSecurityTokenResponseFinalAction => StandardsManager.TrustDriver.Reque |
| | | 186 | | |
| | | 187 | | public SecurityStandardsManager StandardsManager |
| | | 188 | | { |
| | 1 | 189 | | get => _standardsManager; |
| | | 190 | | set |
| | | 191 | | { |
| | 1 | 192 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 193 | | _standardsManager = (value ?? SecurityStandardsManager.DefaultInstance); |
| | 1 | 194 | | } |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | public SecurityTokenParameters IssuedSecurityTokenParameters |
| | | 198 | | { |
| | 1 | 199 | | get => _issuedSecurityTokenParameters; |
| | | 200 | | set |
| | | 201 | | { |
| | 1 | 202 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 203 | | _issuedSecurityTokenParameters = value; |
| | 1 | 204 | | } |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | public ISecurityContextSecurityTokenCache IssuedTokenCache |
| | | 208 | | { |
| | 1 | 209 | | get => _issuedTokenCache; |
| | | 210 | | set |
| | | 211 | | { |
| | 1 | 212 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 213 | | _issuedTokenCache = value; |
| | 1 | 214 | | } |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | //public AuditLogLocation AuditLogLocation |
| | | 218 | | //{ |
| | | 219 | | // get |
| | | 220 | | // { |
| | | 221 | | // return this.auditLogLocation; |
| | | 222 | | // } |
| | | 223 | | // set |
| | | 224 | | // { |
| | | 225 | | // this.CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | | 226 | | // this.auditLogLocation = value; |
| | | 227 | | // } |
| | | 228 | | //} |
| | | 229 | | |
| | | 230 | | public bool SuppressAuditFailure |
| | | 231 | | { |
| | 0 | 232 | | get => _suppressAuditFailure; |
| | | 233 | | set |
| | | 234 | | { |
| | 0 | 235 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 0 | 236 | | _suppressAuditFailure = value; |
| | 0 | 237 | | } |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | //public AuditLevel MessageAuthenticationAuditLevel |
| | | 241 | | //{ |
| | | 242 | | // get |
| | | 243 | | // { |
| | | 244 | | // return this.messageAuthenticationAuditLevel; |
| | | 245 | | // } |
| | | 246 | | // set |
| | | 247 | | // { |
| | | 248 | | // this.CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | | 249 | | // this.messageAuthenticationAuditLevel = value; |
| | | 250 | | // } |
| | | 251 | | //} |
| | | 252 | | |
| | | 253 | | public BindingContext IssuerBindingContext |
| | | 254 | | { |
| | 4 | 255 | | get => _issuerBindingContext; |
| | | 256 | | set |
| | | 257 | | { |
| | 1 | 258 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 259 | | if (value == null) |
| | | 260 | | { |
| | 0 | 261 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 262 | | } |
| | 1 | 263 | | _issuerBindingContext = value.Clone(); |
| | 1 | 264 | | } |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | public Uri ListenUri |
| | | 268 | | { |
| | 1 | 269 | | get => _listenUri; |
| | | 270 | | set |
| | | 271 | | { |
| | 1 | 272 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 273 | | _listenUri = value; |
| | 1 | 274 | | } |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | public SecurityStateEncoder SecurityStateEncoder |
| | | 278 | | { |
| | 2 | 279 | | get => _securityStateEncoder; |
| | | 280 | | set |
| | | 281 | | { |
| | 1 | 282 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 283 | | _securityStateEncoder = value; |
| | 1 | 284 | | } |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | public IList<Type> KnownTypes |
| | | 288 | | { |
| | 1 | 289 | | get => _knownTypes; |
| | | 290 | | set |
| | | 291 | | { |
| | 1 | 292 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 293 | | if (value != null) |
| | | 294 | | { |
| | 1 | 295 | | _knownTypes = new Collection<Type>(value); |
| | | 296 | | } |
| | | 297 | | else |
| | | 298 | | { |
| | 0 | 299 | | _knownTypes = null; |
| | | 300 | | } |
| | 0 | 301 | | } |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | public int MaxMessageSize |
| | | 305 | | { |
| | 0 | 306 | | get => _maxMessageSize; |
| | | 307 | | set |
| | | 308 | | { |
| | 1 | 309 | | CommunicationObject.ThrowIfDisposedOrImmutable(); |
| | 1 | 310 | | _maxMessageSize = value; |
| | 1 | 311 | | } |
| | | 312 | | } |
| | | 313 | | |
| | 1 | 314 | | protected string SecurityContextTokenUri { get; private set; } |
| | | 315 | | |
| | 1 | 316 | | private object ThisLock => CommunicationObject; |
| | | 317 | | |
| | | 318 | | // helpers |
| | | 319 | | protected SecurityContextSecurityToken IssueSecurityContextToken(UniqueId contextId, string id, byte[] key, |
| | | 320 | | DateTime tokenEffectiveTime, DateTime tokenExpirationTime, |
| | 0 | 321 | | ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies, bool isCookieMode) => IssueSecurityContextTo |
| | 0 | 322 | | tokenEffectiveTime, tokenExpirationTime, authorizationPolicies, isCookieMode); |
| | | 323 | | |
| | | 324 | | protected SecurityContextSecurityToken IssueSecurityContextToken(UniqueId contextId, string id, byte[] key, |
| | | 325 | | DateTime tokenEffectiveTime, DateTime tokenExpirationTime, UniqueId keyGeneration, DateTime keyEffectiveTime |
| | | 326 | | DateTime keyExpirationTime, ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies, bool isCookieMod |
| | | 327 | | { |
| | | 328 | | // this.CommunicationObject.ThrowIfClosedOrNotOpen(); |
| | 0 | 329 | | if (_securityStateEncoder == null && isCookieMode) |
| | | 330 | | { |
| | 0 | 331 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sct |
| | | 332 | | } |
| | 0 | 333 | | byte[] cookieBlob = (isCookieMode) ? _cookieSerializer.CreateCookieFromSecurityContext(contextId, id, key, t |
| | 0 | 334 | | keyEffectiveTime, keyExpirationTime, authorizationPolicies) : null; |
| | | 335 | | |
| | 0 | 336 | | SecurityContextSecurityToken issuedToken = new SecurityContextSecurityToken(contextId, id, key, tokenEffecti |
| | 0 | 337 | | authorizationPolicies, isCookieMode, cookieBlob, keyGeneration, keyEffectiveTime, keyExpirationTime); |
| | 0 | 338 | | return issuedToken; |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | private void InitializeDefaults() |
| | | 342 | | { |
| | 1 | 343 | | _encryptStateInServiceToken = !DefaultServerMaintainState; |
| | 1 | 344 | | _serviceTokenLifetime = DefaultServerIssuedTokenLifetime; |
| | 1 | 345 | | _maximumCachedNegotiationState = DefaultServerMaxActiveNegotiations; |
| | 1 | 346 | | _negotiationTimeout = s_defaultServerMaxNegotiationLifetime; |
| | 1 | 347 | | _isClientAnonymous = false; |
| | 1 | 348 | | _standardsManager = s_defaultStandardsManager; |
| | 1 | 349 | | _securityStateEncoder = s_defaultSecurityStateEncoder; |
| | 1 | 350 | | _maximumConcurrentNegotiations = DefaultServerMaxActiveNegotiations; |
| | | 351 | | // we rely on the transport encoders to enforce the message size except in the |
| | | 352 | | // mixed mode nego case, where the client is unauthenticated and the maxMessageSize is too |
| | | 353 | | // large to be a mitigation |
| | 1 | 354 | | _maxMessageSize = int.MaxValue; |
| | 1 | 355 | | } |
| | | 356 | | |
| | | 357 | | public override Task CloseAsync(CancellationToken token) |
| | | 358 | | { |
| | 0 | 359 | | if (_negotiationHost != null) |
| | | 360 | | { |
| | 0 | 361 | | _negotiationHost = null; |
| | | 362 | | } |
| | | 363 | | |
| | 0 | 364 | | lock (ThisLock) |
| | | 365 | | { |
| | 0 | 366 | | if (_idlingNegotiationSessionTimer != null && !_isTimerCancelled) |
| | | 367 | | { |
| | 0 | 368 | | _isTimerCancelled = true; |
| | 0 | 369 | | _idlingNegotiationSessionTimer.Cancel(); |
| | | 370 | | } |
| | 0 | 371 | | } |
| | 0 | 372 | | return base.CloseAsync(token); ; |
| | | 373 | | } |
| | | 374 | | |
| | | 375 | | public override void OnAbort() |
| | | 376 | | { |
| | 0 | 377 | | if (_negotiationHost != null) |
| | | 378 | | { |
| | | 379 | | // this.negotiationHost.Abort(); |
| | 0 | 380 | | _negotiationHost = null; |
| | | 381 | | } |
| | | 382 | | |
| | 0 | 383 | | lock (ThisLock) |
| | | 384 | | { |
| | 0 | 385 | | if (_idlingNegotiationSessionTimer != null && !_isTimerCancelled) |
| | | 386 | | { |
| | 0 | 387 | | _isTimerCancelled = true; |
| | 0 | 388 | | _idlingNegotiationSessionTimer.Cancel(); |
| | | 389 | | } |
| | 0 | 390 | | } |
| | 0 | 391 | | base.OnAbort(); |
| | 0 | 392 | | } |
| | | 393 | | |
| | | 394 | | public override Task OpenAsync(CancellationToken token) |
| | | 395 | | { |
| | 1 | 396 | | if (IssuerBindingContext == null) |
| | | 397 | | { |
| | 0 | 398 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Iss |
| | | 399 | | } |
| | 1 | 400 | | if (IssuedSecurityTokenParameters == null) |
| | | 401 | | { |
| | 0 | 402 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Iss |
| | | 403 | | } |
| | 1 | 404 | | if (SecurityAlgorithmSuite == null) |
| | | 405 | | { |
| | 0 | 406 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sec |
| | | 407 | | } |
| | 1 | 408 | | if (IssuedTokenCache == null) |
| | | 409 | | { |
| | 0 | 410 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Iss |
| | | 411 | | } |
| | 1 | 412 | | SetupServiceHost(); |
| | 1 | 413 | | if (_negotiationHost != null) |
| | | 414 | | { |
| | 1 | 415 | | _negotiationHost.InitializeRuntime(); |
| | | 416 | | } |
| | | 417 | | |
| | 1 | 418 | | _stateCache = new NegotiationTokenAuthenticatorStateCache<T>(NegotiationTimeout, MaximumCachedNegotiationSta |
| | 1 | 419 | | SecurityContextTokenUri = StandardsManager.SecureConversationDriver.TokenTypeUri; |
| | 1 | 420 | | if (SecurityStateEncoder != null) |
| | | 421 | | { |
| | 1 | 422 | | _cookieSerializer = new SecurityContextCookieSerializer(SecurityStateEncoder, KnownTypes); |
| | | 423 | | } |
| | 1 | 424 | | if (_negotiationTimeout < TimeSpan.MaxValue) |
| | | 425 | | { |
| | 1 | 426 | | lock (ThisLock) |
| | | 427 | | { |
| | 1 | 428 | | _activeNegotiationChannels1 = new List<IChannel>(); |
| | 1 | 429 | | _activeNegotiationChannels2 = new List<IChannel>(); |
| | 1 | 430 | | _idlingNegotiationSessionTimer = new IOThreadTimer(new Action<object>(OnIdlingNegotiationSessionTime |
| | 1 | 431 | | _isTimerCancelled = false; |
| | 1 | 432 | | _idlingNegotiationSessionTimer.Set(_negotiationTimeout); |
| | 1 | 433 | | } |
| | | 434 | | } |
| | 1 | 435 | | return base.OpenAsync(); |
| | | 436 | | } |
| | | 437 | | |
| | 0 | 438 | | protected override bool CanValidateTokenCore(SecurityToken token) => (token is SecurityContextSecurityToken); |
| | | 439 | | |
| | | 440 | | protected override ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenCoreAsync(SecurityToken toke |
| | | 441 | | { |
| | 0 | 442 | | SecurityContextSecurityToken sct = (SecurityContextSecurityToken)token; |
| | 0 | 443 | | return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(sct.AuthorizationPolicies); |
| | | 444 | | } |
| | | 445 | | |
| | | 446 | | protected abstract Binding GetNegotiationBinding(Binding binding); |
| | | 447 | | protected abstract bool IsMultiLegNegotiation { get; } |
| | | 448 | | |
| | 2 | 449 | | internal static TimeSpan DefaultServerIssuedTokenLifetime { get; } = TimeSpan.Parse(DefaultServerIssuedTokenLife |
| | | 450 | | |
| | | 451 | | protected abstract MessageFilter GetListenerFilter(); |
| | | 452 | | |
| | | 453 | | private void SetupServiceHost() |
| | | 454 | | { |
| | | 455 | | // ChannelBuilder channelBuilder = new ChannelBuilder(this.IssuerBindingContext.Clone(), true); |
| | | 456 | | // channelBuilder.Binding.Elements.Insert(0, new ReplyAdapterBindingElement()); |
| | | 457 | | // channelBuilder.Binding = new CustomBinding(this.GetNegotiationBinding(channelBuilder.Binding)); |
| | 1 | 458 | | ChannelBuilder channelBuilder = IssuerBindingContext.BindingParameters.Find<ChannelBuilder>(); |
| | 1 | 459 | | _negotiationHost = new NegotiationHost(this, ListenUri, channelBuilder, GetListenerFilter()); |
| | 1 | 460 | | } |
| | | 461 | | |
| | | 462 | | |
| | | 463 | | // message processing abstract method |
| | | 464 | | protected abstract ValueTask<(BodyWriter, T)> ProcessRequestSecurityTokenAsync(Message request, RequestSecurityT |
| | | 465 | | protected abstract ValueTask<BodyWriter> ProcessRequestSecurityTokenResponseAsync(T negotiationState, Message re |
| | | 466 | | |
| | | 467 | | // message handlers |
| | | 468 | | protected virtual void ParseMessageBody(Message message, out string context, out RequestSecurityToken requestSec |
| | | 469 | | { |
| | 0 | 470 | | requestSecurityToken = null; |
| | 0 | 471 | | requestSecurityTokenResponse = null; |
| | 0 | 472 | | if (message.Headers.Action == RequestSecurityTokenAction.Value) |
| | | 473 | | { |
| | 0 | 474 | | XmlDictionaryReader reader = message.GetReaderAtBodyContents(); |
| | 0 | 475 | | using (reader) |
| | | 476 | | { |
| | 0 | 477 | | requestSecurityToken = RequestSecurityToken.CreateFrom(StandardsManager, reader); |
| | 0 | 478 | | message.ReadFromBodyContentsToEnd(reader); |
| | 0 | 479 | | } |
| | 0 | 480 | | context = requestSecurityToken.Context; |
| | | 481 | | } |
| | 0 | 482 | | else if (message.Headers.Action == RequestSecurityTokenResponseAction.Value) |
| | | 483 | | { |
| | 0 | 484 | | XmlDictionaryReader reader = message.GetReaderAtBodyContents(); |
| | 0 | 485 | | using (reader) |
| | | 486 | | { |
| | 0 | 487 | | requestSecurityTokenResponse = RequestSecurityTokenResponse.CreateFrom(StandardsManager, reader); |
| | 0 | 488 | | message.ReadFromBodyContentsToEnd(reader); |
| | 0 | 489 | | } |
| | 0 | 490 | | context = requestSecurityTokenResponse.Context; |
| | | 491 | | } |
| | | 492 | | else |
| | | 493 | | { |
| | 0 | 494 | | throw TraceUtility.ThrowHelperError(new SecurityNegotiationException(SR.Format(SR.InvalidActionForNegoti |
| | | 495 | | } |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | private static Message CreateReply(Message request, XmlDictionaryString action, BodyWriter body) |
| | | 499 | | { |
| | 0 | 500 | | if (request.Headers.MessageId != null) |
| | | 501 | | { |
| | 0 | 502 | | Message reply = Message.CreateMessage(request.Version, ActionHeader.Create(action, request.Version.Addre |
| | 0 | 503 | | reply.InitializeReply(request); |
| | 0 | 504 | | return reply; |
| | | 505 | | } |
| | | 506 | | else |
| | | 507 | | { |
| | | 508 | | // the message id may not be present if MapToHttp is true |
| | 0 | 509 | | return Message.CreateMessage(request.Version, ActionHeader.Create(action, request.Version.Addressing), b |
| | | 510 | | } |
| | | 511 | | } |
| | | 512 | | |
| | | 513 | | private void OnTokenIssued(SecurityToken token) |
| | | 514 | | { |
| | 0 | 515 | | IssuedSecurityTokenHandler?.Invoke(token, null); |
| | 0 | 516 | | } |
| | | 517 | | |
| | | 518 | | private void AddNegotiationChannelForIdleTracking() |
| | | 519 | | { |
| | 0 | 520 | | if (OperationContext.Current.SessionId == null) |
| | | 521 | | { |
| | 0 | 522 | | return; |
| | | 523 | | } |
| | 0 | 524 | | lock (ThisLock) |
| | | 525 | | { |
| | 0 | 526 | | if (_idlingNegotiationSessionTimer == null) |
| | | 527 | | { |
| | 0 | 528 | | return; |
| | | 529 | | } |
| | 0 | 530 | | IChannel channel = OperationContext.Current.Channel; |
| | 0 | 531 | | if (!_activeNegotiationChannels1.Contains(channel) && !_activeNegotiationChannels2.Contains(channel)) |
| | | 532 | | { |
| | 0 | 533 | | _activeNegotiationChannels1.Add(channel); |
| | | 534 | | } |
| | 0 | 535 | | if (_isTimerCancelled) |
| | | 536 | | { |
| | 0 | 537 | | _isTimerCancelled = false; |
| | 0 | 538 | | _idlingNegotiationSessionTimer.Set(_negotiationTimeout); |
| | | 539 | | } |
| | 0 | 540 | | } |
| | 0 | 541 | | } |
| | | 542 | | |
| | | 543 | | private void RemoveNegotiationChannelFromIdleTracking() |
| | | 544 | | { |
| | 0 | 545 | | if (OperationContext.Current.SessionId == null) |
| | | 546 | | { |
| | 0 | 547 | | return; |
| | | 548 | | } |
| | 0 | 549 | | lock (ThisLock) |
| | | 550 | | { |
| | 0 | 551 | | if (_idlingNegotiationSessionTimer == null) |
| | | 552 | | { |
| | 0 | 553 | | return; |
| | | 554 | | } |
| | 0 | 555 | | IChannel channel = OperationContext.Current.Channel; |
| | 0 | 556 | | _activeNegotiationChannels1.Remove(channel); |
| | 0 | 557 | | _activeNegotiationChannels2.Remove(channel); |
| | 0 | 558 | | if (_activeNegotiationChannels1.Count == 0 && _activeNegotiationChannels2.Count == 0) |
| | | 559 | | { |
| | 0 | 560 | | _isTimerCancelled = true; |
| | 0 | 561 | | _idlingNegotiationSessionTimer.Cancel(); |
| | | 562 | | } |
| | 0 | 563 | | } |
| | 0 | 564 | | } |
| | | 565 | | |
| | | 566 | | private void OnIdlingNegotiationSessionTimer(object state) |
| | | 567 | | { |
| | 0 | 568 | | lock (ThisLock) |
| | | 569 | | { |
| | 0 | 570 | | if (_isTimerCancelled || (CommunicationObject.State != CommunicationState.Opened && CommunicationObject. |
| | | 571 | | { |
| | 0 | 572 | | return; |
| | | 573 | | } |
| | | 574 | | |
| | | 575 | | try |
| | | 576 | | { |
| | 0 | 577 | | for (int i = 0; i < _activeNegotiationChannels2.Count; ++i) |
| | | 578 | | { |
| | 0 | 579 | | _activeNegotiationChannels2[i].Abort(); |
| | | 580 | | } |
| | 0 | 581 | | List<IChannel> temp = _activeNegotiationChannels2; |
| | 0 | 582 | | temp.Clear(); |
| | 0 | 583 | | _activeNegotiationChannels2 = _activeNegotiationChannels1; |
| | 0 | 584 | | _activeNegotiationChannels1 = temp; |
| | 0 | 585 | | } |
| | | 586 | | catch (Exception e) |
| | | 587 | | { |
| | 0 | 588 | | if (Fx.IsFatal(e)) |
| | | 589 | | { |
| | 0 | 590 | | throw; |
| | | 591 | | } |
| | 0 | 592 | | } |
| | | 593 | | finally |
| | | 594 | | { |
| | 0 | 595 | | if (CommunicationObject.State == CommunicationState.Opened || CommunicationObject.State == Communica |
| | | 596 | | { |
| | 0 | 597 | | if (_activeNegotiationChannels1.Count == 0 && _activeNegotiationChannels2.Count == 0) |
| | | 598 | | { |
| | 0 | 599 | | _isTimerCancelled = true; |
| | 0 | 600 | | _idlingNegotiationSessionTimer.Cancel(); |
| | | 601 | | } |
| | | 602 | | else |
| | | 603 | | { |
| | 0 | 604 | | _idlingNegotiationSessionTimer.Set(_negotiationTimeout); |
| | | 605 | | } |
| | | 606 | | } |
| | 0 | 607 | | } |
| | | 608 | | } |
| | 0 | 609 | | } |
| | | 610 | | |
| | | 611 | | private async ValueTask<Message> ProcessRequestCoreAsync(Message request) |
| | | 612 | | { |
| | 0 | 613 | | if (request == null) |
| | | 614 | | { |
| | 0 | 615 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(request)); |
| | | 616 | | } |
| | | 617 | | |
| | 0 | 618 | | bool disposeRequest = false; |
| | 0 | 619 | | bool isNegotiationFailure = true; |
| | 0 | 620 | | T negotiationState = null; |
| | | 621 | | |
| | | 622 | | try |
| | | 623 | | { |
| | | 624 | | // validate the message size if needed |
| | 0 | 625 | | if (_maxMessageSize < int.MaxValue) |
| | | 626 | | { |
| | 0 | 627 | | string action = request.Headers.Action; |
| | | 628 | | try |
| | | 629 | | { |
| | 0 | 630 | | using (MessageBuffer buffer = request.CreateBufferedCopy(_maxMessageSize)) |
| | | 631 | | { |
| | 0 | 632 | | request = buffer.CreateMessage(); |
| | 0 | 633 | | disposeRequest = true; |
| | 0 | 634 | | } |
| | 0 | 635 | | } |
| | 0 | 636 | | catch (QuotaExceededException e) |
| | | 637 | | { |
| | 0 | 638 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(SR.Fo |
| | | 639 | | } |
| | | 640 | | } |
| | | 641 | | try |
| | | 642 | | { |
| | 0 | 643 | | Uri to = request.Headers.To; |
| | 0 | 644 | | ParseMessageBody(request, out string context, out RequestSecurityToken rst, out RequestSecurityToken |
| | | 645 | | // check if there is existing state |
| | 0 | 646 | | if (context != null) |
| | | 647 | | { |
| | 0 | 648 | | negotiationState = _stateCache.GetState(context); |
| | | 649 | | } |
| | | 650 | | else |
| | | 651 | | { |
| | 0 | 652 | | negotiationState = null; |
| | | 653 | | } |
| | 0 | 654 | | bool disposeState = false; |
| | | 655 | | BodyWriter replyBody; |
| | | 656 | | try |
| | | 657 | | { |
| | 0 | 658 | | if (rst != null) |
| | | 659 | | { |
| | 0 | 660 | | if (negotiationState != null) |
| | | 661 | | { |
| | 0 | 662 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new SecurityNegotiationExcep |
| | | 663 | | } |
| | 0 | 664 | | (BodyWriter replyBody, T negotiatonState) processedRequestSecurityToken = await ProcessReque |
| | 0 | 665 | | negotiationState = processedRequestSecurityToken.negotiatonState; |
| | 0 | 666 | | replyBody = processedRequestSecurityToken.replyBody; |
| | 0 | 667 | | await using (await negotiationState.AsyncLock.TakeLockAsync()) |
| | | 668 | | { |
| | 0 | 669 | | if (negotiationState.IsNegotiationCompleted) |
| | | 670 | | { |
| | | 671 | | // if session-sct add it to cache and add a redirect header |
| | 0 | 672 | | if (!negotiationState.ServiceToken.IsCookieMode) |
| | | 673 | | { |
| | 0 | 674 | | IssuedTokenCache.AddContext(negotiationState.ServiceToken); |
| | | 675 | | } |
| | | 676 | | |
| | 0 | 677 | | OnTokenIssued(negotiationState.ServiceToken); |
| | | 678 | | // SecurityTraceRecordHelper.TraceServiceSecurityNegotiationCompleted(request, this, |
| | 0 | 679 | | disposeState = true; |
| | | 680 | | } |
| | | 681 | | else |
| | | 682 | | { |
| | 0 | 683 | | _stateCache.AddState(context, negotiationState); |
| | 0 | 684 | | disposeState = false; |
| | | 685 | | } |
| | | 686 | | |
| | 0 | 687 | | AddNegotiationChannelForIdleTracking(); |
| | | 688 | | } |
| | | 689 | | } |
| | | 690 | | else |
| | | 691 | | { |
| | 0 | 692 | | if (negotiationState == null) |
| | | 693 | | { |
| | 0 | 694 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new SecurityNegotiationExcep |
| | | 695 | | } |
| | | 696 | | |
| | 0 | 697 | | await using (await negotiationState.AsyncLock.TakeLockAsync()) |
| | | 698 | | { |
| | 0 | 699 | | replyBody = await ProcessRequestSecurityTokenResponseAsync(negotiationState, request, |
| | 0 | 700 | | rstr); //.AsTask().GetAwaiter().GetResult(); |
| | 0 | 701 | | if (negotiationState.IsNegotiationCompleted) |
| | | 702 | | { |
| | | 703 | | // if session-sct add it to cache and add a redirect header |
| | 0 | 704 | | if (!negotiationState.ServiceToken.IsCookieMode) |
| | | 705 | | { |
| | 0 | 706 | | IssuedTokenCache.AddContext(negotiationState.ServiceToken); |
| | | 707 | | } |
| | | 708 | | |
| | 0 | 709 | | OnTokenIssued(negotiationState.ServiceToken); |
| | | 710 | | // SecurityTraceRecordHelper.TraceServiceSecurityNegotiationCompleted(request, this, |
| | 0 | 711 | | disposeState = true; |
| | | 712 | | } |
| | | 713 | | else |
| | | 714 | | { |
| | 0 | 715 | | disposeState = false; |
| | | 716 | | } |
| | | 717 | | } |
| | | 718 | | } |
| | | 719 | | |
| | 0 | 720 | | if (negotiationState.IsNegotiationCompleted && null != ListenUri) |
| | | 721 | | { |
| | | 722 | | //if (AuditLevel.Success == (this.messageAuthenticationAuditLevel & AuditLevel.Success)) |
| | | 723 | | //{ |
| | | 724 | | // string primaryIdentity = negotiationState.GetRemoteIdentityName(); |
| | | 725 | | // SecurityAuditHelper.WriteSecurityNegotiationSuccessEvent(this.auditLogLocation, |
| | | 726 | | // this.suppressAuditFailure, request, request.Headers.To, request.Headers.Action, |
| | | 727 | | // primaryIdentity, this.GetType().Name); |
| | | 728 | | //} |
| | | 729 | | } |
| | 0 | 730 | | isNegotiationFailure = false; |
| | 0 | 731 | | } |
| | | 732 | | catch (Exception exception) |
| | | 733 | | { |
| | 0 | 734 | | if (Fx.IsFatal(exception)) |
| | | 735 | | { |
| | 0 | 736 | | throw; |
| | | 737 | | } |
| | | 738 | | |
| | | 739 | | // if (PerformanceCounters.PerformanceCountersEnabled && null != this.Lis |
| | | 740 | | // { |
| | | 741 | | // PerformanceCounters.AuthenticationFailed(request, this.ListenUri); |
| | | 742 | | // } |
| | | 743 | | // if (AuditLevel.Failure == (this.messageAuthenticationAuditLevel & Audi |
| | | 744 | | // { |
| | | 745 | | // try |
| | | 746 | | // { |
| | | 747 | | // string primaryIdentity = (negotiationState != null) ? negotiat |
| | | 748 | | // SecurityAuditHelper.WriteSecurityNegotiationFailureEvent(this. |
| | | 749 | | // this.suppressAuditFailure, request, request.Headers.To, re |
| | | 750 | | // primaryIdentity, this.GetType().Name, exception); |
| | | 751 | | // } |
| | | 752 | | //#pragma warning suppress 56500 |
| | | 753 | | // catch (Exception auditException) |
| | | 754 | | // { |
| | | 755 | | // if (Fx.IsFatal(auditException)) |
| | | 756 | | // throw; |
| | | 757 | | |
| | | 758 | | // DiagnosticUtility.TraceHandledException(auditException, TraceE |
| | | 759 | | // } |
| | | 760 | | // } |
| | | 761 | | |
| | 0 | 762 | | disposeState = true; |
| | 0 | 763 | | throw; |
| | | 764 | | } |
| | | 765 | | finally |
| | | 766 | | { |
| | 0 | 767 | | if (disposeState) |
| | | 768 | | { |
| | 0 | 769 | | if (negotiationState != null) |
| | | 770 | | { |
| | 0 | 771 | | if (context != null) |
| | | 772 | | { |
| | 0 | 773 | | _stateCache.RemoveState(context); |
| | | 774 | | } |
| | 0 | 775 | | negotiationState.Dispose(); |
| | | 776 | | } |
| | | 777 | | } |
| | | 778 | | } |
| | | 779 | | |
| | 0 | 780 | | return CreateReply(request, (replyBody is RequestSecurityTokenResponseCollection) ? RequestSecurityT |
| | | 781 | | } |
| | | 782 | | finally |
| | | 783 | | { |
| | 0 | 784 | | if (disposeRequest) |
| | | 785 | | { |
| | 0 | 786 | | request.Close(); |
| | | 787 | | } |
| | | 788 | | } |
| | | 789 | | } |
| | | 790 | | finally |
| | | 791 | | { |
| | 0 | 792 | | if (isNegotiationFailure) |
| | | 793 | | { |
| | 0 | 794 | | AddNegotiationChannelForIdleTracking(); |
| | | 795 | | } |
| | 0 | 796 | | else if (negotiationState != null && negotiationState.IsNegotiationCompleted) |
| | | 797 | | { |
| | 0 | 798 | | RemoveNegotiationChannelFromIdleTracking(); |
| | | 799 | | } |
| | | 800 | | } |
| | 0 | 801 | | } |
| | | 802 | | |
| | | 803 | | // negotiation failure methods |
| | | 804 | | private Message HandleNegotiationException(Message request, Exception e) => |
| | | 805 | | |
| | | 806 | | //SecurityTraceRecordHelper.TraceServiceSecurityNegotiationFailure<T>( |
| | | 807 | | // EventTraceActivityHelper.TryExtractActivity(request), |
| | | 808 | | // this, |
| | | 809 | | // e); |
| | 0 | 810 | | CreateFault(request, e); |
| | | 811 | | |
| | | 812 | | private Message CreateFault(Message request, Exception e) |
| | | 813 | | { |
| | 0 | 814 | | MessageVersion version = request.Version; |
| | | 815 | | FaultCode subCode; |
| | | 816 | | FaultReason reason; |
| | | 817 | | bool isSenderFault; |
| | 0 | 818 | | if (e is SecurityTokenValidationException || e is System.ComponentModel.Win32Exception) |
| | | 819 | | { |
| | 0 | 820 | | subCode = new FaultCode(TrustApr2004Strings.FailedAuthenticationFaultCode, TrustFeb2005Strings.Namespace |
| | 0 | 821 | | reason = new FaultReason(SR.Format(SR.FailedAuthenticationTrustFaultCode), CultureInfo.CurrentCulture); |
| | 0 | 822 | | isSenderFault = true; |
| | | 823 | | } |
| | 0 | 824 | | else if (e is QuotaExceededException) |
| | | 825 | | { |
| | | 826 | | // send a receiver fault so that the sender can retry |
| | 0 | 827 | | subCode = new FaultCode(DotNetSecurityStrings.SecurityServerTooBusyFault, DotNetSecurityStrings.Namespac |
| | 0 | 828 | | reason = new FaultReason(SR.Format(SR.NegotiationQuotasExceededFaultReason), CultureInfo.CurrentCulture) |
| | 0 | 829 | | isSenderFault = false; |
| | | 830 | | } |
| | | 831 | | else |
| | | 832 | | { |
| | 0 | 833 | | subCode = new FaultCode(TrustApr2004Strings.InvalidRequestFaultCode, TrustFeb2005Strings.Namespace); |
| | 0 | 834 | | reason = new FaultReason(SR.Format(SR.InvalidRequestTrustFaultCode), CultureInfo.CurrentCulture); |
| | 0 | 835 | | isSenderFault = true; |
| | | 836 | | } |
| | | 837 | | FaultCode faultCode; |
| | 0 | 838 | | if (isSenderFault) |
| | | 839 | | { |
| | 0 | 840 | | faultCode = FaultCode.CreateSenderFaultCode(subCode); |
| | | 841 | | } |
| | | 842 | | else |
| | | 843 | | { |
| | 0 | 844 | | faultCode = FaultCode.CreateReceiverFaultCode(subCode); |
| | | 845 | | } |
| | 0 | 846 | | MessageFault fault = MessageFault.CreateFault(faultCode, reason); |
| | 0 | 847 | | Message faultReply = Message.CreateMessage(version, fault, version.Addressing.DefaultFaultAction); |
| | 0 | 848 | | faultReply.Headers.RelatesTo = request.Headers.MessageId; |
| | | 849 | | |
| | 0 | 850 | | return faultReply; |
| | | 851 | | } |
| | | 852 | | |
| | | 853 | | private class NegotiationHost //: ServiceHostBase |
| | | 854 | | { |
| | | 855 | | private readonly NegotiationTokenAuthenticator<T> _authenticator; |
| | | 856 | | private readonly Uri _listenUri; |
| | | 857 | | private readonly ChannelBuilder _channelBuilder; |
| | | 858 | | private readonly MessageFilter _listenerFilter; |
| | | 859 | | |
| | 1 | 860 | | public NegotiationHost(NegotiationTokenAuthenticator<T> authenticator, Uri listenUri, ChannelBuilder channel |
| | | 861 | | { |
| | 1 | 862 | | _authenticator = authenticator; |
| | 1 | 863 | | _listenUri = listenUri; |
| | 1 | 864 | | _channelBuilder = channelBuilder; |
| | 1 | 865 | | _listenerFilter = listenerFilter; |
| | 1 | 866 | | } |
| | | 867 | | |
| | | 868 | | //protected override ServiceDescription CreateDescription(out IDictionary<string, ContractDescription> imple |
| | | 869 | | //{ |
| | | 870 | | // implementedContracts = null; |
| | | 871 | | // return null; |
| | | 872 | | //} |
| | | 873 | | |
| | | 874 | | internal void InitializeRuntime() |
| | | 875 | | { |
| | | 876 | | |
| | 1 | 877 | | MessageFilter contractFilter = _listenerFilter; |
| | 1 | 878 | | int filterPriority = int.MaxValue - 20; |
| | 1 | 879 | | List<Type> endpointChannelTypes = new List<Type> { typeof(IReplyChannel), |
| | 1 | 880 | | typeof(IDuplexChannel), |
| | 1 | 881 | | typeof(IReplySessionChannel), |
| | 1 | 882 | | typeof(IDuplexSessionChannel) }; |
| | 1 | 883 | | Binding binding = _authenticator.IssuerBindingContext.Binding; |
| | 1 | 884 | | var bindingQname = new XmlQualifiedName(binding.Name, binding.Namespace); |
| | 1 | 885 | | var channelDispatcher = new ChannelDispatcher(_listenUri, binding, bindingQname.ToString(), binding, end |
| | 1 | 886 | | { |
| | 1 | 887 | | MessageVersion = binding.MessageVersion, |
| | 1 | 888 | | ManualAddressing = true |
| | 1 | 889 | | }; |
| | | 890 | | //TODO : Throttle |
| | | 891 | | // channelDispatcher.ServiceThrottle = new ServiceThrottle(this); |
| | | 892 | | // channelDispatcher.ServiceThrottle.MaxConcurrentCalls = this.authenticator.MaximumConcurrentNegotiatio |
| | | 893 | | // channelDispatcher.ServiceThrottle.MaxConcurrentSessions = this.authenticator.MaximumConcurrentNegotia |
| | 1 | 894 | | EndpointDispatcher endpointDispatcher = new EndpointDispatcher(new EndpointAddress(_listenUri, new Addre |
| | 1 | 895 | | { |
| | 1 | 896 | | DispatchRuntime = { |
| | 1 | 897 | | SingletonInstanceContext = new InstanceContext( null, _authenticator, false), |
| | 1 | 898 | | ConcurrencyMode = ConcurrencyMode.Multiple |
| | 1 | 899 | | }, |
| | 1 | 900 | | AddressFilter = new MatchAllMessageFilter(), |
| | 1 | 901 | | ContractFilter = _listenerFilter, |
| | 1 | 902 | | FilterPriority = filterPriority |
| | 1 | 903 | | }; |
| | 1 | 904 | | endpointDispatcher.DispatchRuntime.PrincipalPermissionMode = PrincipalPermissionMode.None; |
| | 1 | 905 | | endpointDispatcher.DispatchRuntime.InstanceContextProvider = new SingletonInstanceContextProvider(endpoi |
| | 1 | 906 | | endpointDispatcher.DispatchRuntime.SynchronizationContext = null; |
| | 1 | 907 | | endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation = new DispatchOperation(endpointDispatcher |
| | 1 | 908 | | { |
| | 1 | 909 | | Formatter = new MessageOperationFormatter(), |
| | 1 | 910 | | Invoker = new NegotiationTokenAuthenticator<T>.NegotiationHost.NegotiationSyncInvoker(_authenticator |
| | 1 | 911 | | }; |
| | 1 | 912 | | channelDispatcher.Endpoints.Add(endpointDispatcher); |
| | 1 | 913 | | channelDispatcher.Init(); |
| | 1 | 914 | | Task openTask = channelDispatcher.OpenAsync(); |
| | | 915 | | Fx.Assert(openTask.IsCompleted, "ChannelDispatcher should open synchronously"); |
| | 1 | 916 | | openTask.GetAwaiter().GetResult(); |
| | 1 | 917 | | ServiceDispatcher service = new ServiceDispatcher(channelDispatcher); |
| | 1 | 918 | | _channelBuilder.AddServiceDispatcher<IReplyChannel>(service, new ChannelDemuxerFilter(contractFilter, fi |
| | 1 | 919 | | } |
| | | 920 | | |
| | | 921 | | private class NegotiationSyncInvoker : IOperationInvoker |
| | | 922 | | { |
| | | 923 | | private readonly NegotiationTokenAuthenticator<T> _parent; |
| | | 924 | | |
| | 2 | 925 | | internal NegotiationSyncInvoker(NegotiationTokenAuthenticator<T> parent) => _parent = parent; |
| | | 926 | | |
| | 0 | 927 | | public bool IsSynchronous => true; |
| | | 928 | | |
| | 0 | 929 | | public object[] AllocateInputs() => EmptyArray<object>.Allocate(1); |
| | | 930 | | |
| | | 931 | | public async ValueTask<(object returnValue, object[] outputs)> InvokeAsync(object instance, object[] inp |
| | | 932 | | { |
| | 0 | 933 | | object[] outputs = EmptyArray<object>.Allocate(0); |
| | 0 | 934 | | if (!(inputs[0] is Message message)) |
| | | 935 | | { |
| | 0 | 936 | | return ((object)null, outputs); |
| | | 937 | | } |
| | 0 | 938 | | object returnVal = await _parent.ProcessRequestCoreAsync(message); |
| | 0 | 939 | | return (returnVal, outputs); |
| | 0 | 940 | | } |
| | | 941 | | } |
| | | 942 | | } |
| | | 943 | | } |
| | | 944 | | } |