Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / MsmqChannelListenerBase.cs / 1 / MsmqChannelListenerBase.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Channels { using System.Collections.Generic; using System.Diagnostics; using System.ServiceModel; using System.ServiceModel.Description; using System.Collections.ObjectModel; using System.Net.Security; using System.Runtime.Serialization; using System.IdentityModel.Claims; using System.IdentityModel.Policy; using System.IdentityModel.Selectors; using System.IdentityModel.Tokens; using System.Security.Cryptography.X509Certificates; using System.Security.Principal; using System.ServiceModel.Security; using System.ServiceModel.Security.Tokens; using System.Transactions; using SR = System.ServiceModel.SR; using AsyncResult = System.ServiceModel.AsyncResult; using System.Threading; abstract class MsmqChannelListenerBase : TransportChannelListener { MsmqReceiveParameters receiveParameters; protected MsmqChannelListenerBase(MsmqBindingElementBase bindingElement, BindingContext context, MsmqReceiveParameters receiveParameters, MessageEncoderFactory messageEncoderFactory) : base(bindingElement, context, messageEncoderFactory) { this.receiveParameters = receiveParameters; } internal MsmqReceiveParameters ReceiveParameters { get { return this.receiveParameters; } } internal Exception NormalizePoisonException(long lookupId, Exception innerException) { if (this.ReceiveParameters.ExactlyOnce) return DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqPoisonMessageException(lookupId, innerException)); else if (null != innerException) return DiagnosticUtility.ExceptionUtility.ThrowHelperError(innerException); else { DiagnosticUtility.DebugAssert("System.ServiceModel.Channels.MsmqChannelListenerBase.NormalizePoisonException(): (innerException == null)"); throw DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(false); } } internal void FaultListener() { this.Fault(); } } abstract class MsmqChannelListenerBase: MsmqChannelListenerBase, IChannelListener where TChannel : class, IChannel { SecurityTokenAuthenticator x509SecurityTokenAuthenticator; protected MsmqChannelListenerBase(MsmqBindingElementBase bindingElement, BindingContext context, MsmqReceiveParameters receiveParameters, MessageEncoderFactory messageEncoderFactory) : base(bindingElement, context, receiveParameters, messageEncoderFactory) {} public override string Scheme { get { return "net.msmq"; } } internal override UriPrefixTable TransportManagerTable { get { return Msmq.StaticTransportManagerTable; } } internal override ITransportManagerRegistration CreateTransportManagerRegistration(Uri listenUri) { return null; } protected virtual void OnCloseCore(bool isAborting) {} protected virtual void OnOpenCore(TimeSpan timeout) { if (MsmqAuthenticationMode.Certificate == this.ReceiveParameters.TransportSecurity.MsmqAuthenticationMode) SecurityUtils.OpenTokenAuthenticatorIfRequired(this.x509SecurityTokenAuthenticator, timeout); } protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state) { OnCloseCore(false); return base.OnBeginClose(timeout, callback, state); } protected override void OnClose(TimeSpan timeout) { OnCloseCore(false); base.OnClose(timeout); } protected override void OnAbort() { OnCloseCore(true); base.OnAbort(); } protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state) { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); IAsyncResult result = base.OnBeginOpen(timeoutHelper.RemainingTime(), callback, state); OnOpenCore(timeoutHelper.RemainingTime()); return result; } protected override void OnOpen(TimeSpan timeout) { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); base.OnOpen(timeoutHelper.RemainingTime()); OnOpenCore(timeoutHelper.RemainingTime()); } internal override IList SelectTransportManagers() { lock (this.TransportManagerTable) { // Look up an existing transport manager registration. We use registration only // for WebHosted case. ITransportManagerRegistration registration; if (this.TransportManagerTable.TryLookupUri(this.Uri, TransportDefaults.HostNameComparisonMode, out registration)) { // no need to use TransportManagerContainer because we never use the transport manager from channels // Use the registration to select a set of compatible transport managers. IList foundTransportManagers = registration.Select(this); if (foundTransportManagers != null) { for (int i = 0; i < foundTransportManagers.Count; i++) { foundTransportManagers[i].Open(this); } } } } return null; } protected void SetSecurityTokenAuthenticator(string scheme, BindingContext context) { if (this.ReceiveParameters.TransportSecurity.MsmqAuthenticationMode == MsmqAuthenticationMode.Certificate) { SecurityCredentialsManager credentials = context.BindingParameters.Find (); if (credentials == null) { credentials = ServiceCredentials.CreateDefaultCredentials(); } SecurityTokenManager tokenManager = credentials.CreateSecurityTokenManager(); RecipientServiceModelSecurityTokenRequirement x509Requirement = new RecipientServiceModelSecurityTokenRequirement(); x509Requirement.TokenType = SecurityTokenTypes.X509Certificate; x509Requirement.TransportScheme = scheme; x509Requirement.KeyUsage = SecurityKeyUsage.Signature; SecurityTokenResolver dummy; this.x509SecurityTokenAuthenticator = tokenManager.CreateSecurityTokenAuthenticator(x509Requirement, out dummy); } } internal SecurityMessageProperty ValidateSecurity(MsmqInputMessage msmqMessage) { SecurityMessageProperty result = null; X509Certificate2 certificate = null; WindowsSidIdentity wsid = null; try { if (MsmqAuthenticationMode.Certificate == this.ReceiveParameters.TransportSecurity.MsmqAuthenticationMode) { try { certificate = new X509Certificate2(msmqMessage.SenderCertificate.GetBufferCopy(msmqMessage.SenderCertificateLength.Value)); X509SecurityToken token = new X509SecurityToken(certificate, false); ReadOnlyCollection authorizationPolicies = this.x509SecurityTokenAuthenticator.ValidateToken(token); SecurityMessageProperty security = new SecurityMessageProperty(); security.TransportToken = new SecurityTokenSpecification(token, authorizationPolicies); security.ServiceSecurityContext = new ServiceSecurityContext(authorizationPolicies); result = security; } catch (SecurityTokenValidationException ex) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(SR.GetString(SR.MsmqBadCertificate), ex)); } catch (System.Security.Cryptography.CryptographicException ex) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(SR.GetString(SR.MsmqBadCertificate), ex)); } } else if (MsmqAuthenticationMode.WindowsDomain == this.ReceiveParameters.TransportSecurity.MsmqAuthenticationMode) { byte[] sid = msmqMessage.SenderId.GetBufferCopy(msmqMessage.SenderIdLength.Value); if (0 == sid.Length) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(SR.GetString(SR.MsmqNoSid))); SecurityIdentifier securityIdentifier = new SecurityIdentifier(sid, 0); List claims = new List (2); claims.Add(new Claim(ClaimTypes.Sid, securityIdentifier, Rights.Identity)); claims.Add(Claim.CreateWindowsSidClaim(securityIdentifier)); ClaimSet claimSet = new DefaultClaimSet(ClaimSet.System, claims); List policies = new List (1); wsid = new WindowsSidIdentity(securityIdentifier); policies.Add(new UnconditionalPolicy(wsid, claimSet)); ReadOnlyCollection authorizationPolicies = policies.AsReadOnly(); SecurityMessageProperty security = new SecurityMessageProperty(); security.TransportToken = new SecurityTokenSpecification(null, authorizationPolicies); security.ServiceSecurityContext = new ServiceSecurityContext(authorizationPolicies); result = security; } } #pragma warning suppress 56500 // covered by FXCop catch (Exception exception) { if (DiagnosticUtility.IsFatal(exception)) throw; // Audit Authentication failure if (AuditLevel.Failure == (this.AuditBehavior.MessageAuthenticationAuditLevel & AuditLevel.Failure)) WriteAuditEvent(AuditLevel.Failure, certificate, wsid, null); throw; } // Audit Authentication success if (result != null && AuditLevel.Success == (this.AuditBehavior.MessageAuthenticationAuditLevel & AuditLevel.Success)) WriteAuditEvent(AuditLevel.Success, certificate, wsid, null); return result; } void WriteAuditEvent(AuditLevel auditLevel, X509Certificate2 certificate, WindowsSidIdentity wsid, Exception exception) { try { String primaryIdentity = String.Empty; if (certificate != null) { primaryIdentity = SecurityUtils.GetCertificateId(certificate); } else if (wsid != null) { primaryIdentity = SecurityUtils.GetIdentityName(wsid); } if (auditLevel == AuditLevel.Success) { SecurityAuditHelper.WriteTransportAuthenticationSuccessEvent(this.AuditBehavior.AuditLogLocation, this.AuditBehavior.SuppressAuditFailure, null, this.Uri, primaryIdentity); } else { SecurityAuditHelper.WriteTransportAuthenticationFailureEvent(this.AuditBehavior.AuditLogLocation, this.AuditBehavior.SuppressAuditFailure, null, this.Uri, primaryIdentity, exception); } } #pragma warning suppress 56500 catch (Exception auditException) { if (DiagnosticUtility.IsFatal(auditException) || auditLevel == AuditLevel.Success) throw; DiagnosticUtility.ExceptionUtility.TraceHandledException(auditException, TraceEventType.Error); } } public abstract TChannel AcceptChannel(); public abstract IAsyncResult BeginAcceptChannel(AsyncCallback callback, object state); public abstract TChannel AcceptChannel(TimeSpan timeout); public abstract IAsyncResult BeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state); public abstract TChannel EndAcceptChannel(IAsyncResult result); } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PolyBezierSegment.cs
- TypeSource.cs
- GridItemCollection.cs
- shaperfactoryquerycacheentry.cs
- SequentialActivityDesigner.cs
- BaseDataListDesigner.cs
- ServicePointManager.cs
- EnterpriseServicesHelper.cs
- SmtpMail.cs
- ConvertEvent.cs
- NameValueCollection.cs
- Gdiplus.cs
- ExpressionUtilities.cs
- Span.cs
- SqlPersonalizationProvider.cs
- Stack.cs
- DesignerSerializationOptionsAttribute.cs
- DurableDispatcherAddressingFault.cs
- Rfc2898DeriveBytes.cs
- _BufferOffsetSize.cs
- ActivityDesignerResources.cs
- CodeRemoveEventStatement.cs
- MenuItemStyle.cs
- PenThreadWorker.cs
- HatchBrush.cs
- CounterCreationDataCollection.cs
- RoleGroup.cs
- C14NUtil.cs
- SettingsSavedEventArgs.cs
- DiscoveryClientRequestChannel.cs
- FixedMaxHeap.cs
- SwitchElementsCollection.cs
- CommonGetThemePartSize.cs
- XmlAnyAttributeAttribute.cs
- RectValueSerializer.cs
- GlyphInfoList.cs
- ValueType.cs
- RuntimeComponentFilter.cs
- DateTime.cs
- Inline.cs
- Rect3DConverter.cs
- HtmlHistory.cs
- UnicodeEncoding.cs
- EventDescriptor.cs
- DescendentsWalkerBase.cs
- DataGridClipboardCellContent.cs
- CommandValueSerializer.cs
- EditorAttribute.cs
- XmlIterators.cs
- PropertyChangingEventArgs.cs
- AddInSegmentDirectoryNotFoundException.cs
- MenuItemBindingCollection.cs
- EmptyStringExpandableObjectConverter.cs
- IndexOutOfRangeException.cs
- WebPartZoneCollection.cs
- FontWeight.cs
- SafeRightsManagementSessionHandle.cs
- ShapeTypeface.cs
- CapabilitiesUse.cs
- PassportIdentity.cs
- TrackingMemoryStreamFactory.cs
- TargetPerspective.cs
- AstNode.cs
- PEFileEvidenceFactory.cs
- JsonDeserializer.cs
- DocumentSequence.cs
- InstalledFontCollection.cs
- ColorConverter.cs
- XmlLanguageConverter.cs
- OleDbConnectionPoolGroupProviderInfo.cs
- DataGridViewCellCancelEventArgs.cs
- DateTimeParse.cs
- BidOverLoads.cs
- OverrideMode.cs
- BaseDataListPage.cs
- DiagnosticsConfigurationHandler.cs
- EdmItemCollection.cs
- XmlTextWriter.cs
- DiscreteKeyFrames.cs
- AttributeCollection.cs
- PartBasedPackageProperties.cs
- ApplicationDirectory.cs
- IPEndPoint.cs
- ServiceDescription.cs
- COSERVERINFO.cs
- OleDbReferenceCollection.cs
- OdbcConnectionOpen.cs
- ScrollData.cs
- ChildrenQuery.cs
- DesignerProperties.cs
- PrimitiveSchema.cs
- TextShapeableCharacters.cs
- DataColumnChangeEvent.cs
- IncrementalReadDecoders.cs
- ApplicationManager.cs
- NameValuePair.cs
- DataReceivedEventArgs.cs
- QilPatternVisitor.cs
- XmlSignificantWhitespace.cs
- RotateTransform.cs