Exemple #1
0
        protected SecurityToken EndNegotiation(IAsyncResult result)
        {
            SecurityToken serviceToken = SecurityNegotiationAsyncResult <T> .End(result);

            SecurityTraceRecordHelper.TraceEndSecurityNegotiation <T>((IssuanceTokenProviderBase <T>) this, serviceToken, this.targetAddress);
            return(serviceToken);
        }
        protected SecurityToken EndNegotiation(IAsyncResult result)
        {
            SecurityToken token = SecurityNegotiationAsyncResult.End(result);

            SecurityTraceRecordHelper.TraceEndSecurityNegotiation(this, token, this.targetAddress);
            return(token);
        }
Exemple #3
0
        protected SecurityToken DoNegotiation(TimeSpan timeout)
        {
            SecurityToken token2;

            this.ThrowIfClosedOrCreated();
            SecurityTraceRecordHelper.TraceBeginSecurityNegotiation <T>((IssuanceTokenProviderBase <T>) this, this.targetAddress);
            TimeoutHelper   helper     = new TimeoutHelper(timeout);
            IRequestChannel rstChannel = null;
            T        negotiationState  = default(T);
            TimeSpan span = timeout;
            int      num  = 1;

            try
            {
                negotiationState = this.CreateNegotiationState(this.targetAddress, this.via, helper.RemainingTime());
                this.InitializeNegotiationState(negotiationState);
                this.InitializeChannelFactories(negotiationState.RemoteAddress, helper.RemainingTime());
                rstChannel = this.CreateClientChannel(negotiationState.RemoteAddress, this.via);
                rstChannel.Open(helper.RemainingTime());
                Message       nextOutgoingMessage = null;
                Message       incomingMessage     = null;
                SecurityToken serviceToken        = null;
                while (true)
                {
                    nextOutgoingMessage = this.GetNextOutgoingMessage(incomingMessage, negotiationState);
                    if (incomingMessage != null)
                    {
                        incomingMessage.Close();
                    }
                    if (nextOutgoingMessage == null)
                    {
                        break;
                    }
                    using (nextOutgoingMessage)
                    {
                        TraceUtility.ProcessOutgoingMessage(nextOutgoingMessage);
                        span            = helper.RemainingTime();
                        incomingMessage = rstChannel.Request(nextOutgoingMessage, span);
                        if (incomingMessage == null)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(System.ServiceModel.SR.GetString("FailToRecieveReplyFromNegotiation")));
                        }
                        TraceUtility.ProcessIncomingMessage(incomingMessage);
                    }
                    num += 2;
                }
                if (!negotiationState.IsNegotiationCompleted)
                {
                    throw TraceUtility.ThrowHelperError(new SecurityNegotiationException(System.ServiceModel.SR.GetString("NoNegotiationMessageToSend")), incomingMessage);
                }
                try
                {
                    rstChannel.Close(helper.RemainingTime());
                }
                catch (CommunicationException exception)
                {
                    if (DiagnosticUtility.ShouldTraceInformation)
                    {
                        DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information);
                    }
                    rstChannel.Abort();
                }
                catch (TimeoutException exception2)
                {
                    if (DiagnosticUtility.ShouldTraceInformation)
                    {
                        DiagnosticUtility.ExceptionUtility.TraceHandledException(exception2, TraceEventType.Information);
                    }
                    rstChannel.Abort();
                }
                rstChannel = null;
                this.ValidateAndCacheServiceToken(negotiationState);
                serviceToken = negotiationState.ServiceToken;
                SecurityTraceRecordHelper.TraceEndSecurityNegotiation <T>((IssuanceTokenProviderBase <T>) this, serviceToken, this.targetAddress);
                token2 = serviceToken;
            }
            catch (Exception exception3)
            {
                if (Fx.IsFatal(exception3))
                {
                    throw;
                }
                if (exception3 is TimeoutException)
                {
                    exception3 = new TimeoutException(System.ServiceModel.SR.GetString("ClientSecurityNegotiationTimeout", new object[] { timeout, num, span }), exception3);
                }
                EndpointAddress targetAddress = (negotiationState == null) ? null : negotiationState.RemoteAddress;
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(IssuanceTokenProviderBase <T> .WrapExceptionIfRequired(exception3, targetAddress, this.issuerAddress));
            }
            finally
            {
                this.Cleanup(rstChannel, negotiationState);
            }
            return(token2);
        }
        /*
         *   Negotiation consists of the following steps (some may be async in the async case):
         *   1. Create negotiation state
         *   2. Initialize channel factories
         *   3. Create an channel
         *   4. Open the channel
         *   5. Create the next message to send to server
         *   6. Send the message and get reply
         *   8. Process incoming message and get next outgoing message.
         *   9. If no outgoing message, then negotiation is over. Go to step 11.
         *   10. Goto step 6
         *   11. Close the IRequest channel and complete
         */
        protected SecurityToken DoNegotiation(TimeSpan timeout)
        {
            ThrowIfClosedOrCreated();
            SecurityTraceRecordHelper.TraceBeginSecurityNegotiation(this, this.targetAddress);
            TimeoutHelper   timeoutHelper = new TimeoutHelper(timeout);
            IRequestChannel rstChannel    = null;
            T        negotiationState     = null;
            TimeSpan timeLeft             = timeout;
            int      legs = 1;

            try
            {
                negotiationState = this.CreateNegotiationState(this.targetAddress, this.via, timeoutHelper.RemainingTime());
                InitializeNegotiationState(negotiationState);
                this.InitializeChannelFactories(negotiationState.RemoteAddress, timeoutHelper.RemainingTime());
                rstChannel = this.CreateClientChannel(negotiationState.RemoteAddress, this.via);
                rstChannel.Open(timeoutHelper.RemainingTime());
                Message       nextOutgoingMessage = null;
                Message       incomingMessage     = null;
                SecurityToken serviceToken        = null;
                for (;;)
                {
                    nextOutgoingMessage = this.GetNextOutgoingMessage(incomingMessage, negotiationState);
                    if (incomingMessage != null)
                    {
                        incomingMessage.Close();
                    }
                    if (nextOutgoingMessage != null)
                    {
                        using (nextOutgoingMessage)
                        {
                            EventTraceActivity eventTraceActivity = null;
                            if (TD.MessageSentToTransportIsEnabled())
                            {
                                eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(nextOutgoingMessage);
                            }

                            TraceUtility.ProcessOutgoingMessage(nextOutgoingMessage, eventTraceActivity);
                            timeLeft        = timeoutHelper.RemainingTime();
                            incomingMessage = rstChannel.Request(nextOutgoingMessage, timeLeft);
                            if (incomingMessage == null)
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(SR.GetString(SR.FailToRecieveReplyFromNegotiation)));
                            }

                            if (eventTraceActivity == null && TD.MessageReceivedFromTransportIsEnabled())
                            {
                                eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(incomingMessage);
                            }

                            TraceUtility.ProcessIncomingMessage(incomingMessage, eventTraceActivity);
                        }
                        legs += 2;
                    }
                    else
                    {
                        if (!negotiationState.IsNegotiationCompleted)
                        {
                            throw TraceUtility.ThrowHelperError(new SecurityNegotiationException(SR.GetString(SR.NoNegotiationMessageToSend)), incomingMessage);
                        }

                        try
                        {
                            rstChannel.Close(timeoutHelper.RemainingTime());
                        }
                        catch (CommunicationException e)
                        {
                            DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);

                            rstChannel.Abort();
                        }
                        catch (TimeoutException e)
                        {
                            if (TD.CloseTimeoutIsEnabled())
                            {
                                TD.CloseTimeout(e.Message);
                            }
                            DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);

                            rstChannel.Abort();
                        }

                        rstChannel = null;
                        this.ValidateAndCacheServiceToken(negotiationState);
                        serviceToken = negotiationState.ServiceToken;
                        SecurityTraceRecordHelper.TraceEndSecurityNegotiation(this, serviceToken, this.targetAddress);
                        break;
                    }
                }
                return(serviceToken);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                if (e is TimeoutException)
                {
                    e = new TimeoutException(SR.GetString(SR.ClientSecurityNegotiationTimeout, timeout, legs, timeLeft), e);
                }
                EndpointAddress temp = (negotiationState == null) ? null : negotiationState.RemoteAddress;
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(WrapExceptionIfRequired(e, temp, this.issuerAddress));
            }
            finally
            {
                Cleanup(rstChannel, negotiationState);
            }
        }