Turning off ASP.Net WebForms authentication for one sub-directory

前端 未结 7 2012
臣服心动
臣服心动 2020-12-15 07:21

I have a large enterprise application containing both WebForms and MVC pages. It has existing authentication and authorisation settings that I don\'t want to change.

7条回答
  •  没有蜡笔的小新
    2020-12-15 07:56

    After looking at your comments to my previous answer, I wondered if you could have your web app automate the deployment of an application on your REST directory. That would allow you to have the benefits of a second application, and would also reduce the deployment burden on your system admins.

    My thought was that you could put a routine into the Application_Start method of the global.asax that would check that the REST directory exists, and that it does not already have an application associated with it. If the test returns true, then the process of associating a new application to the REST directory occurs.

    Another thought I had was that you could use WIX (or another deployment technology) to build a install package that your admins could run to create the application, however I don't think that's as automatic as having the app configure its dependency.

    Below, I've included a sample implementation that checks IIS for a given directory and applies an application to it if it does not already have one. The code was tested with IIS 7, but should work on IIS 6 as well.

    //This is part of global.asax.cs
    //This approach may require additional user privileges to query IIS
    
    //using System.DirectoryServices;
    //using System.Runtime.InteropServices;
    
    protected void Application_Start(object sender, EventArgs evt)
    {
      const string iisRootUri = "IIS://localhost/W3SVC/1/Root";
      const string restPhysicalPath = @"C:\inetpub\wwwroot\Rest";
      const string restVirtualPath = "Rest";
    
      if (!Directory.Exists(restPhysicalPath))
      {
        // there is no rest path, so do nothing
        return;
      }
    
      using (var root = new DirectoryEntry(iisRootUri))
      {
        DirectoryEntries children = root.Children;
    
        try
        {
          using (DirectoryEntry rest = children.Find(restVirtualPath, root.SchemaClassName))
          {
            // the above call throws an exception if the vdir does not exist
            return;
          }
        }
        catch (COMException e)
        {
          // something got unlinked incorrectly, kill the vdir and application
          foreach (DirectoryEntry entry in children)
          {
            if (string.Compare(entry.Name, restVirtualPath, true) == 0)
            {
              entry.DeleteTree();
            }     
          }
        }
        catch (DirectoryNotFoundException e)
        {
          // the vdir and application do not exist, add them below
        }
    
        using (DirectoryEntry rest = children.Add(restVirtualPath, root.SchemaClassName))
        {
          rest.CommitChanges();
          rest.Properties["Path"].Value = restPhysicalPath;
          rest.Properties["AccessRead"].Add(true);
          rest.Properties["AccessScript"].Add(true);
          rest.Invoke("AppCreate2", true);
          rest.Properties["AppFriendlyName"].Add(restVirtualPath);
          rest.CommitChanges();
        }
      }
    }
    

    Portions of this code came from here. Good luck with your app!

提交回复
热议问题