Virtual Directory Defaults <virtualDirectoryDefaults>

Overview

The <virtualDirectoryDefaults> element of the <application> element specifies the default settings for all virtual directories in the parent application. The <virtualDirectoryDefaults> element is useful for defining the common attributes that you want all virtual directories in the parent application to share implicitly when they do not have explicitly-defined attributes.

Note: If the same attribute or child element is configured in both the <virtualDirectoryDefaults> section and in the <virtualDirectory> section for a specific virtual directory, the configuration in the <virtualDirectory> section is used for that virtual directory.

Compatibility

Version Notes
IIS 7.5 The <virtualDirectoryDefaults> element was not modified in IIS 7.5.
IIS 7.0 The <virtualDirectoryDefaults> element of the <application> element was introduced in IIS 7.0.
IIS 6.0 N/A

Setup

The <virtualDirectoryDefaults> element of the <application> element is included in the default installation of IIS 7.

How To

How to configure the default virtual directory credentials for an application

  1. Open Internet Information Services (IIS) Manager:
    • If you are using Windows Server 2008 or Windows Server 2008 R2:
      • On the taskbar, click Start, point to Administrative Tools, and then click Internet Information Services (IIS) Manager.
    • If you are using Windows Vista or Windows 7:
      • On the taskbar, click Start, and then click Control Panel.
      • Double-click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
  2. In the Connections pane, expand the server name, expand the Sites node, expand the node for your site, then click the name of the application.
  3. In the site's Home pane, click View Virtual Directories in the Actions pane.

  4. In the Virtual Directories pane, click Set Virtual Directory Defaults... in the Actions pane.

  5. In the Virtual Directories Defaults dialog box, specify your default virtual directory credentials for the application, and then click OK.

Configuration

Attributes

Attribute Description
allowSubDirConfig Optional Boolean attribute.

Specifies whether IIS looks for Web.config files in content directories lower than the current level (true) or does not look for Web.config files in content directories lower than the current level (false).

The default value is true.
logonMethod Optional enum attribute.

Specifies the default logon method for all virtual directories in applications in the parent site.

The logonMethod attribute can be one of the following possible values. The default is ClearText.
Value Description
Batch This logon type is intended for batch servers, where processes may be executing on behalf of a user without that user's direct intervention.

The numeric value is 1.
ClearText This logon type preserves the name and password in the authentication package, which allows the server to make connections to other network servers while impersonating the client.

The numeric value is 3.
Interactive This logon type is intended for users who will be using the computer interactively .

The numeric value is 0.
Network This logon type is intended for high performance servers that authenticate plaintext passwords. Credentials are not cached for this logon type.

The numeric value is 2.
For more information about these values, see LogonUser on the MSDN site.
password Optional string attribute.

Specifies the password associated with the user name.

Note: To avoid storing unencrypted password strings in configuration files, always use AppCmd.exe or IIS Manager to enter passwords. If you use these management tools, the password strings will be encrypted automatically before they are written to the XML configuration files. This provides better password security than storing unencrypted passwords.
path Optional string attribute.

Specifies the default virtual path of all virtual directories in applications in the parent site.
physicalPath Optional string attribute.

Specifies the default physical path of all virtual directories in applications in the parent site.
userName Optional string attribute.

Specifies the default user name of an account that can access configuration files and content for all virtual directories in applications in the parent site.

Child Elements

None.

Configuration Sample

The following configuration sample specifies the default logon method for virtual directories in the "/MyApp" application of the Default Web Site.

<sites>
   <site name="Default Web Site" id="1">
      <application path="/">
         <virtualDirectory path="/" physicalPath="C:\Inetpub\wwwroot" />
      </application>
      <application path="/MyApp" applicationPool="DefaultAppPool">
         <virtualDirectory path="/" physicalPath="C:\inetpub\myapp" />
         <virtualDirectoryDefaults logonMethod="Network" />
      </application>
      <bindings>
         <binding protocol="http" bindingInformation="*:80:" />
      </bindings>
  </site>
</sites>

Sample Code

The following code samples specify the default logon method for virtual directories in the "/MyApp" application of the Default Web Site.

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/sites "/[name='Default Web Site'].[path='/MyApp'].virtualDirectoryDefaults.logonMethod:Network" /commit:apphost

Note: You must be sure to set the commit parameter to apphost when you use AppCmd.exe to configure these settings. This commits the configuration settings to the appropriate location section in the ApplicationHost.config file.

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {

         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");

         ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
         ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"Default Web Site");
         if (siteElement == null) throw new InvalidOperationException("Element not found!");

         ConfigurationElementCollection siteCollection = siteElement.GetCollection();
         ConfigurationElement applicationElement = FindElement(siteCollection, "application", "path", @"/MyApp");
         if (applicationElement == null) throw new InvalidOperationException("Element not found!");

         ConfigurationElement virtualDirectoryDefaultsElement = applicationElement.GetChildElement("virtualDirectoryDefaults");
         virtualDirectoryDefaultsElement["logonMethod"] = @"Network";

         serverManager.CommitChanges();
      }
   }

   private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
   {
      foreach (ConfigurationElement element in collection)
      {
         if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
         {
            bool matches = true;
            for (int i = 0; i < keyValues.Length; i += 2)
            {
               object o = element.GetAttributeValue(keyValues[i]);
               string value = null;
               if (o != null)
               {
                  value = o.ToString();
               }
               if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
               {
                  matches = false;
                  break;
               }
            }
            if (matches)
            {
               return element;
            }
         }
      }
      return null;
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample

   Sub Main()

      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")

      Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection
      Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "Default Web Site")
      If (siteElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If

      Dim siteCollection As ConfigurationElementCollection = siteElement.GetCollection
      Dim applicationElement As ConfigurationElement = FindElement(siteCollection, "application", "path", "/MyApp")
      If (applicationElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If

      Dim virtualDirectoryDefaultsElement As ConfigurationElement = applicationElement.GetChildElement("virtualDirectoryDefaults")
      virtualDirectoryDefaultsElement("logonMethod") = "Network"

      serverManager.CommitChanges()
   End Sub

   Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
      For Each element As ConfigurationElement In collection
         If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
            Dim matches As Boolean = True
            Dim i As Integer
            For i = 0 To keyValues.Length - 1 Step 2
               Dim o As Object = element.GetAttributeValue(keyValues(i))
               Dim value As String = Nothing
               If (Not (o) Is Nothing) Then
                  value = o.ToString
               End If
               If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
                  matches = False
                  Exit For
               End If
            Next
            If matches Then
               Return element
            End If
         End If
      Next
      Return Nothing
   End Function

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");

var sitesCollection = sitesSection.Collection;
var siteElementPos = FindElement(sitesCollection, "site", ["name", "Default Web Site"]);
if (siteElementPos == -1) throw "Element not found!";
var siteElement = sitesCollection.Item(siteElementPos);

var siteCollection = siteElement.Collection;
var applicationElementPos = FindElement(siteCollection, "application", ["path", "/MyApp"]);
if (applicationElementPos == -1) throw "Element not found!";
var applicationElement = siteCollection.Item(applicationElementPos);

var virtualDirectoryDefaultsElement = applicationElement.ChildElements.Item("virtualDirectoryDefaults");
virtualDirectoryDefaultsElement.Properties.Item("logonMethod").Value = "Network";

adminManager.CommitChanges();

function FindElement(collection, elementTagName, valuesToMatch) {
   for (var i = 0; i < collection.Count; i++) {
      var element = collection.Item(i);
      if (element.Name == elementTagName) {
         var matches = true;
         for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
            var property = element.GetPropertyByName(valuesToMatch[iVal]);
            var value = property.Value;
            if (value != null) {
               value = value.toString();
            }
            if (value != valuesToMatch[iVal + 1]) {
               matches = false;
               break;
            }
         }
         if (matches) {
            return i;
         }
      }
   }
   return -1;
}

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")

Set sitesCollection = sitesSection.Collection
siteElementPos = FindElement(sitesCollection, "site", Array("name", "Default Web Site"))
If (siteElementPos = -1) Then
   WScript.Echo "Element not found!"
   WScript.Quit
End If
Set siteElement = sitesCollection.Item(siteElementPos)

Set siteCollection = siteElement.Collection
applicationElementPos = FindElement(siteCollection, "application", Array("path", "/MyApp"))
If (applicationElementPos = -1) Then
   WScript.Echo "Element not found!"
   WScript.Quit
End If
Set applicationElement = siteCollection.Item(applicationElementPos)

Set virtualDirectoryDefaultsElement = applicationElement.ChildElements.Item("virtualDirectoryDefaults")
virtualDirectoryDefaultsElement.Properties.Item("logonMethod").Value = "Network"

adminManager.CommitChanges()

Function FindElement(collection, elementTagName, valuesToMatch)
   For i = 0 To CInt(collection.Count) - 1
      Set element = collection.Item(i)
      If element.Name = elementTagName Then
         matches = True
         For iVal = 0 To UBound(valuesToMatch) Step 2
            Set property = element.GetPropertyByName(valuesToMatch(iVal))
            value = property.Value
            If Not IsNull(value) Then
               value = CStr(value)
            End If
            If Not value = CStr(valuesToMatch(iVal + 1)) Then
               matches = False
               Exit For
            End If
         Next
         If matches Then
            Exit For
         End If
      End If
   Next
   If matches Then
      FindElement = i
   Else
      FindElement = -1
   End If
End Function
Deprecated Elements