1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
4 namespace System.ServiceModel.Channels
8 using System.Security.Authentication;
9 using System.ServiceModel.Security;
10 using System.ServiceModel.Diagnostics;
12 abstract class StreamSecurityUpgradeAcceptorAsyncResult : TraceAsyncResult
14 SecurityMessageProperty remoteSecurity;
15 Stream upgradedStream;
17 static AsyncCallback onAuthenticateAsServer = Fx.ThunkCallback(new AsyncCallback(OnAuthenticateAsServer));
19 protected StreamSecurityUpgradeAcceptorAsyncResult(AsyncCallback callback, object state)
20 : base(callback, state)
24 public void Begin(Stream stream)
29 result = this.OnBegin(stream, onAuthenticateAsServer);
31 catch (AuthenticationException exception)
33 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception.Message,
36 catch (IOException ioException)
38 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(
39 SR.GetString(SR.NegotiationFailedIO, ioException.Message), ioException));
42 if (!result.CompletedSynchronously)
47 CompleteAuthenticateAsServer(result);
51 void CompleteAuthenticateAsServer(IAsyncResult result)
55 this.upgradedStream = this.OnCompleteAuthenticateAsServer(result);
57 catch (AuthenticationException exception)
59 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception.Message,
62 catch (IOException ioException)
64 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(
65 SR.GetString(SR.NegotiationFailedIO, ioException.Message), ioException));
68 this.remoteSecurity = this.ValidateCreateSecurity();
71 public static Stream End(IAsyncResult result, out SecurityMessageProperty remoteSecurity)
73 StreamSecurityUpgradeAcceptorAsyncResult thisPtr = AsyncResult.End<StreamSecurityUpgradeAcceptorAsyncResult>(result);
74 remoteSecurity = thisPtr.remoteSecurity;
75 return thisPtr.upgradedStream;
78 static void OnAuthenticateAsServer(IAsyncResult result)
80 if (result.CompletedSynchronously)
85 StreamSecurityUpgradeAcceptorAsyncResult acceptUpgradeAsyncResult =
86 (StreamSecurityUpgradeAcceptorAsyncResult)result.AsyncState;
88 Exception completionException = null;
91 acceptUpgradeAsyncResult.CompleteAuthenticateAsServer(result);
93 #pragma warning suppress 56500 // Microsoft, transferring exception to another thread
100 completionException = e;
102 acceptUpgradeAsyncResult.Complete(false, completionException);
105 protected abstract IAsyncResult OnBegin(Stream stream, AsyncCallback callback);
106 protected abstract Stream OnCompleteAuthenticateAsServer(IAsyncResult result);
107 protected abstract SecurityMessageProperty ValidateCreateSecurity();