Example #1
0
 public NegotiationHost(NegotiationTokenAuthenticator <T> authenticator, Uri listenUri, ChannelBuilder channelBuilder, MessageFilter listenerFilter)
 {
     this.authenticator  = authenticator;
     this.listenUri      = listenUri;
     this.channelBuilder = channelBuilder;
     this.listenerFilter = listenerFilter;
 }
Example #2
0
 internal NegotiationSyncInvoker(NegotiationTokenAuthenticator <T> parent)
 {
     this.parent = parent;
 }
Example #3
0
        private Message ProcessRequestCore(Message request)
        {
            Message message;

            if (request == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("request");
            }
            RequestSecurityToken         requestSecurityToken         = null;
            RequestSecurityTokenResponse requestSecurityTokenResponse = null;
            string context          = null;
            bool   flag             = false;
            bool   flag2            = true;
            T      negotiationState = default(T);

            try
            {
                if (this.maxMessageSize < 0x7fffffff)
                {
                    string action = request.Headers.Action;
                    try
                    {
                        using (MessageBuffer buffer = request.CreateBufferedCopy(this.maxMessageSize))
                        {
                            request = buffer.CreateMessage();
                            flag    = true;
                        }
                    }
                    catch (QuotaExceededException exception)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(System.ServiceModel.SR.GetString("SecurityNegotiationMessageTooLarge", new object[] { action, this.maxMessageSize }), exception));
                    }
                }
                try
                {
                    BodyWriter writer;
                    Uri        to = request.Headers.To;
                    this.ParseMessageBody(request, out context, out requestSecurityToken, out requestSecurityTokenResponse);
                    if (context != null)
                    {
                        negotiationState = this.stateCache.GetState(context);
                    }
                    else
                    {
                        negotiationState = default(T);
                    }
                    bool flag3 = false;
                    try
                    {
                        if (requestSecurityToken != null)
                        {
                            if (negotiationState != null)
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new SecurityNegotiationException(System.ServiceModel.SR.GetString("NegotiationStateAlreadyPresent", new object[] { context })));
                            }
                            writer = this.ProcessRequestSecurityToken(request, requestSecurityToken, out negotiationState);
                            lock (negotiationState.ThisLock)
                            {
                                if (negotiationState.IsNegotiationCompleted)
                                {
                                    if (!negotiationState.ServiceToken.IsCookieMode)
                                    {
                                        this.IssuedTokenCache.AddContext(negotiationState.ServiceToken);
                                    }
                                    this.OnTokenIssued(negotiationState.ServiceToken);
                                    SecurityTraceRecordHelper.TraceServiceSecurityNegotiationCompleted <T>((NegotiationTokenAuthenticator <T>) this, negotiationState.ServiceToken);
                                    flag3 = true;
                                }
                                else
                                {
                                    this.stateCache.AddState(context, negotiationState);
                                    flag3 = false;
                                }
                                this.AddNegotiationChannelForIdleTracking();
                                goto Label_0299;
                            }
                        }
                        if (negotiationState == null)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new SecurityNegotiationException(System.ServiceModel.SR.GetString("CannotFindNegotiationState", new object[] { context })));
                        }
                        lock (negotiationState.ThisLock)
                        {
                            writer = this.ProcessRequestSecurityTokenResponse(negotiationState, request, requestSecurityTokenResponse);
                            if (negotiationState.IsNegotiationCompleted)
                            {
                                if (!negotiationState.ServiceToken.IsCookieMode)
                                {
                                    this.IssuedTokenCache.AddContext(negotiationState.ServiceToken);
                                }
                                this.OnTokenIssued(negotiationState.ServiceToken);
                                SecurityTraceRecordHelper.TraceServiceSecurityNegotiationCompleted <T>((NegotiationTokenAuthenticator <T>) this, negotiationState.ServiceToken);
                                flag3 = true;
                            }
                            else
                            {
                                flag3 = false;
                            }
                        }
Label_0299:
                        if ((negotiationState.IsNegotiationCompleted && (null != this.ListenUri)) && (AuditLevel.Success == (this.messageAuthenticationAuditLevel & AuditLevel.Success)))
                        {
                            string remoteIdentityName = negotiationState.GetRemoteIdentityName();
                            SecurityAuditHelper.WriteSecurityNegotiationSuccessEvent(this.auditLogLocation, this.suppressAuditFailure, request, request.Headers.To, request.Headers.Action, remoteIdentityName, base.GetType().Name);
                        }
                        flag2 = false;
                    }
                    catch (Exception exception2)
                    {
                        if (Fx.IsFatal(exception2))
                        {
                            throw;
                        }
                        if (PerformanceCounters.PerformanceCountersEnabled && (null != this.ListenUri))
                        {
                            PerformanceCounters.AuthenticationFailed(request, this.ListenUri);
                        }
                        if (AuditLevel.Failure == (this.messageAuthenticationAuditLevel & AuditLevel.Failure))
                        {
                            try
                            {
                                string clientIdentity = (negotiationState != null) ? negotiationState.GetRemoteIdentityName() : string.Empty;
                                SecurityAuditHelper.WriteSecurityNegotiationFailureEvent(this.auditLogLocation, this.suppressAuditFailure, request, request.Headers.To, request.Headers.Action, clientIdentity, base.GetType().Name, exception2);
                            }
                            catch (Exception exception3)
                            {
                                if (Fx.IsFatal(exception3))
                                {
                                    throw;
                                }
                                DiagnosticUtility.ExceptionUtility.TraceHandledException(exception3, TraceEventType.Error);
                            }
                        }
                        flag3 = true;
                        throw;
                    }
                    finally
                    {
                        if (flag3 && (negotiationState != null))
                        {
                            if (context != null)
                            {
                                this.stateCache.RemoveState(context);
                            }
                            negotiationState.Dispose();
                        }
                    }
                    return(NegotiationTokenAuthenticator <T> .CreateReply(request, (writer is RequestSecurityTokenResponseCollection)?this.RequestSecurityTokenResponseFinalAction : this.RequestSecurityTokenResponseAction, writer));
                }
                finally
                {
                    if (flag)
                    {
                        request.Close();
                    }
                }
            }
            finally
            {
                if (flag2)
                {
                    this.AddNegotiationChannelForIdleTracking();
                }
                else if ((negotiationState != null) && negotiationState.IsNegotiationCompleted)
                {
                    this.RemoveNegotiationChannelFromIdleTracking();
                }
            }
            return(message);
        }