HostingEnvironmentWrapper.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / System.ServiceModel.Activation / System / ServiceModel / Activation / HostingEnvironmentWrapper.cs / 1305376 / HostingEnvironmentWrapper.cs

                            //---------------------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------------------
namespace System.ServiceModel
{ 
    using System;
    using System.Runtime; 
    using System.Runtime.CompilerServices; 
    using System.Security;
    using System.Security.Permissions; 
    using System.Web;
    using System.Web.Hosting;

    // wrapper class that helps with partial trust analysis 
    // -- HostingEnvironment does a number of Demands and LinkDemands
    // -- this wrapper encapsulates access into "Safe" and "Unsafe" methods that do the appropriate asserts 
    // -- it is recommended that ALL HostingEnvironment access go through this class 
    // -- "Safe" methods are [SecurityCritical, SecurityTreatAsSafe] or not [SecurityCritical]
    // -- "Unsafe" methods are [SecurityCritical] 
    // -- because each method does precisely one access, we use declarative asserts for clarity
    static class HostingEnvironmentWrapper
    {
        public static string ApplicationVirtualPath 
        {
            get { return HostingEnvironment.ApplicationVirtualPath; } 
        } 

        public static bool IsHosted 
        {
            get { return HostingEnvironment.IsHosted; }
        }
 
        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")] 
        public static VirtualPathProvider VirtualPathProvider 
        {
            [SecuritySafeCritical] 
            get { return HostingEnvironment.VirtualPathProvider; }
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.", 
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical] 
        [MethodImpl(MethodImplOptions.NoInlining)] 
        public static void DecrementBusyCount()
        { 
            HostingEnvironment.DecrementBusyCount();
        }

        // demands SecurityPermission(ControlPrincipal) -- use Unsafe version to assert 
        public static IDisposable Impersonate()
        { 
            return HostingEnvironment.Impersonate(); 
        }
 
        // demands SecurityPermission(Unrestricted) -- use Unsafe version to assert
        public static IDisposable Impersonate(IntPtr token)
        {
            return HostingEnvironment.Impersonate(token); 
        }
 
        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.", 
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical] 
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void IncrementBusyCount()
        {
            HostingEnvironment.IncrementBusyCount(); 
        }
 
        public static string MapPath(string virtualPath) 
        {
            return HostingEnvironment.MapPath(virtualPath); 
        }

        public static string UnsafeApplicationID
        { 
            [Fx.Tag.SecurityNote(Critical = "Asserts SecurityPermission in order to call HostingEnvironment.get_ApplicationID.")]
            [SecurityCritical] 
            [AspNetHostingPermission(System.Security.Permissions.SecurityAction.Assert, Level = AspNetHostingPermissionLevel.High)] 
            get { return HostingEnvironment.ApplicationID; }
        } 

        [Fx.Tag.SecurityNote(Critical = "Asserts SecurityPermission in order to call HostingEnvironment.UnsafeImpersonate.")]
        [SecurityCritical]
        [SecurityPermission(SecurityAction.Assert, ControlPrincipal = true)] 
        public static IDisposable UnsafeImpersonate()
        { 
            return HostingEnvironment.Impersonate(); 
        }
 
        [Fx.Tag.SecurityNote(Critical = "Asserts SecurityPermission in order to call HostingEnvironment.UnsafeImpersonate.")]
        [SecurityCritical]
        [SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
        public static IDisposable UnsafeImpersonate(IntPtr token) 
        {
            return HostingEnvironment.Impersonate(token); 
        } 

        [Fx.Tag.SecurityNote(Critical = "Asserts SecurityPermission in order to call HostingEnvironment.RegisterObject.")] 
        [SecurityCritical]
        [SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
        public static void UnsafeRegisterObject(IRegisteredObject target)
        { 
            HostingEnvironment.RegisterObject(target);
        } 
 
        [Fx.Tag.SecurityNote(Critical = "Asserts SecurityPermission in order to call HostingEnvironment.UnregisterObject.")]
        [SecurityCritical] 
        [SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
        public static void UnsafeUnregisterObject(IRegisteredObject target)
        {
            HostingEnvironment.UnregisterObject(target); 
        }
 
        [Fx.Tag.SecurityNote(Critical = "Uses SecurityCritical method UnsafeImpersonate in order to check whether the service file exists.", 
            Safe = "Does not leak anything, does not let caller influence impersonation.")]
        [SecuritySafeCritical] 
        public static bool ServiceFileExists(string normalizedVirtualPath)
        {
            IDisposable unsafeImpersonate = null;
            try 
            {
                try 
                { 
                    try
                    { 
                    }
                    finally
                    {
                        unsafeImpersonate = HostingEnvironmentWrapper.UnsafeImpersonate(); 
                    }
                    return HostingEnvironment.VirtualPathProvider.FileExists(normalizedVirtualPath); 
                } 
                finally
                { 
                    if (null != unsafeImpersonate)
                    {
                        unsafeImpersonate.Dispose();
                    } 
                }
            } 
            catch 
            {
                throw; 
            }
        }

        [Fx.Tag.SecurityNote(Critical = "Uses SecurityCritical method UnsafeImpersonate in order to get service file.", 
            Safe = "Does not leak anything, does not let caller influence impersonation.")]
        [SecuritySafeCritical] 
        public static VirtualFile GetServiceFile(string normalizedVirtualPath) 
        {
            IDisposable unsafeImpersonate = null; 
            try
            {
                try
                { 
                    try
                    { 
                    } 
                    finally
                    { 
                        unsafeImpersonate = HostingEnvironmentWrapper.UnsafeImpersonate();
                    }
                    return HostingEnvironment.VirtualPathProvider.GetFile(normalizedVirtualPath);
                } 
                finally
                { 
                    if (null != unsafeImpersonate) 
                    {
                        unsafeImpersonate.Dispose(); 
                    }
                }
            }
            catch 
            {
                throw; 
            } 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------------------
namespace System.ServiceModel
{ 
    using System;
    using System.Runtime; 
    using System.Runtime.CompilerServices; 
    using System.Security;
    using System.Security.Permissions; 
    using System.Web;
    using System.Web.Hosting;

    // wrapper class that helps with partial trust analysis 
    // -- HostingEnvironment does a number of Demands and LinkDemands
    // -- this wrapper encapsulates access into "Safe" and "Unsafe" methods that do the appropriate asserts 
    // -- it is recommended that ALL HostingEnvironment access go through this class 
    // -- "Safe" methods are [SecurityCritical, SecurityTreatAsSafe] or not [SecurityCritical]
    // -- "Unsafe" methods are [SecurityCritical] 
    // -- because each method does precisely one access, we use declarative asserts for clarity
    static class HostingEnvironmentWrapper
    {
        public static string ApplicationVirtualPath 
        {
            get { return HostingEnvironment.ApplicationVirtualPath; } 
        } 

        public static bool IsHosted 
        {
            get { return HostingEnvironment.IsHosted; }
        }
 
        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.",
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")] 
        public static VirtualPathProvider VirtualPathProvider 
        {
            [SecuritySafeCritical] 
            get { return HostingEnvironment.VirtualPathProvider; }
        }

        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.", 
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical] 
        [MethodImpl(MethodImplOptions.NoInlining)] 
        public static void DecrementBusyCount()
        { 
            HostingEnvironment.DecrementBusyCount();
        }

        // demands SecurityPermission(ControlPrincipal) -- use Unsafe version to assert 
        public static IDisposable Impersonate()
        { 
            return HostingEnvironment.Impersonate(); 
        }
 
        // demands SecurityPermission(Unrestricted) -- use Unsafe version to assert
        public static IDisposable Impersonate(IntPtr token)
        {
            return HostingEnvironment.Impersonate(token); 
        }
 
        [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.", 
            Safe = "Does not leak control or mutable/harmful data, no potential for harm.")]
        [SecuritySafeCritical] 
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void IncrementBusyCount()
        {
            HostingEnvironment.IncrementBusyCount(); 
        }
 
        public static string MapPath(string virtualPath) 
        {
            return HostingEnvironment.MapPath(virtualPath); 
        }

        public static string UnsafeApplicationID
        { 
            [Fx.Tag.SecurityNote(Critical = "Asserts SecurityPermission in order to call HostingEnvironment.get_ApplicationID.")]
            [SecurityCritical] 
            [AspNetHostingPermission(System.Security.Permissions.SecurityAction.Assert, Level = AspNetHostingPermissionLevel.High)] 
            get { return HostingEnvironment.ApplicationID; }
        } 

        [Fx.Tag.SecurityNote(Critical = "Asserts SecurityPermission in order to call HostingEnvironment.UnsafeImpersonate.")]
        [SecurityCritical]
        [SecurityPermission(SecurityAction.Assert, ControlPrincipal = true)] 
        public static IDisposable UnsafeImpersonate()
        { 
            return HostingEnvironment.Impersonate(); 
        }
 
        [Fx.Tag.SecurityNote(Critical = "Asserts SecurityPermission in order to call HostingEnvironment.UnsafeImpersonate.")]
        [SecurityCritical]
        [SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
        public static IDisposable UnsafeImpersonate(IntPtr token) 
        {
            return HostingEnvironment.Impersonate(token); 
        } 

        [Fx.Tag.SecurityNote(Critical = "Asserts SecurityPermission in order to call HostingEnvironment.RegisterObject.")] 
        [SecurityCritical]
        [SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
        public static void UnsafeRegisterObject(IRegisteredObject target)
        { 
            HostingEnvironment.RegisterObject(target);
        } 
 
        [Fx.Tag.SecurityNote(Critical = "Asserts SecurityPermission in order to call HostingEnvironment.UnregisterObject.")]
        [SecurityCritical] 
        [SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
        public static void UnsafeUnregisterObject(IRegisteredObject target)
        {
            HostingEnvironment.UnregisterObject(target); 
        }
 
        [Fx.Tag.SecurityNote(Critical = "Uses SecurityCritical method UnsafeImpersonate in order to check whether the service file exists.", 
            Safe = "Does not leak anything, does not let caller influence impersonation.")]
        [SecuritySafeCritical] 
        public static bool ServiceFileExists(string normalizedVirtualPath)
        {
            IDisposable unsafeImpersonate = null;
            try 
            {
                try 
                { 
                    try
                    { 
                    }
                    finally
                    {
                        unsafeImpersonate = HostingEnvironmentWrapper.UnsafeImpersonate(); 
                    }
                    return HostingEnvironment.VirtualPathProvider.FileExists(normalizedVirtualPath); 
                } 
                finally
                { 
                    if (null != unsafeImpersonate)
                    {
                        unsafeImpersonate.Dispose();
                    } 
                }
            } 
            catch 
            {
                throw; 
            }
        }

        [Fx.Tag.SecurityNote(Critical = "Uses SecurityCritical method UnsafeImpersonate in order to get service file.", 
            Safe = "Does not leak anything, does not let caller influence impersonation.")]
        [SecuritySafeCritical] 
        public static VirtualFile GetServiceFile(string normalizedVirtualPath) 
        {
            IDisposable unsafeImpersonate = null; 
            try
            {
                try
                { 
                    try
                    { 
                    } 
                    finally
                    { 
                        unsafeImpersonate = HostingEnvironmentWrapper.UnsafeImpersonate();
                    }
                    return HostingEnvironment.VirtualPathProvider.GetFile(normalizedVirtualPath);
                } 
                finally
                { 
                    if (null != unsafeImpersonate) 
                    {
                        unsafeImpersonate.Dispose(); 
                    }
                }
            }
            catch 
            {
                throw; 
            } 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

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