WSHttpSecurity.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 / ServiceModel / System / ServiceModel / WSHttpSecurity.cs / 1 / WSHttpSecurity.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------
namespace System.ServiceModel
{ 
    using System.Diagnostics;
    using System.Net.Security; 
    using System.ServiceModel.Channels; 
    using System.ServiceModel.Security;
 
    public sealed class WSHttpSecurity
    {
        internal const SecurityMode DefaultMode = SecurityMode.Message;
 
        SecurityMode mode;
        HttpTransportSecurity transportSecurity; 
        NonDualMessageSecurityOverHttp messageSecurity; 

        internal WSHttpSecurity() 
            : this(DefaultMode, GetDefaultHttpTransportSecurity(), new NonDualMessageSecurityOverHttp())
        {
        }
 
        internal WSHttpSecurity(SecurityMode mode, HttpTransportSecurity transportSecurity, NonDualMessageSecurityOverHttp messageSecurity)
        { 
            this.mode = mode; 
            this.transportSecurity = transportSecurity == null ? GetDefaultHttpTransportSecurity() : transportSecurity;
            this.messageSecurity = messageSecurity == null ? new NonDualMessageSecurityOverHttp() : messageSecurity; 
        }

        internal static HttpTransportSecurity GetDefaultHttpTransportSecurity()
        { 
            HttpTransportSecurity transportSecurity = new HttpTransportSecurity();
            transportSecurity.ClientCredentialType = HttpClientCredentialType.Windows; 
            return transportSecurity; 
        }
 
        public SecurityMode Mode
        {
            get { return this.mode; }
            set 
            {
                if (!SecurityModeHelper.IsDefined(value)) 
                { 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
                } 
                this.mode = value;
            }
        }
 
        public HttpTransportSecurity Transport
        { 
            get { return this.transportSecurity; } 
        }
 
        public NonDualMessageSecurityOverHttp Message
        {
            get { return this.messageSecurity; }
        } 

        internal void ApplyTransportSecurity(HttpsTransportBindingElement https) 
        { 
            if (this.mode == SecurityMode.TransportWithMessageCredential)
            { 
                this.transportSecurity.ConfigureTransportProtectionOnly(https);
            }
            else
            { 
                this.transportSecurity.ConfigureTransportProtectionAndAuthentication(https);
            } 
        } 

        internal static void ApplyTransportSecurity(HttpsTransportBindingElement transport, HttpTransportSecurity transportSecurity) 
        {
            HttpTransportSecurity.ConfigureTransportProtectionAndAuthentication(transport, transportSecurity);
        }
 
        internal SecurityBindingElement CreateMessageSecurity(bool isReliableSessionEnabled, MessageSecurityVersion version)
        { 
            if (this.mode == SecurityMode.Message || this.mode == SecurityMode.TransportWithMessageCredential) 
            {
                return this.messageSecurity.CreateSecurityBindingElement(this.Mode == SecurityMode.TransportWithMessageCredential, isReliableSessionEnabled, version); 
            }
            else
            {
                return null; 
            }
        } 
 
        internal static bool TryCreate(SecurityBindingElement sbe, UnifiedSecurityMode mode, HttpTransportSecurity transportSecurity, bool isReliableSessionEnabled, out WSHttpSecurity security)
        { 
            security = null;
            NonDualMessageSecurityOverHttp messageSecurity = null;
            SecurityMode securityMode = SecurityMode.None;
            if (sbe != null) 
            {
                mode &= UnifiedSecurityMode.Message | UnifiedSecurityMode.TransportWithMessageCredential; 
                securityMode = SecurityModeHelper.ToSecurityMode(mode); 
                DiagnosticUtility.DebugAssert(SecurityModeHelper.IsDefined(securityMode), string.Format("Invalid SecurityMode value: {0}.", mode.ToString()));
                if (!MessageSecurityOverHttp.TryCreate(sbe, securityMode == SecurityMode.TransportWithMessageCredential, isReliableSessionEnabled, out messageSecurity)) 
                {
                    return false;
                }
            } 
            else
            { 
                mode &= ~(UnifiedSecurityMode.Message | UnifiedSecurityMode.TransportWithMessageCredential); 
                securityMode = SecurityModeHelper.ToSecurityMode(mode);
            } 
            DiagnosticUtility.DebugAssert(SecurityModeHelper.IsDefined(securityMode), string.Format("Invalid SecurityMode value: {0}.", securityMode.ToString()));
            security = new WSHttpSecurity(securityMode, transportSecurity, messageSecurity);
            return true;
        } 

    } 
} 

// 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