| | | 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.Concurrent; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Collections.ObjectModel; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Reflection; |
| | | 10 | | using System.Runtime.ExceptionServices; |
| | | 11 | | using System.Security.Claims; |
| | | 12 | | using System.Threading.Tasks; |
| | | 13 | | using CoreWCF.Channels; |
| | | 14 | | using CoreWCF.IdentityModel.Policy; |
| | | 15 | | using CoreWCF.Runtime; |
| | | 16 | | using CoreWCF.Security; |
| | | 17 | | using Microsoft.AspNetCore.Authorization; |
| | | 18 | | using Claim = CoreWCF.IdentityModel.Claims.Claim; |
| | | 19 | | |
| | | 20 | | namespace CoreWCF.Dispatcher |
| | | 21 | | { |
| | | 22 | | internal class DispatchOperationRuntime |
| | | 23 | | { |
| | | 24 | | private readonly bool _isSessionOpenNotificationEnabled; |
| | | 25 | | |
| | | 26 | | //readonly bool transactionAutoComplete; |
| | | 27 | | //readonly bool transactionRequired; |
| | | 28 | | private readonly bool _deserializeRequest; |
| | | 29 | | |
| | | 30 | | //readonly bool isInsideTransactedReceiveScope; |
| | | 31 | | |
| | 3674 | 32 | | internal DispatchOperationRuntime(DispatchOperation operation, ImmutableDispatchRuntime parent) |
| | | 33 | | { |
| | 3674 | 34 | | if (operation == null) |
| | | 35 | | { |
| | 0 | 36 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operation)); |
| | | 37 | | } |
| | | 38 | | |
| | 3674 | 39 | | if (operation.Invoker == null) |
| | | 40 | | { |
| | 0 | 41 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.RuntimeRequir |
| | | 42 | | } |
| | | 43 | | |
| | 3674 | 44 | | DisposeParameters = ((operation.AutoDisposeParameters) && (!operation.HasNoDisposableParameters)); |
| | 3674 | 45 | | Parent = parent ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parent)); |
| | 3674 | 46 | | CallContextInitializers = EmptyArray<ICallContextInitializer>.ToArray(operation.CallContextInitializers); |
| | 3674 | 47 | | ParameterInspectors = EmptyArray<IParameterInspector>.ToArray(operation.ParameterInspectors); |
| | 3674 | 48 | | FaultFormatter = operation.FaultFormatter; |
| | 3674 | 49 | | Impersonation = operation.Impersonation; |
| | 3674 | 50 | | AuthorizeClaims = operation.AuthorizeClaims; |
| | 3674 | 51 | | AuthorizationPolicy = operation.AuthorizationPolicy; |
| | 3674 | 52 | | _deserializeRequest = operation.DeserializeRequest; |
| | 3674 | 53 | | SerializeReply = operation.SerializeReply; |
| | 3674 | 54 | | Formatter = operation.Formatter; |
| | 3674 | 55 | | Invoker = operation.Invoker; |
| | 3674 | 56 | | IsTerminating = operation.IsTerminating; |
| | 3674 | 57 | | _isSessionOpenNotificationEnabled = operation.IsSessionOpenNotificationEnabled; |
| | 3674 | 58 | | Action = operation.Action; |
| | 3674 | 59 | | Name = operation.Name; |
| | 3674 | 60 | | ReleaseInstanceAfterCall = operation.ReleaseInstanceAfterCall; |
| | 3674 | 61 | | ReleaseInstanceBeforeCall = operation.ReleaseInstanceBeforeCall; |
| | 3674 | 62 | | ReplyAction = operation.ReplyAction; |
| | 3674 | 63 | | IsOneWay = operation.IsOneWay; |
| | 3674 | 64 | | ReceiveContextAcknowledgementMode = operation.ReceiveContextAcknowledgementMode; |
| | | 65 | | |
| | 3674 | 66 | | if (Formatter == null && (_deserializeRequest || SerializeReply)) |
| | | 67 | | { |
| | 0 | 68 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Dis |
| | | 69 | | } |
| | | 70 | | |
| | 3674 | 71 | | if ((operation.Parent.InstanceProvider == null) && (operation.Parent.Type != null)) |
| | | 72 | | { |
| | 966 | 73 | | if (Invoker is SyncMethodInvoker sync) |
| | | 74 | | { |
| | 3 | 75 | | ValidateInstanceType(operation.Parent.Type, sync.Method); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | //AsyncMethodInvoker async = this.invoker as AsyncMethodInvoker; |
| | | 79 | | //if (async != null) |
| | | 80 | | //{ |
| | | 81 | | // this.ValidateInstanceType(operation.Parent.Type, async.BeginMethod); |
| | | 82 | | // this.ValidateInstanceType(operation.Parent.Type, async.EndMethod); |
| | | 83 | | //} |
| | | 84 | | |
| | 966 | 85 | | if (Invoker is TaskMethodInvoker task) |
| | | 86 | | { |
| | 0 | 87 | | ValidateInstanceType(operation.Parent.Type, task.TaskMethod); |
| | | 88 | | } |
| | | 89 | | } |
| | 3674 | 90 | | } |
| | | 91 | | |
| | 2 | 92 | | internal string Action { get; } |
| | | 93 | | |
| | 5070 | 94 | | internal ICallContextInitializer[] CallContextInitializers { get; } |
| | | 95 | | |
| | 2538 | 96 | | internal bool DisposeParameters { get; } |
| | | 97 | | |
| | | 98 | | internal bool HasDefaultUnhandledActionInvoker |
| | | 99 | | { |
| | 0 | 100 | | get { return (Invoker is DispatchRuntime.UnhandledActionInvoker); } |
| | | 101 | | } |
| | | 102 | | |
| | 681 | 103 | | internal bool SerializeReply { get; } |
| | | 104 | | |
| | 103 | 105 | | internal IDispatchFaultFormatter FaultFormatter { get; } |
| | | 106 | | |
| | 6763 | 107 | | internal IDispatchMessageFormatter Formatter { get; } |
| | | 108 | | |
| | 4972 | 109 | | internal ImpersonationOption Impersonation { get; } |
| | | 110 | | |
| | 2524 | 111 | | internal ConcurrentDictionary<string, List<Claim>> AuthorizeClaims { get; } |
| | | 112 | | |
| | 55 | 113 | | internal Lazy<AuthorizationPolicy> AuthorizationPolicy { get; } |
| | | 114 | | |
| | 6935 | 115 | | internal IOperationInvoker Invoker { get; } |
| | | 116 | | |
| | 15065 | 117 | | internal bool IsOneWay { get; } |
| | | 118 | | |
| | 2549 | 119 | | internal bool IsTerminating { get; } |
| | | 120 | | |
| | 0 | 121 | | internal string Name { get; } |
| | | 122 | | |
| | 4879 | 123 | | internal IParameterInspector[] ParameterInspectors { get; } |
| | | 124 | | |
| | 17050 | 125 | | internal ImmutableDispatchRuntime Parent { get; } |
| | | 126 | | |
| | 0 | 127 | | internal ReceiveContextAcknowledgementMode ReceiveContextAcknowledgementMode { get; } |
| | | 128 | | |
| | 2536 | 129 | | internal bool ReleaseInstanceAfterCall { get; } |
| | | 130 | | |
| | 2536 | 131 | | internal bool ReleaseInstanceBeforeCall { get; } |
| | | 132 | | |
| | 10 | 133 | | internal string ReplyAction { get; } |
| | | 134 | | |
| | | 135 | | //internal bool TransactionAutoComplete |
| | | 136 | | //{ |
| | | 137 | | // get { return this.transactionAutoComplete; } |
| | | 138 | | //} |
| | | 139 | | |
| | | 140 | | //internal bool TransactionRequired |
| | | 141 | | //{ |
| | | 142 | | // get { return this.transactionRequired; } |
| | | 143 | | //} |
| | | 144 | | |
| | | 145 | | //internal bool IsInsideTransactedReceiveScope |
| | | 146 | | //{ |
| | | 147 | | // get { return this.isInsideTransactedReceiveScope; } |
| | | 148 | | //} |
| | | 149 | | |
| | | 150 | | private void DeserializeInputs(MessageRpc rpc) |
| | | 151 | | { |
| | | 152 | | //bool success = false; |
| | | 153 | | try |
| | | 154 | | { |
| | | 155 | | try |
| | | 156 | | { |
| | 2505 | 157 | | rpc.InputParameters = Invoker.AllocateInputs(); |
| | 2505 | 158 | | } |
| | 0 | 159 | | catch (Exception e) |
| | | 160 | | { |
| | 0 | 161 | | if (Fx.IsFatal(e)) |
| | | 162 | | { |
| | 0 | 163 | | throw; |
| | | 164 | | } |
| | 0 | 165 | | if (ErrorBehavior.ShouldRethrowExceptionAsIs(e)) |
| | | 166 | | { |
| | 0 | 167 | | throw; |
| | | 168 | | } |
| | 0 | 169 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 170 | | } |
| | | 171 | | try |
| | | 172 | | { |
| | | 173 | | // If the field is true, then this operation is to be invoked at the time the service |
| | | 174 | | // channel is opened. The incoming message is created at ChannelHandler level with no |
| | | 175 | | // content, so we don't need to deserialize the message. |
| | 2505 | 176 | | if (!_isSessionOpenNotificationEnabled) |
| | | 177 | | { |
| | 2504 | 178 | | if (_deserializeRequest) |
| | | 179 | | { |
| | | 180 | | //if (TD.DispatchFormatterDeserializeRequestStartIsEnabled()) |
| | | 181 | | //{ |
| | | 182 | | // TD.DispatchFormatterDeserializeRequestStart(rpc.EventTraceActivity); |
| | | 183 | | //} |
| | | 184 | | |
| | 2481 | 185 | | Formatter.DeserializeRequest(rpc.Request, rpc.InputParameters); |
| | | 186 | | |
| | | 187 | | //if (TD.DispatchFormatterDeserializeRequestStopIsEnabled()) |
| | | 188 | | //{ |
| | | 189 | | // TD.DispatchFormatterDeserializeRequestStop(rpc.EventTraceActivity); |
| | | 190 | | //} |
| | | 191 | | } |
| | | 192 | | else |
| | | 193 | | { |
| | 23 | 194 | | rpc.InputParameters[0] = rpc.Request; |
| | | 195 | | } |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | //success = true; |
| | 2504 | 199 | | } |
| | 1 | 200 | | catch (Exception e) |
| | | 201 | | { |
| | 1 | 202 | | if (Fx.IsFatal(e)) |
| | | 203 | | { |
| | 0 | 204 | | throw; |
| | | 205 | | } |
| | 1 | 206 | | if (ErrorBehavior.ShouldRethrowExceptionAsIs(e)) |
| | | 207 | | { |
| | 1 | 208 | | throw; |
| | | 209 | | } |
| | 0 | 210 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | finally |
| | | 214 | | { |
| | 2505 | 215 | | rpc.DidDeserializeRequestBody = (rpc.Request.State != MessageState.Created); |
| | | 216 | | |
| | | 217 | | //if (!success && MessageLogger.LoggingEnabled) |
| | | 218 | | //{ |
| | | 219 | | // MessageLogger.LogMessage(rpc.Request, MessageLoggingSource.Malformed); |
| | | 220 | | //} |
| | 2505 | 221 | | } |
| | 2504 | 222 | | } |
| | | 223 | | |
| | | 224 | | private void InitializeCallContext(MessageRpc rpc) |
| | | 225 | | { |
| | 2505 | 226 | | if (CallContextInitializers.Length > 0) |
| | | 227 | | { |
| | 12 | 228 | | InitializeCallContextCore(rpc); |
| | | 229 | | } |
| | 2505 | 230 | | } |
| | | 231 | | |
| | | 232 | | private void InitializeCallContextCore(MessageRpc rpc) |
| | | 233 | | { |
| | 12 | 234 | | IClientChannel channel = rpc.Channel.Proxy as IClientChannel; |
| | 12 | 235 | | int offset = Parent.CallContextCorrelationOffset; |
| | | 236 | | |
| | | 237 | | try |
| | | 238 | | { |
| | 48 | 239 | | for (int i = 0; i < rpc.Operation.CallContextInitializers.Length; i++) |
| | | 240 | | { |
| | 12 | 241 | | ICallContextInitializer initializer = CallContextInitializers[i]; |
| | 12 | 242 | | rpc.Correlation[offset + i] = initializer.BeforeInvoke(rpc.InstanceContext, channel, rpc.Request); |
| | | 243 | | } |
| | 12 | 244 | | } |
| | 0 | 245 | | catch (Exception e) |
| | | 246 | | { |
| | 0 | 247 | | if (Fx.IsFatal(e)) |
| | | 248 | | { |
| | 0 | 249 | | throw; |
| | | 250 | | } |
| | 0 | 251 | | if (ErrorBehavior.ShouldRethrowExceptionAsIs(e)) |
| | | 252 | | { |
| | 0 | 253 | | throw; |
| | | 254 | | } |
| | 0 | 255 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 256 | | } |
| | 12 | 257 | | } |
| | | 258 | | |
| | | 259 | | private void UninitializeCallContext(MessageRpc rpc) |
| | | 260 | | { |
| | 2505 | 261 | | if (CallContextInitializers.Length > 0) |
| | | 262 | | { |
| | 12 | 263 | | UninitializeCallContextCore(rpc); |
| | | 264 | | } |
| | 2505 | 265 | | } |
| | | 266 | | |
| | | 267 | | private void UninitializeCallContextCore(MessageRpc rpc) |
| | | 268 | | { |
| | 12 | 269 | | IClientChannel channel = rpc.Channel.Proxy as IClientChannel; |
| | 12 | 270 | | int offset = Parent.CallContextCorrelationOffset; |
| | | 271 | | |
| | | 272 | | try |
| | | 273 | | { |
| | 48 | 274 | | for (int i = CallContextInitializers.Length - 1; i >= 0; i--) |
| | | 275 | | { |
| | 12 | 276 | | ICallContextInitializer initializer = CallContextInitializers[i]; |
| | 12 | 277 | | initializer.AfterInvoke(rpc.Correlation[offset + i]); |
| | | 278 | | } |
| | 12 | 279 | | } |
| | 0 | 280 | | catch (Exception e) |
| | | 281 | | { |
| | | 282 | | // thread-local storage may be corrupt |
| | 0 | 283 | | DiagnosticUtility.FailFast(string.Format(CultureInfo.InvariantCulture, "ICallContextInitializer.BeforeIn |
| | 0 | 284 | | } |
| | 12 | 285 | | } |
| | | 286 | | |
| | | 287 | | private void InspectInputs(MessageRpc rpc) |
| | | 288 | | { |
| | 2504 | 289 | | if (ParameterInspectors.Length > 0) |
| | | 290 | | { |
| | 0 | 291 | | InspectInputsCore(rpc); |
| | | 292 | | } |
| | 2504 | 293 | | } |
| | | 294 | | |
| | | 295 | | private void InspectInputsCore(MessageRpc rpc) |
| | | 296 | | { |
| | 0 | 297 | | int offset = Parent.ParameterInspectorCorrelationOffset; |
| | | 298 | | |
| | 0 | 299 | | for (int i = 0; i < ParameterInspectors.Length; i++) |
| | | 300 | | { |
| | 0 | 301 | | IParameterInspector inspector = ParameterInspectors[i]; |
| | 0 | 302 | | rpc.Correlation[offset + i] = inspector.BeforeCall(Name, rpc.InputParameters); |
| | | 303 | | //if (TD.ParameterInspectorBeforeCallInvokedIsEnabled()) |
| | | 304 | | //{ |
| | | 305 | | // TD.ParameterInspectorBeforeCallInvoked(rpc.EventTraceActivity, this.ParameterInspectors[i].GetType |
| | | 306 | | //} |
| | | 307 | | } |
| | 0 | 308 | | } |
| | | 309 | | |
| | | 310 | | private void InspectOutputs(MessageRpc rpc) |
| | | 311 | | { |
| | 2375 | 312 | | if (ParameterInspectors.Length > 0) |
| | | 313 | | { |
| | 0 | 314 | | InspectOutputsCore(rpc); |
| | | 315 | | } |
| | 2375 | 316 | | } |
| | | 317 | | |
| | | 318 | | private void InspectOutputsCore(MessageRpc rpc) |
| | | 319 | | { |
| | 0 | 320 | | int offset = Parent.ParameterInspectorCorrelationOffset; |
| | | 321 | | |
| | 0 | 322 | | for (int i = ParameterInspectors.Length - 1; i >= 0; i--) |
| | | 323 | | { |
| | 0 | 324 | | IParameterInspector inspector = ParameterInspectors[i]; |
| | 0 | 325 | | inspector.AfterCall(Name, rpc.OutputParameters, rpc.ReturnParameter, rpc.Correlation[offset + i]); |
| | | 326 | | //if (TD.ParameterInspectorAfterCallInvokedIsEnabled()) |
| | | 327 | | //{ |
| | | 328 | | // TD.ParameterInspectorAfterCallInvoked(rpc.EventTraceActivity, this.ParameterInspectors[i].GetType( |
| | | 329 | | //} |
| | | 330 | | } |
| | 0 | 331 | | } |
| | | 332 | | |
| | | 333 | | internal async Task<MessageRpc> InvokeAsync(MessageRpc rpc) |
| | | 334 | | { |
| | 2505 | 335 | | if (rpc.Error == null) |
| | | 336 | | { |
| | | 337 | | try |
| | | 338 | | { |
| | 2505 | 339 | | InitializeCallContext(rpc); |
| | 2505 | 340 | | object target = rpc.Instance; |
| | 2505 | 341 | | DeserializeInputs(rpc); |
| | 2504 | 342 | | InspectInputs(rpc); |
| | 2504 | 343 | | ValidateMustUnderstand(rpc); |
| | 2504 | 344 | | ValidateAuthorizedClaims(rpc); |
| | | 345 | | |
| | 2498 | 346 | | if (Parent.RequireClaimsPrincipalOnOperationContext) |
| | | 347 | | { |
| | 25 | 348 | | SetClaimsPrincipalToOperationContext(rpc); |
| | | 349 | | } |
| | | 350 | | |
| | 2498 | 351 | | if (Parent.SecurityImpersonation != null) |
| | | 352 | | { |
| | 2486 | 353 | | await Parent.SecurityImpersonation.RunImpersonated(rpc, async () => |
| | 2486 | 354 | | { |
| | 2486 | 355 | | (rpc.ReturnParameter, rpc.OutputParameters) = await Invoker.InvokeAsync(target, rpc.InputPar |
| | 4849 | 356 | | }); |
| | | 357 | | } |
| | | 358 | | else |
| | | 359 | | { |
| | 12 | 360 | | (rpc.ReturnParameter, rpc.OutputParameters) = await Invoker.InvokeAsync(target, rpc.InputParamet |
| | | 361 | | } |
| | | 362 | | |
| | 2375 | 363 | | InspectOutputs(rpc); |
| | 2375 | 364 | | SerializeOutputs(rpc); |
| | 2375 | 365 | | } |
| | | 366 | | #pragma warning disable CA1031 // Do not catch general exception types |
| | | 367 | | catch (Exception ex) |
| | | 368 | | { |
| | 130 | 369 | | ExceptionDispatchInfo.Capture(ex).Throw(); |
| | 0 | 370 | | } // Make sure user Exception filters are not impersonated. |
| | | 371 | | #pragma warning restore CA1031 // Do not catch general exception types |
| | | 372 | | finally |
| | | 373 | | { |
| | 2505 | 374 | | UninitializeCallContext(rpc); |
| | | 375 | | } |
| | | 376 | | } |
| | | 377 | | |
| | 2375 | 378 | | return rpc; |
| | 2375 | 379 | | } |
| | | 380 | | |
| | | 381 | | internal void SetClaimsPrincipalToOperationContext(MessageRpc rpc) |
| | | 382 | | { |
| | 80 | 383 | | ServiceSecurityContext securityContext = rpc.SecurityContext; |
| | 80 | 384 | | if (!rpc.HasSecurityContext) |
| | | 385 | | { |
| | 80 | 386 | | SecurityMessageProperty securityContextProperty = rpc.Request.Properties.Security; |
| | 80 | 387 | | if (securityContextProperty != null) |
| | | 388 | | { |
| | 80 | 389 | | securityContext = securityContextProperty.ServiceSecurityContext; |
| | | 390 | | } |
| | | 391 | | } |
| | | 392 | | |
| | 80 | 393 | | if (securityContext != null) |
| | | 394 | | { |
| | | 395 | | object principal; |
| | 80 | 396 | | if (securityContext.AuthorizationContext.Properties.TryGetValue(IdentityModel.Tokens.AuthorizationPolicy |
| | | 397 | | { |
| | 80 | 398 | | ClaimsPrincipal claimsPrincipal = principal as ClaimsPrincipal; |
| | 80 | 399 | | if (claimsPrincipal != null) |
| | | 400 | | { |
| | | 401 | | // |
| | | 402 | | // Always set ClaimsPrincipal to OperationContext.Current if identityModel pipeline is used. |
| | | 403 | | // |
| | 80 | 404 | | OperationContext.Current.ClaimsPrincipal = claimsPrincipal; |
| | | 405 | | } |
| | | 406 | | else |
| | | 407 | | { |
| | 0 | 408 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.NoPri |
| | | 409 | | } |
| | | 410 | | } |
| | | 411 | | } |
| | 0 | 412 | | } |
| | | 413 | | |
| | | 414 | | private void SerializeOutputs(MessageRpc rpc) |
| | | 415 | | { |
| | 2375 | 416 | | if (!IsOneWay && Parent.EnableFaults) |
| | | 417 | | { |
| | | 418 | | Message reply; |
| | 618 | 419 | | if (SerializeReply) |
| | | 420 | | { |
| | | 421 | | try |
| | | 422 | | { |
| | | 423 | | //if (TD.DispatchFormatterSerializeReplyStartIsEnabled()) |
| | | 424 | | //{ |
| | | 425 | | // TD.DispatchFormatterSerializeReplyStart(rpc.EventTraceActivity); |
| | | 426 | | //} |
| | | 427 | | |
| | 608 | 428 | | reply = Formatter.SerializeReply(rpc.RequestVersion, rpc.OutputParameters, rpc.ReturnParameter); |
| | | 429 | | |
| | | 430 | | //if (TD.DispatchFormatterSerializeReplyStopIsEnabled()) |
| | | 431 | | //{ |
| | | 432 | | // TD.DispatchFormatterSerializeReplyStop(rpc.EventTraceActivity); |
| | | 433 | | //} |
| | 608 | 434 | | } |
| | 0 | 435 | | catch (Exception e) |
| | | 436 | | { |
| | 0 | 437 | | if (Fx.IsFatal(e)) |
| | | 438 | | { |
| | 0 | 439 | | throw; |
| | | 440 | | } |
| | 0 | 441 | | if (ErrorBehavior.ShouldRethrowExceptionAsIs(e)) |
| | | 442 | | { |
| | 0 | 443 | | throw; |
| | | 444 | | } |
| | 0 | 445 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 446 | | } |
| | | 447 | | |
| | 608 | 448 | | if (reply == null) |
| | | 449 | | { |
| | 0 | 450 | | string message = SR.Format(SR.SFxNullReplyFromFormatter2, Formatter.GetType().ToString(), (Name |
| | 0 | 451 | | ErrorBehavior.ThrowAndCatch(new InvalidOperationException(message)); |
| | | 452 | | } |
| | | 453 | | } |
| | | 454 | | else |
| | | 455 | | { |
| | 10 | 456 | | if ((rpc.ReturnParameter == null) && (rpc.OperationContext.RequestContext != null)) |
| | | 457 | | { |
| | 0 | 458 | | string message = SR.Format(SR.SFxDispatchRuntimeMessageCannotBeNull, Name); |
| | 0 | 459 | | ErrorBehavior.ThrowAndCatch(new InvalidOperationException(message)); |
| | | 460 | | } |
| | | 461 | | |
| | 10 | 462 | | reply = (Message)rpc.ReturnParameter; |
| | | 463 | | |
| | 10 | 464 | | if ((reply != null) && (!ProxyOperationRuntime.IsValidAction(reply, ReplyAction))) |
| | | 465 | | { |
| | 0 | 466 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 467 | | } |
| | | 468 | | } |
| | | 469 | | |
| | | 470 | | //if (DiagnosticUtility.ShouldUseActivity && rpc.Activity != null && reply != null) |
| | | 471 | | //{ |
| | | 472 | | // TraceUtility.SetActivity(reply, rpc.Activity); |
| | | 473 | | // if (TraceUtility.ShouldPropagateActivity) |
| | | 474 | | // { |
| | | 475 | | // TraceUtility.AddActivityHeader(reply); |
| | | 476 | | // } |
| | | 477 | | //} |
| | | 478 | | //else if (TraceUtility.ShouldPropagateActivity && reply != null && rpc.ResponseActivityId != Guid.Empty |
| | | 479 | | //{ |
| | | 480 | | // ActivityIdHeader header = new ActivityIdHeader(rpc.ResponseActivityId); |
| | | 481 | | // header.AddTo(reply); |
| | | 482 | | //} |
| | | 483 | | |
| | | 484 | | //rely on the property set during the message receive to correlate the trace |
| | | 485 | | //if (TraceUtility.MessageFlowTracingOnly) |
| | | 486 | | //{ |
| | | 487 | | // //Guard against MEX scenarios where the message is closed by now |
| | | 488 | | // if (null != rpc.OperationContext.IncomingMessage && MessageState.Closed != rpc.OperationContext.In |
| | | 489 | | // { |
| | | 490 | | // FxTrace.Trace.SetAndTraceTransfer(TraceUtility.GetReceivedActivityId(rpc.OperationContext), tr |
| | | 491 | | // } |
| | | 492 | | // else |
| | | 493 | | // { |
| | | 494 | | // if (rpc.ResponseActivityId != Guid.Empty) |
| | | 495 | | // { |
| | | 496 | | // FxTrace.Trace.SetAndTraceTransfer(rpc.ResponseActivityId, true); |
| | | 497 | | // } |
| | | 498 | | // } |
| | | 499 | | //} |
| | | 500 | | |
| | | 501 | | // Add the ImpersonateOnSerializingReplyMessageProperty on the reply message iff |
| | | 502 | | // a. reply message is not null. |
| | | 503 | | // b. Impersonation is enabled on serializing Reply |
| | | 504 | | |
| | 618 | 505 | | if (reply != null && Parent.IsImpersonationEnabledOnSerializingReply) |
| | | 506 | | { |
| | 0 | 507 | | bool shouldImpersonate = Parent.SecurityImpersonation != null && Parent.SecurityImpersonation.IsImpe |
| | 0 | 508 | | if (shouldImpersonate) |
| | | 509 | | { |
| | 0 | 510 | | reply.Properties.Add(ImpersonateOnSerializingReplyMessageProperty.Name, new ImpersonateOnSeriali |
| | 0 | 511 | | reply = new ImpersonatingMessage(reply); |
| | | 512 | | } |
| | | 513 | | } |
| | | 514 | | |
| | | 515 | | //if (MessageLogger.LoggingEnabled && null != reply) |
| | | 516 | | //{ |
| | | 517 | | // MessageLogger.LogMessage(ref reply, MessageLoggingSource.ServiceLevelSendReply | MessageLoggingSou |
| | | 518 | | //} |
| | 618 | 519 | | rpc.Reply = reply; |
| | | 520 | | } |
| | 2375 | 521 | | } |
| | | 522 | | |
| | | 523 | | private void ValidateInstanceType(Type type, MethodInfo method) |
| | | 524 | | { |
| | 3 | 525 | | if (!method.DeclaringType.IsAssignableFrom(type)) |
| | | 526 | | { |
| | 0 | 527 | | string message = SR.Format(SR.SFxMethodNotSupportedByType2, type.FullName, |
| | 0 | 528 | | method.DeclaringType.FullName); |
| | | 529 | | |
| | 0 | 530 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(message)); |
| | | 531 | | } |
| | 3 | 532 | | } |
| | | 533 | | |
| | | 534 | | private void ValidateMustUnderstand(MessageRpc rpc) |
| | | 535 | | { |
| | 2504 | 536 | | if (Parent.ValidateMustUnderstand) |
| | | 537 | | { |
| | 2504 | 538 | | rpc.NotUnderstoodHeaders = rpc.Request.Headers.GetHeadersNotUnderstood(); |
| | 2504 | 539 | | if (rpc.NotUnderstoodHeaders != null) |
| | | 540 | | { |
| | 0 | 541 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 542 | | new MustUnderstandSoapException(rpc.NotUnderstoodHeaders, rpc.Request.Version.Envelope)); |
| | | 543 | | } |
| | | 544 | | } |
| | 2504 | 545 | | } |
| | | 546 | | |
| | | 547 | | private void ValidateAuthorizedClaims(MessageRpc rpc) |
| | | 548 | | { |
| | 2504 | 549 | | if (AuthorizeClaims.IsEmpty || AuthorizeClaims.Keys.Count == 0) |
| | | 550 | | { |
| | 2494 | 551 | | return; |
| | | 552 | | } |
| | | 553 | | |
| | 10 | 554 | | if (rpc.OperationContext?.ServiceSecurityContext?.AuthorizationContext == null) |
| | | 555 | | { |
| | 0 | 556 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(AuthorizationBehavior.CreateAccessDeniedFaultE |
| | | 557 | | } |
| | | 558 | | |
| | 36 | 559 | | foreach (var eachAuthClaim in AuthorizeClaims) |
| | | 560 | | { |
| | 11 | 561 | | List<Claim> allClaims = eachAuthClaim.Value; |
| | 11 | 562 | | if(!IsClaimFound(rpc.OperationContext?.ServiceSecurityContext.AuthorizationContext.ClaimSets, allClaims) |
| | | 563 | | { |
| | 6 | 564 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(AuthorizationBehavior.CreateAccessDeniedFa |
| | | 565 | | } |
| | | 566 | | } |
| | 4 | 567 | | } |
| | | 568 | | |
| | | 569 | | private bool IsClaimFound(ReadOnlyCollection<IdentityModel.Claims.ClaimSet> claimSets, List<Claim> anyClaims) |
| | | 570 | | { |
| | 43 | 571 | | foreach (var claim in anyClaims) |
| | | 572 | | { |
| | 47 | 573 | | foreach (var claimSet in claimSets) |
| | | 574 | | { |
| | 13 | 575 | | if (claimSet.ContainsClaim(claim)) |
| | | 576 | | { |
| | 5 | 577 | | return true; |
| | | 578 | | } |
| | | 579 | | } |
| | | 580 | | } |
| | | 581 | | |
| | 6 | 582 | | return false; |
| | 5 | 583 | | } |
| | | 584 | | } |
| | | 585 | | } |