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;
11 abstract class StreamSecurityUpgradeInitiatorAsyncResult : AsyncResult
13 Stream originalStream;
14 SecurityMessageProperty remoteSecurity;
15 Stream upgradedStream;
17 static AsyncCallback onAuthenticateAsClient = Fx.ThunkCallback(new AsyncCallback(OnAuthenticateAsClient));
19 public StreamSecurityUpgradeInitiatorAsyncResult(AsyncCallback callback, object state)
20 : base(callback, state)
25 public void Begin(Stream stream)
27 this.originalStream = stream;
32 result = this.OnBeginAuthenticateAsClient(this.originalStream, onAuthenticateAsClient);
34 catch (AuthenticationException exception)
36 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception.Message,
39 catch (IOException ioException)
41 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(
42 SR.GetString(SR.NegotiationFailedIO, ioException.Message), ioException));
45 if (!result.CompletedSynchronously)
50 CompleteAuthenticateAsClient(result);
54 void CompleteAuthenticateAsClient(IAsyncResult result)
58 this.upgradedStream = this.OnCompleteAuthenticateAsClient(result);
60 catch (AuthenticationException exception)
62 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception.Message,
65 catch (IOException ioException)
67 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(
68 SR.GetString(SR.NegotiationFailedIO, ioException.Message), ioException));
71 this.remoteSecurity = this.ValidateCreateSecurity();
74 public static Stream End(IAsyncResult result, out SecurityMessageProperty remoteSecurity)
76 StreamSecurityUpgradeInitiatorAsyncResult thisPtr = AsyncResult.End<StreamSecurityUpgradeInitiatorAsyncResult>(result);
77 remoteSecurity = thisPtr.remoteSecurity;
78 return thisPtr.upgradedStream;
81 static void OnAuthenticateAsClient(IAsyncResult result)
83 if (result.CompletedSynchronously)
88 StreamSecurityUpgradeInitiatorAsyncResult thisPtr =
89 (StreamSecurityUpgradeInitiatorAsyncResult)result.AsyncState;
91 Exception completionException = null;
94 thisPtr.CompleteAuthenticateAsClient(result);
96 #pragma warning suppress 56500 // Microsoft, transferring exception to another thread
104 completionException = e;
106 thisPtr.Complete(false, completionException);
109 protected abstract IAsyncResult OnBeginAuthenticateAsClient(Stream stream, AsyncCallback callback);
110 protected abstract Stream OnCompleteAuthenticateAsClient(IAsyncResult result);
111 protected abstract SecurityMessageProperty ValidateCreateSecurity();