CustomServiceCredentials.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / TransactionBridge / Microsoft / Transactions / Wsat / Messaging / CustomServiceCredentials.cs / 1 / CustomServiceCredentials.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

// Override ServiceCredentials to provide a custom token authenticator 

using System; 
using System.Collections.Generic; 
using System.Diagnostics;
using System.IdentityModel.Selectors; 
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Security; 
using System.ServiceModel.Security.Tokens;
using System.Threading; 
 
using DiagnosticUtility = Microsoft.Transactions.Bridge.DiagnosticUtility;
 
namespace Microsoft.Transactions.Wsat.Messaging
{
    class SupportingTokenServiceCredentials : ServiceCredentials
    { 
        SupportingTokenSecurityTokenResolver tokenResolver;
        SecurityContextSecurityTokenAuthenticator tokenAuthenticator; 
 
        public SupportingTokenServiceCredentials()
        { 
            this.tokenResolver = new SupportingTokenSecurityTokenResolver();
            this.tokenAuthenticator = new SecurityContextSecurityTokenAuthenticator();
        }
 
        SupportingTokenServiceCredentials(SupportingTokenServiceCredentials other)
            : 
            base(other) 
        {
            this.tokenResolver = other.tokenResolver; 
            this.tokenAuthenticator = other.tokenAuthenticator;
        }

        public SupportingTokenSecurityTokenResolver TokenResolver 
        {
            get { return this.tokenResolver; } 
        } 

        public override SecurityTokenManager CreateSecurityTokenManager() 
        {
            return new SupportingTokenSecurityTokenManager(this);
        }
 
        protected override ServiceCredentials CloneCore()
        { 
            return new SupportingTokenServiceCredentials(this); 
        }
 
        class SupportingTokenSecurityTokenManager : ServiceCredentialsSecurityTokenManager
        {
            SupportingTokenServiceCredentials serverCreds;
 
            public SupportingTokenSecurityTokenManager(SupportingTokenServiceCredentials serverCreds)
                : 
                base(serverCreds) 
            {
                this.serverCreds = serverCreds; 
            }

            public override SecurityTokenAuthenticator CreateSecurityTokenAuthenticator(SecurityTokenRequirement tokenRequirement, out SecurityTokenResolver outOfBandTokenResolver)
            { 
                if (tokenRequirement == null)
                { 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("tokenRequirement"); 
                }
 
                if (tokenRequirement.TokenType == ServiceModelSecurityTokenTypes.SecurityContext)
                {
                    if (DebugTrace.Verbose)
                        DebugTrace.Trace(TraceLevel.Verbose, "CreateSecurityTokenAuthenticator for SecurityContext"); 

                    outOfBandTokenResolver = this.serverCreds.tokenResolver; 
                    return this.serverCreds.tokenAuthenticator; 
                }
                else 
                {
                    return base.CreateSecurityTokenAuthenticator(tokenRequirement, out outOfBandTokenResolver);
                }
            } 

            // Transactions does not need an identity to be specified in the EPR. 
            public override EndpointIdentity GetIdentityOfSelf(SecurityTokenRequirement tokenRequirement) 
            {
                return null; 
            }
        }
    }
 
    // ServiceCredentialsTokenManager defined in ServiceModel will default to
    // creating a X509CertificateIdentity when no endpoint identity is specified 
    // but while a service certificate is specified. Transactions does not need 
    // identity in the EPR. In order to avoid the performance hit in serializing
    // a Certificate in the EPR we are providing a custom token manager that 
    // will return a null Identity from GetIdentityOfSelf.
    class DefaultServiceCredentials : ServiceCredentials
    {
        public DefaultServiceCredentials() 
            : base()
        { 
        } 

        public DefaultServiceCredentials(DefaultServiceCredentials other) 
            : base(other)
        {
        }
 
        public override SecurityTokenManager CreateSecurityTokenManager()
        { 
            return new DefaultSecurityTokenManager(this); 
        }
 
        protected override ServiceCredentials CloneCore()
        {
            return new DefaultServiceCredentials(this);
        } 

        class DefaultSecurityTokenManager : ServiceCredentialsSecurityTokenManager 
        { 
            DefaultServiceCredentials serverCreds;
 
            public DefaultSecurityTokenManager(DefaultServiceCredentials serverCreds)
                : base(serverCreds)
            {
                this.serverCreds = serverCreds; 
            }
 
            public override EndpointIdentity GetIdentityOfSelf(SecurityTokenRequirement tokenRequirement) 
            {
                return null; 
            }
        }
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK