private static PolicyLevel GetPartialTrustPolicyLevel(TrustSection trustSection, SecurityPolicySection securityPolicySection, CompilationSection compilationSection, string physicalPath, VirtualPath virtualPath)
 {
     if ((securityPolicySection == null) || (securityPolicySection.TrustLevels[trustSection.Level] == null))
     {
         throw new ConfigurationErrorsException(System.Web.SR.GetString("Unable_to_get_policy_file", new object[] { trustSection.Level }), string.Empty, 0);
     }
     string policyFileExpanded = securityPolicySection.TrustLevels[trustSection.Level].PolicyFileExpanded;
     if ((policyFileExpanded == null) || !System.Web.Util.FileUtil.FileExists(policyFileExpanded))
     {
         throw new HttpException(System.Web.SR.GetString("Unable_to_get_policy_file", new object[] { trustSection.Level }));
     }
     PolicyLevel level = null;
     string path = System.Web.Util.FileUtil.RemoveTrailingDirectoryBackSlash(physicalPath);
     string newValue = HttpRuntime.MakeFileUrl(path);
     string tempDirectory = null;
     string tempDirAttribName = null;
     string configFileName = null;
     int configLineNumber = 0;
     if ((compilationSection != null) && !string.IsNullOrEmpty(compilationSection.TempDirectory))
     {
         tempDirectory = compilationSection.TempDirectory;
         compilationSection.GetTempDirectoryErrorInfo(out tempDirAttribName, out configFileName, out configLineNumber);
     }
     if (tempDirectory != null)
     {
         tempDirectory = tempDirectory.Trim();
         if (!Path.IsPathRooted(tempDirectory))
         {
             tempDirectory = null;
         }
         else
         {
             try
             {
                 tempDirectory = new DirectoryInfo(tempDirectory).FullName;
             }
             catch
             {
                 tempDirectory = null;
             }
         }
         if (tempDirectory == null)
         {
             throw new ConfigurationErrorsException(System.Web.SR.GetString("Invalid_temp_directory", new object[] { tempDirAttribName }), configFileName, configLineNumber);
         }
         try
         {
             Directory.CreateDirectory(tempDirectory);
             goto Label_0165;
         }
         catch (Exception exception)
         {
             throw new ConfigurationErrorsException(System.Web.SR.GetString("Invalid_temp_directory", new object[] { tempDirAttribName }), exception, configFileName, configLineNumber);
         }
     }
     tempDirectory = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "Temporary ASP.NET Files");
 Label_0165:
     if (!Util.HasWriteAccessToDirectory(tempDirectory))
     {
         if (!Environment.UserInteractive)
         {
             throw new HttpException(System.Web.SR.GetString("No_codegen_access", new object[] { Util.GetCurrentAccountName(), tempDirectory }));
         }
         tempDirectory = Path.Combine(Path.GetTempPath(), "Temporary ASP.NET Files");
     }
     string str7 = AppManagerAppDomainFactory.ConstructSimpleAppName(VirtualPath.GetVirtualPathStringNoTrailingSlash(virtualPath));
     string str9 = HttpRuntime.MakeFileUrl(System.Web.Util.FileUtil.RemoveTrailingDirectoryBackSlash(Path.Combine(tempDirectory, str7)));
     string originUrl = trustSection.OriginUrl;
     FileStream stream = new FileStream(policyFileExpanded, FileMode.Open, FileAccess.Read);
     StreamReader reader = new StreamReader(stream, Encoding.UTF8);
     string str = reader.ReadToEnd();
     reader.Close();
     str = str.Replace("$AppDir$", path).Replace("$AppDirUrl$", newValue).Replace("$CodeGen$", str9);
     if (originUrl == null)
     {
         originUrl = string.Empty;
     }
     str = str.Replace("$OriginHost$", originUrl);
     string gacLocation = null;
     if (str.IndexOf("$Gac$", StringComparison.Ordinal) != -1)
     {
         gacLocation = HttpRuntime.GetGacLocation();
         if (gacLocation != null)
         {
             gacLocation = HttpRuntime.MakeFileUrl(gacLocation);
         }
         if (gacLocation == null)
         {
             gacLocation = string.Empty;
         }
         str = str.Replace("$Gac$", gacLocation);
     }
     level = SecurityManager.LoadPolicyLevelFromString(str, PolicyLevelType.AppDomain);
     if (level == null)
     {
         throw new ConfigurationErrorsException(System.Web.SR.GetString("Unable_to_get_policy_file", new object[] { trustSection.Level }));
     }
     if (gacLocation != null)
     {
         CodeGroup rootCodeGroup = level.RootCodeGroup;
         bool flag = false;
         foreach (CodeGroup group2 in rootCodeGroup.Children)
         {
             if (group2.MembershipCondition is GacMembershipCondition)
             {
                 flag = true;
                 break;
             }
         }
         if (!flag && (rootCodeGroup is FirstMatchCodeGroup))
         {
             FirstMatchCodeGroup group3 = (FirstMatchCodeGroup) rootCodeGroup;
             if (!(group3.MembershipCondition is AllMembershipCondition) || !(group3.PermissionSetName == "Nothing"))
             {
                 return level;
             }
             PermissionSet permSet = new PermissionSet(PermissionState.Unrestricted);
             CodeGroup group = new UnionCodeGroup(new GacMembershipCondition(), new PolicyStatement(permSet));
             CodeGroup group5 = new FirstMatchCodeGroup(rootCodeGroup.MembershipCondition, rootCodeGroup.PolicyStatement);
             foreach (CodeGroup group6 in rootCodeGroup.Children)
             {
                 if (((group6 is UnionCodeGroup) && (group6.MembershipCondition is UrlMembershipCondition)) && (group6.PolicyStatement.PermissionSet.IsUnrestricted() && (group != null)))
                 {
                     group5.AddChild(group);
                     group = null;
                 }
                 group5.AddChild(group6);
             }
             level.RootCodeGroup = group5;
         }
     }
     return level;
 }
示例#2
0
 private void SetUpCodegenDirectory(CompilationSection compilationSection)
 {
   AppDomain domain = Thread.GetDomain();
   string path2 = AppManagerAppDomainFactory.ConstructSimpleAppName(HttpRuntime.AppDomainAppVirtualPath);
   string str1 = (string) null;
   string tempDirAttribName = (string) null;
   string configFileName = (string) null;
   int configLineNumber = 0;
   if (compilationSection != null && !string.IsNullOrEmpty(compilationSection.TempDirectory))
   {
     str1 = compilationSection.TempDirectory;
     compilationSection.GetTempDirectoryErrorInfo(out tempDirAttribName, out configFileName, out configLineNumber);
   }
   string str2;
   if (str1 != null)
   {
     string path = str1.Trim();
     if (!Path.IsPathRooted(path))
     {
       str2 = (string) null;
     }
     else
     {
       try
       {
         str2 = new DirectoryInfo(path).FullName;
       }
       catch
       {
         str2 = (string) null;
       }
     }
     if (str2 == null)
     {
       throw new ConfigurationErrorsException(System.Web.SR.GetString("Invalid_temp_directory", new object[1]
       {
         (object) tempDirAttribName
       }), configFileName, configLineNumber);
     }
     else
     {
       try
       {
         Directory.CreateDirectory(str2);
       }
       catch (Exception ex)
       {
         throw new ConfigurationErrorsException(System.Web.SR.GetString("Invalid_temp_directory", new object[1]
         {
           (object) tempDirAttribName
         }), ex, configFileName, configLineNumber);
       }
     }
   }
   else
     str2 = Path.Combine(HttpRuntime.s_installDirectory, "Temporary ASP.NET Files");
   if (!Util.HasWriteAccessToDirectory(str2))
   {
     if (!Environment.UserInteractive)
       throw new HttpException(System.Web.SR.GetString("No_codegen_access", (object) Util.GetCurrentAccountName(), (object) str2));
     else
       str2 = Path.Combine(Path.GetTempPath(), "Temporary ASP.NET Files");
   }
   this._tempDir = str2;
   string path1 = Path.Combine(str2, path2);
   domain.SetDynamicBase(path1);
   this._codegenDir = Thread.GetDomain().DynamicDirectory;
   Directory.CreateDirectory(this._codegenDir);
 }