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
- SqlGatherConsumedAliases.cs
- RoutedPropertyChangedEventArgs.cs
- RowTypePropertyElement.cs
- figurelengthconverter.cs
- DataFormats.cs
- BinaryNode.cs
- SequentialWorkflowHeaderFooter.cs
- ServiceBuildProvider.cs
- DataGridViewRowEventArgs.cs
- FrameworkElementFactoryMarkupObject.cs
- ManifestResourceInfo.cs
- CheckBoxList.cs
- SurrogateEncoder.cs
- _TransmitFileOverlappedAsyncResult.cs
- TextEditor.cs
- BinaryObjectReader.cs
- Page.cs
- OleDbError.cs
- MdiWindowListStrip.cs
- CLSCompliantAttribute.cs
- SignerInfo.cs
- HtmlInputPassword.cs
- RedBlackList.cs
- DescendantOverDescendantQuery.cs
- GenericUriParser.cs
- WsdlBuildProvider.cs
- mediaeventargs.cs
- Screen.cs
- ProxyElement.cs
- Size.cs
- Debug.cs
- SerializationFieldInfo.cs
- XPathSingletonIterator.cs
- NetworkInformationException.cs
- SqlCommandAsyncResult.cs
- GenerateScriptTypeAttribute.cs
- DataSpaceManager.cs
- HttpCacheVaryByContentEncodings.cs
- ActivityDefaults.cs
- LinkDesigner.cs
- ImageAttributes.cs
- ResourcesBuildProvider.cs
- IdleTimeoutMonitor.cs
- XPathAncestorQuery.cs
- UnionCodeGroup.cs
- HtmlElementCollection.cs
- MessageQueueTransaction.cs
- CodeGotoStatement.cs
- ConfigsHelper.cs
- ElementHost.cs
- TableLayoutSettingsTypeConverter.cs
- HistoryEventArgs.cs
- TrustDriver.cs
- WebPartVerbsEventArgs.cs
- WindowsEditBoxRange.cs
- EventPrivateKey.cs
- EmbeddedMailObject.cs
- TraversalRequest.cs
- DataTableReaderListener.cs
- DataControlFieldTypeEditor.cs
- WebServiceResponseDesigner.cs
- HttpStreams.cs
- CommandManager.cs
- LinkUtilities.cs
- ThicknessAnimation.cs
- XmlSchemaComplexType.cs
- FrugalList.cs
- ObjectDataSourceSelectingEventArgs.cs
- UnionCqlBlock.cs
- HttpListenerContext.cs
- DesignerCatalogPartChrome.cs
- DataGridViewHitTestInfo.cs
- Pkcs7Recipient.cs
- TypeConverterHelper.cs
- TraceHandlerErrorFormatter.cs
- LocationInfo.cs
- NamespaceCollection.cs
- PowerModeChangedEventArgs.cs
- DataBoundControl.cs
- XslTransform.cs
- EntityDataSourceValidationException.cs
- UpdateTracker.cs
- GradientBrush.cs
- TextElementEnumerator.cs
- XPathNodeInfoAtom.cs
- SaveFileDialog.cs
- DiscoveryOperationContextExtension.cs
- TemplateApplicationHelper.cs
- WebException.cs
- MembershipUser.cs
- SqlCrossApplyToCrossJoin.cs
- PrimitiveType.cs
- StylusPointProperties.cs
- NativeMethods.cs
- PrivilegeNotHeldException.cs
- BitmapData.cs
- SQLByte.cs
- Encoder.cs
- Viewport3DVisual.cs
- ScrollPatternIdentifiers.cs