1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Security
{
using System.Security.Principal;
using System.ServiceModel.Channels;
using System.Xml;
sealed class ImpersonatingMessage : Message
{
Message innerMessage;
public ImpersonatingMessage(Message innerMessage)
{
if (innerMessage == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("innerMessage");
}
this.innerMessage = innerMessage;
}
public override bool IsEmpty
{
get
{
return this.innerMessage.IsEmpty;
}
}
public override bool IsFault
{
get { return this.innerMessage.IsFault; }
}
public override MessageHeaders Headers
{
get { return this.innerMessage.Headers; }
}
public override MessageProperties Properties
{
get { return this.innerMessage.Properties; }
}
public override MessageVersion Version
{
get { return this.innerMessage.Version; }
}
internal override RecycledMessageState RecycledMessageState
{
get
{
return this.innerMessage.RecycledMessageState;
}
}
protected override void OnClose()
{
base.OnClose();
this.innerMessage.Close();
}
//Runs impersonated.
protected override IAsyncResult OnBeginWriteMessage(XmlDictionaryWriter writer, AsyncCallback callback, object state)
{
ImpersonateOnSerializingReplyMessageProperty impersonationProperty = null;
IDisposable impersonationContext = null;
IPrincipal originalPrincipal = null;
bool isThreadPrincipalSet = false;
if (!ImpersonateOnSerializingReplyMessageProperty.TryGet(this.innerMessage, out impersonationProperty))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.UnableToImpersonateWhileSerializingReponse)));
}
try
{
impersonationProperty.StartImpersonation(out impersonationContext, out originalPrincipal, out isThreadPrincipalSet);
return this.innerMessage.BeginWriteMessage(writer, callback, state);
}
finally
{
try
{
impersonationProperty.StopImpersonation(impersonationContext, originalPrincipal, isThreadPrincipalSet);
}
#pragma warning suppress 56500 // covered by FxCOP
catch
{
string message = null;
try
{
message = SR.GetString(SR.SFxRevertImpersonationFailed0);
}
finally
{
DiagnosticUtility.FailFast(message);
}
}
}
}
//Runs impersonated.
protected override void OnWriteMessage(XmlDictionaryWriter writer)
{
this.ImpersonateCall(() => this.innerMessage.WriteMessage(writer));
}
protected override void OnEndWriteMessage(IAsyncResult result)
{
this.innerMessage.EndWriteMessage(result);
}
protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer)
{
this.innerMessage.WriteStartEnvelope(writer);
}
protected override void OnWriteStartHeaders(XmlDictionaryWriter writer)
{
this.innerMessage.WriteStartHeaders(writer);
}
protected override void OnWriteStartBody(XmlDictionaryWriter writer)
{
this.innerMessage.WriteStartBody(writer);
}
protected override string OnGetBodyAttribute(string localName, string ns)
{
return this.innerMessage.GetBodyAttribute(localName, ns);
}
protected override MessageBuffer OnCreateBufferedCopy(int maxBufferSize)
{
return this.innerMessage.CreateBufferedCopy(maxBufferSize);
}
protected override IAsyncResult OnBeginWriteBodyContents(XmlDictionaryWriter writer, AsyncCallback callback, object state)
{
ImpersonateOnSerializingReplyMessageProperty impersonationProperty = null;
IDisposable impersonationContext = null;
IPrincipal originalPrincipal = null;
bool isThreadPrincipalSet = false;
if (!ImpersonateOnSerializingReplyMessageProperty.TryGet(this.innerMessage, out impersonationProperty))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.UnableToImpersonateWhileSerializingReponse)));
}
try
{
impersonationProperty.StartImpersonation(out impersonationContext, out originalPrincipal, out isThreadPrincipalSet);
return this.innerMessage.BeginWriteBodyContents(writer, callback, state);
}
finally
{
try
{
impersonationProperty.StopImpersonation(impersonationContext, originalPrincipal, isThreadPrincipalSet);
}
#pragma warning suppress 56500 // covered by FxCOP
catch
{
string message = null;
try
{
message = SR.GetString(SR.SFxRevertImpersonationFailed0);
}
finally
{
DiagnosticUtility.FailFast(message);
}
}
}
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
this.ImpersonateCall( () => this.innerMessage.WriteBodyContents(writer));
}
protected override void OnEndWriteBodyContents(IAsyncResult result)
{
this.innerMessage.EndWriteBodyContents(result);
}
//Runs impersonated.
protected override void OnBodyToString(XmlDictionaryWriter writer)
{
this.ImpersonateCall(() => this.innerMessage.BodyToString(writer));
}
void ImpersonateCall(Action callToImpersonate)
{
ImpersonateOnSerializingReplyMessageProperty impersonationProperty = null;
IDisposable impersonationContext = null;
IPrincipal originalPrincipal = null;
bool isThreadPrincipalSet = false;
if (!ImpersonateOnSerializingReplyMessageProperty.TryGet(this.innerMessage, out impersonationProperty))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.UnableToImpersonateWhileSerializingReponse)));
}
try
{
impersonationProperty.StartImpersonation(out impersonationContext, out originalPrincipal, out isThreadPrincipalSet);
callToImpersonate();
}
finally
{
try
{
impersonationProperty.StopImpersonation(impersonationContext, originalPrincipal, isThreadPrincipalSet);
}
#pragma warning suppress 56500 // covered by FxCOP
catch
{
string message = null;
try
{
message = SR.GetString(SR.SFxRevertImpersonationFailed0);
}
finally
{
DiagnosticUtility.FailFast(message);
}
}
}
}
}
}
|