| | | 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.Threading.Tasks; |
| | | 6 | | using System.Xml; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Security |
| | | 10 | | { |
| | | 11 | | internal sealed class ImpersonatingMessage : Message |
| | | 12 | | { |
| | | 13 | | private readonly Message _innerMessage; |
| | | 14 | | |
| | 0 | 15 | | public ImpersonatingMessage(Message innerMessage) |
| | | 16 | | { |
| | 0 | 17 | | _innerMessage = innerMessage ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(inne |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | public override bool IsEmpty |
| | | 21 | | { |
| | | 22 | | get |
| | | 23 | | { |
| | 0 | 24 | | return _innerMessage.IsEmpty; |
| | | 25 | | } |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public override bool IsFault |
| | | 29 | | { |
| | 0 | 30 | | get { return _innerMessage.IsFault; } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public override MessageHeaders Headers |
| | | 34 | | { |
| | 0 | 35 | | get { return _innerMessage.Headers; } |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public override MessageProperties Properties |
| | | 39 | | { |
| | 0 | 40 | | get { return _innerMessage.Properties; } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public override MessageVersion Version |
| | | 44 | | { |
| | 0 | 45 | | get { return _innerMessage.Version; } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public override RecycledMessageState RecycledMessageState |
| | | 49 | | { |
| | | 50 | | get |
| | | 51 | | { |
| | 0 | 52 | | return _innerMessage.RecycledMessageState; |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | protected override void OnClose() |
| | | 57 | | { |
| | 0 | 58 | | base.OnClose(); |
| | 0 | 59 | | _innerMessage.Close(); |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | //Runs impersonated. |
| | | 63 | | public override Task OnWriteMessageAsync(XmlDictionaryWriter writer) |
| | | 64 | | { |
| | 0 | 65 | | return ImpersonateCall(() => _innerMessage.WriteMessageAsync(writer)); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | //Runs impersonated. |
| | | 69 | | protected override void OnWriteMessage(XmlDictionaryWriter writer) |
| | | 70 | | { |
| | 0 | 71 | | ImpersonateCall(() => _innerMessage.WriteMessage(writer)); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer) |
| | | 75 | | { |
| | 0 | 76 | | _innerMessage.WriteStartEnvelope(writer); |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | protected override void OnWriteStartHeaders(XmlDictionaryWriter writer) |
| | | 80 | | { |
| | 0 | 81 | | _innerMessage.WriteStartHeaders(writer); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | protected override void OnWriteStartBody(XmlDictionaryWriter writer) |
| | | 85 | | { |
| | 0 | 86 | | _innerMessage.WriteStartBody(writer); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | protected override string OnGetBodyAttribute(string localName, string ns) |
| | | 90 | | { |
| | 0 | 91 | | return _innerMessage.GetBodyAttribute(localName, ns); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | protected override MessageBuffer OnCreateBufferedCopy(int maxBufferSize) |
| | | 95 | | { |
| | 0 | 96 | | return _innerMessage.CreateBufferedCopy(maxBufferSize); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | internal override Task OnWriteBodyContentsAsync(XmlDictionaryWriter writer) |
| | | 100 | | { |
| | 0 | 101 | | return ImpersonateCall(() => _innerMessage.WriteBodyContentsAsync(writer)); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 105 | | { |
| | 0 | 106 | | ImpersonateCall(() => _innerMessage.WriteBodyContents(writer)); |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | //Runs impersonated. |
| | | 110 | | protected override void OnBodyToString(XmlDictionaryWriter writer) |
| | | 111 | | { |
| | 0 | 112 | | ImpersonateCall(() => _innerMessage.BodyToString(writer)); |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | private void ImpersonateCall(Action callToImpersonate) |
| | | 116 | | { |
| | 0 | 117 | | if (ImpersonateOnSerializingReplyMessageProperty.TryGet(_innerMessage, out ImpersonateOnSerializingReplyMess |
| | | 118 | | { |
| | 0 | 119 | | _ = impersonationProperty.RunImpersonated(() => |
| | 0 | 120 | | { |
| | 0 | 121 | | callToImpersonate(); |
| | 0 | 122 | | return (object)null; |
| | 0 | 123 | | }); |
| | | 124 | | } |
| | | 125 | | else |
| | | 126 | | { |
| | 0 | 127 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.UnableToImper |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | private T ImpersonateCall<T>(Func<T> callToImpersonate) |
| | | 132 | | { |
| | 0 | 133 | | if (ImpersonateOnSerializingReplyMessageProperty.TryGet(_innerMessage, out ImpersonateOnSerializingReplyMess |
| | | 134 | | { |
| | 0 | 135 | | return impersonationProperty.RunImpersonated(callToImpersonate); |
| | | 136 | | } |
| | | 137 | | else |
| | | 138 | | { |
| | 0 | 139 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.UnableToImper |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | } |