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
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.IO;
using System.Runtime;
using System.Security.Authentication;
using System.ServiceModel.Security;
using System.ServiceModel.Diagnostics;
abstract class StreamSecurityUpgradeAcceptorAsyncResult : TraceAsyncResult
{
SecurityMessageProperty remoteSecurity;
Stream upgradedStream;
static AsyncCallback onAuthenticateAsServer = Fx.ThunkCallback(new AsyncCallback(OnAuthenticateAsServer));
protected StreamSecurityUpgradeAcceptorAsyncResult(AsyncCallback callback, object state)
: base(callback, state)
{
}
public void Begin(Stream stream)
{
IAsyncResult result;
try
{
result = this.OnBegin(stream, onAuthenticateAsServer);
}
catch (AuthenticationException exception)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception.Message,
exception));
}
catch (IOException ioException)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(
SR.GetString(SR.NegotiationFailedIO, ioException.Message), ioException));
}
if (!result.CompletedSynchronously)
{
return;
}
CompleteAuthenticateAsServer(result);
base.Complete(true);
}
void CompleteAuthenticateAsServer(IAsyncResult result)
{
try
{
this.upgradedStream = this.OnCompleteAuthenticateAsServer(result);
}
catch (AuthenticationException exception)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception.Message,
exception));
}
catch (IOException ioException)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(
SR.GetString(SR.NegotiationFailedIO, ioException.Message), ioException));
}
this.remoteSecurity = this.ValidateCreateSecurity();
}
public static Stream End(IAsyncResult result, out SecurityMessageProperty remoteSecurity)
{
StreamSecurityUpgradeAcceptorAsyncResult thisPtr = AsyncResult.End<StreamSecurityUpgradeAcceptorAsyncResult>(result);
remoteSecurity = thisPtr.remoteSecurity;
return thisPtr.upgradedStream;
}
static void OnAuthenticateAsServer(IAsyncResult result)
{
if (result.CompletedSynchronously)
{
return;
}
StreamSecurityUpgradeAcceptorAsyncResult acceptUpgradeAsyncResult =
(StreamSecurityUpgradeAcceptorAsyncResult)result.AsyncState;
Exception completionException = null;
try
{
acceptUpgradeAsyncResult.CompleteAuthenticateAsServer(result);
}
#pragma warning suppress 56500 // [....], transferring exception to another thread
catch (Exception e)
{
if (Fx.IsFatal(e))
{
throw;
}
completionException = e;
}
acceptUpgradeAsyncResult.Complete(false, completionException);
}
protected abstract IAsyncResult OnBegin(Stream stream, AsyncCallback callback);
protected abstract Stream OnCompleteAuthenticateAsServer(IAsyncResult result);
protected abstract SecurityMessageProperty ValidateCreateSecurity();
}
}
|