BasicHttpSecurity.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 / BasicHttpSecurity.cs / 1 / BasicHttpSecurity.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------
namespace System.ServiceModel
{ 
    using System.ServiceModel.Channels;
    using System.Net.Security; 
    using System.ServiceModel.Security; 
    using System.ServiceModel.Configuration;
    using System.Diagnostics; 

    public sealed class BasicHttpSecurity
    {
        internal const BasicHttpSecurityMode DefaultMode = BasicHttpSecurityMode.None; 
        BasicHttpSecurityMode mode;
        HttpTransportSecurity transportSecurity; 
        BasicHttpMessageSecurity messageSecurity; 

        internal BasicHttpSecurity() : this(DefaultMode, new HttpTransportSecurity(), new BasicHttpMessageSecurity()) 
        {
        }

        BasicHttpSecurity(BasicHttpSecurityMode mode, HttpTransportSecurity transportSecurity, BasicHttpMessageSecurity messageSecurity) 
        {
            DiagnosticUtility.DebugAssert(BasicHttpSecurityModeHelper.IsDefined(mode), string.Format("Invalid BasicHttpSecurityMode value: {0}.", mode.ToString())); 
            this.Mode = mode; 
            this.transportSecurity = transportSecurity == null ? new HttpTransportSecurity() : transportSecurity;
            this.messageSecurity = messageSecurity == null ? new BasicHttpMessageSecurity() : messageSecurity; 
        }

        public BasicHttpSecurityMode Mode
        { 
            get { return this.mode; }
            set 
            { 
                if (!BasicHttpSecurityModeHelper.IsDefined(value))
                { 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
                }
                this.mode = value;
            } 
        }
 
        public HttpTransportSecurity Transport 
        {
            get { return this.transportSecurity; } 
        }

        public BasicHttpMessageSecurity Message
        { 
            get { return this.messageSecurity; }
        } 
 
        internal void EnableTransportSecurity(HttpsTransportBindingElement https)
        { 
            if (this.mode == BasicHttpSecurityMode.TransportWithMessageCredential)
            {
                this.transportSecurity.ConfigureTransportProtectionOnly(https);
            } 
            else
            { 
                this.transportSecurity.ConfigureTransportProtectionAndAuthentication(https); 
            }
        } 

        internal static void EnableTransportSecurity(HttpsTransportBindingElement https, HttpTransportSecurity transportSecurity)
        {
            HttpTransportSecurity.ConfigureTransportProtectionAndAuthentication(https, transportSecurity); 
        }
 
        internal void EnableTransportAuthentication(HttpTransportBindingElement http) 
        {
            this.transportSecurity.ConfigureTransportAuthentication(http); 
        }

        internal static bool IsEnabledTransportAuthentication(HttpTransportBindingElement http, HttpTransportSecurity transportSecurity)
        { 
            return HttpTransportSecurity.IsConfiguredTransportAuthentication(http, transportSecurity);
        } 
 
        internal void DisableTransportAuthentication(HttpTransportBindingElement http)
        { 
            this.transportSecurity.DisableTransportAuthentication(http);
        }

        internal SecurityBindingElement CreateMessageSecurity() 
        {
            if (this.mode == BasicHttpSecurityMode.Message 
                || this.mode == BasicHttpSecurityMode.TransportWithMessageCredential) 
            {
                return this.messageSecurity.CreateMessageSecurity(this.Mode == BasicHttpSecurityMode.TransportWithMessageCredential); 
            }
            else
            {
                return null; 
            }
        } 
 
        internal static bool TryCreate(SecurityBindingElement sbe, UnifiedSecurityMode mode, HttpTransportSecurity transportSecurity, out BasicHttpSecurity security)
        { 
            security = null;
            BasicHttpMessageSecurity messageSecurity = null;
            if (sbe != null)
            { 
                mode &= UnifiedSecurityMode.Message | UnifiedSecurityMode.TransportWithMessageCredential;
                bool isSecureTransportMode; 
                if (!BasicHttpMessageSecurity.TryCreate(sbe, out messageSecurity, out isSecureTransportMode)) 
                {
                    return false; 
                }
            }
            else
            { 
                mode &= ~(UnifiedSecurityMode.Message | UnifiedSecurityMode.TransportWithMessageCredential);
            } 
            BasicHttpSecurityMode basicHttpSecurityMode = BasicHttpSecurityModeHelper.ToSecurityMode(mode); 
            DiagnosticUtility.DebugAssert(BasicHttpSecurityModeHelper.IsDefined(basicHttpSecurityMode), string.Format("Invalid BasicHttpSecurityMode value: {0}.", basicHttpSecurityMode.ToString()));
            security = new BasicHttpSecurity(basicHttpSecurityMode, transportSecurity, messageSecurity); 

            return SecurityElement.AreBindingsMatching(security.CreateMessageSecurity(), sbe);
        }
    } 
}

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