次の方法で共有


方法 : リモート ASP.NET 構成ファイルにアクセスして変更する

更新 : 2007 年 11 月

System.Configuration 型と System.Web.Configuration 型を使用して、アプリケーションからリモート サーバー上の構成ファイルにアクセスできます。特に、リモート サーバー上の Microsoft Internet Information Services (IIS) アプリケーションまたはその子ディレクトリにある Machine.config ファイルまたは Web.config ファイルを開いて変更できます。このトピックでは、次のことを行います。

  • クライアント コンピュータがサーバー構成ファイルにアクセスできるようにリモート サーバーを設定する方法について説明します。

  • クライアント コンピュータ上で実行し、サーバー構成ファイルを読み取ったり変更したりできるコンソール アプリケーションを提供します。

  • セキュリティ上の考慮事項について説明します。

リモート サーバー上の構成ファイルにアクセスするには、クライアント コンピュータがサーバーと通信できることが必要です。この通信を有効にするには、リモート構成コンポーネントがサーバー上にインストールされている必要があります。

クライアント コンピュータは、そのリモート構成コンポーネントから ASP.NET 構成 API を呼び出すことにより、サーバー構成ファイルにアクセスします。詳細については、「ASP.NET リモート構成ファイルの編集」を参照してください。

ms228054.alert_note(ja-jp,VS.90).gifメモ :

要求される構成ファイルが存在しない場合、サーバー上で実行している .NET Framework が、指定したパスに適用される、継承された設定でのみ構成された構成ファイルを返します。アプリケーションが更新を要求した場合、新しいファイルが作成されます。コンソール アプリケーションを実行する際には、有効なサーバー名を入力する必要があります。これは、ASP.NET が、この名前をオペレーティング システムに直接渡すためです。また、同様に、ドメイン名プリフィックスの付いた完全修飾されたユーザー名を入力する必要もあります。

リモート サーバーをセットアップするには

  1. 次のコード例のように、ASP.NET IIS 登録ツール (Aspnet_regiis.exe) を使用して config+ コマンドで、サーバー上にリモート構成コンポーネントをインストールします。

    Aspnet_regiis config+
    
  2. IIS サーバーの "既定の Web サイト" の Web.config ファイルに、以下のような値が含まれていることをプログラムによってまたは手動で確認します。

    <appSettings>
        <add key="keyName1", value = "this entry value"/>
        <add key="keyName2", value = "this entry value"/>
        <add key="keyName3", value = "this entry value"/>
    </appSettings>
    

コンソール アプリケーションを使用してリモート構成ファイルを更新するには

  • 以下のコード例で示されているコンソール アプリケーションを実行して、リモート サーバー上の既定の Web サイトの Web.config ファイルを更新します。この例は、クライアント アプリケーションを実行しているユーザーがリモート サーバー上での管理者特権を持っていることを前提としています。次の 2 つのコマンドのうちのいずれかを使用できます。

    >remoteconfiguration machineName
    >remoteconfiguration machineName domainName\userName password
    

使用例

Imports System
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Collections.Specialized


' This example dDemonstrates how to access and modify remote 
' configuration files


Public Class RemoteConfiguration


    ' The application entry point.
    Public Shared Sub Main(ByVal args() As String) 

        ' This string  contains the name of 
        ' the remote server.
        Dim machineName As String = String.Empty

        ' Get the user's input.
        If args.Length > 0 Then
            ' Get the name of the remote server.
            ' The machine name must be in the format
            ' accepted by the operating system.
            machineName = args(0)

            ' Get the user name and password.
            If args.Length > 1 Then
                Dim userName As String = args(1)
                Dim password As String = String.Empty
                If args.Length > 2 Then
                    password = args(2)
                End If 
                ' Update the site configuration.
                UpdateConfiguration(machineName, userName, password)
            Else
                ' Update the site configuration.
                UpdateConfiguration(machineName)
            End If
        Else
            Console.WriteLine("Enter a valid server name.")
        End If

    End Sub 'Main


    ' Update the configuration file using the credentials
    ' of the user running this application. Tthen,
    ' call the routine that reads the configuration 
    ' settings.
    ' Note that the system issues an error if the user
    ' does not have administrative privileges on the server.
    Public Overloads Shared Sub _
    UpdateConfiguration(ByVal machineName As String)
        Try
            Console.WriteLine("MachineName:  [{0}]", machineName)
            Console.WriteLine("UserDomainName:  [{0}]", _
                Environment.UserDomainName)
            Console.WriteLine("UserName:  [{0}]", _
                Environment.UserName)

            ' Get the configuration.
            Dim config As Configuration = _
                WebConfigurationManager.OpenWebConfiguration("/", _
                "Default Web Site", Nothing, machineName)

            ' Add a new key/value pair to appSettings.
            Dim count As String = _
                config.AppSettings.Settings.Count.ToString()
            config.AppSettings.Settings.Add("key_" + count, _
                "value_" + count)

            ' Save changes to the file.
            config.Save()

            ' Read the new appSettings.
            ReadAppSettings(config)
        Catch err As Exception
            Console.WriteLine(err.ToString())
        End Try

    End Sub 'UpdateConfiguration


    ' Update the configuration file using the credentials
    ' of the passed user,  then call the routine that reads 
    ' the configuration settings.
    ' Note that the system issues an error if the user
    ' does not have administrative privileges on the server.
    Public Overloads Shared Sub UpdateConfiguration(ByVal _
        machineName As String, ByVal userName As String, ByVal _
        password As String)
        Try
            Console.WriteLine("MachineName:  [{0}]", machineName)
            Console.WriteLine("UserName:  [{0}]", userName)
            Console.WriteLine("Password:  [{0}]", password)

            ' Get the configuration.
            Dim config As Configuration = _
                WebConfigurationManager.OpenWebConfiguration("/", _
                "Default Web Site", Nothing, machineName, _
                userName, password)


            ' Add a new key/value pair to appSettings
            Dim count As String = _
                config.AppSettings.Settings.Count.ToString()
            config.AppSettings.Settings.Add("key_" + _
                count, "value_" + count)
            ' Save to the file,
            config.Save()

            ' Read the new appSettings.
            ReadAppSettings(config)

        Catch err As Exception
            Console.WriteLine(err.ToString())
        End Try

    End Sub 'UpdateConfiguration


    ' Read the appSettings on the remote server.
    Public Shared Sub ReadAppSettings(ByVal config As Configuration) 
        Try
            Console.WriteLine("--- Printing appSettings at [{0}] ---", _
                config.FilePath)
            Console.WriteLine("Count: [{0}]", _
                config.AppSettings.Settings.Count)
            Dim key As String
            For Each key In  config.AppSettings.Settings.AllKeys
                Console.WriteLine("  [{0}] = [{1}]", _
                    key, config.AppSettings.Settings(key).Value)
            Next key
            Console.WriteLine()

        Catch err As Exception
            Console.WriteLine(err.ToString())
        End Try

    End Sub 'ReadAppSettings
End Class 'RemoteConfiguration

using System;
using System.Configuration;
using System.Web.Configuration;
using System.Collections.Specialized;

namespace Samples.AspNet
{
    // This example dDemonstrates how to access and modify remote 
   // configuration files
    public class RemoteConfiguration
    {

        // The application entry point.
        public static void Main(string[] args)
        {
            // This string  contains the name of 
            // the remote server.
            string machineName = string.Empty;

            // Get the user's input.
            if (args.Length > 0)
            {
                // Get the name of the remote server.
                // The machine name must be in the format
                // accepted by the operating system.
                machineName = args[0];

                // Get the user name and password.
                if (args.Length > 1)     
                {                
                    string userName = args[1];
                    string password = string.Empty;
                    if (args.Length > 2)
                        password = args[2];

                    // Update the site configuration.
                    UpdateConfiguration(machineName, userName, 
                        password);
                }
                else
                { 
                    // Update the site configuration.
                    UpdateConfiguration(machineName);
                }
            }
            else
            {
                Console.WriteLine("Enter a valid server name.");
            }
        }

        // Update the configuration file using the credentials
        // of the user running this application then
        // call the routine that reads the configuration 
        // settings.
        // Note that the system issues an error if the user
        // does not have administrative privileges on the server.
        public static void UpdateConfiguration(string machineName)
        {
            try
            {
                Console.WriteLine("MachineName:  [{0}]", machineName);
                Console.WriteLine("UserDomainName:  [{0}]", 
                    Environment.UserDomainName);
                Console.WriteLine("UserName:  [{0}]", 
                    Environment.UserName);

                // Get the configuration.
                Configuration config = 
                    WebConfigurationManager.OpenWebConfiguration(
                    "/", "Default Web Site", null, machineName);

                // Add a new key/value pair to appSettings.
                string count = 
                    config.AppSettings.Settings.Count.ToString();
                config.AppSettings.Settings.Add("key_" + count, "value_" + count);
                // Save to the file,
                config.Save();

                // Read the new appSettings.
                ReadAppSettings(config);
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
            }
        }

         // Update the configuration file using the credentials
         // of the passed user. Tthen,
     // call the routine that reads the configuration settings.
        // Note that the system issues an error if the user
        // does not have administrative privileges on the server.
        public static void UpdateConfiguration(string machineName, 
            string userName, string password)
        {
            try
            {
                Console.WriteLine("MachineName:  [{0}]", machineName);
                Console.WriteLine("UserName:  [{0}]", userName);
                Console.WriteLine("Password:  [{0}]", password);

                // Get the configuration.
                Configuration config = 
                    WebConfigurationManager.OpenWebConfiguration(
                    "/", "Default Web Site", null, 
                    machineName, userName, password);


                // Add a new key/value pair to appSettings
                string count =
                    config.AppSettings.Settings.Count.ToString();
                config.AppSettings.Settings.Add("key_" + count, "value_" + count);

             // Save changes to the file.
                config.Save();

                // Read the new appSettings.
                ReadAppSettings(config);

            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
            }
        }

        // Read the appSettings on the remote server.
        public static void ReadAppSettings(
            Configuration config)
        {
            try
            {
                Console.WriteLine("--- Printing appSettings at [{0}] ---", 
                    config.FilePath);
                Console.WriteLine("Count: [{0}]", 
                    config.AppSettings.Settings.Count);
                foreach (string key in config.AppSettings.Settings.AllKeys)
                {
                    Console.WriteLine("  [{0}] = [{1}]", 
                        key, config.AppSettings.Settings[key].Value);
                }
                Console.WriteLine();

            }           
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
            }
        }
    }

}

上記のコード例では、サーバーの既定の Web サイト用に構成されている appSettings 要素の値を読み取って変更します。次に、これらの値がコンソール上に表示されます。

このコードでは、次のメソッドが使用されます。

  • OpenWebConfiguration。Web アプリケーションの構成ファイルを Configuration オブジェクトとして開きます。このユーザーには、リモート サーバー上での管理者特権が必要です。

  • OpenWebConfiguration。Web アプリケーションの構成ファイルを Configuration オブジェクトとして開きます。パラメータ リストで指定されるユーザーには、リモート サーバー上での管理者特権が必要です。

  • AppSettings。既定のサイト関連セクションにアクセスします。

  • GetSection。既定のサイト ID セクションにアクセスします。

コードのコンパイル方法

コンソール アプリケーションをコンパイルするには、次のコマンドを使用する必要があります。

vbc /out:RemoteConfiguration.exe /t:exe RemoteConfiguration.vb
/r:System.Web.dll /r:System.Configuration.dll
csc /out: RemoteConfiguration.exe /t:exe RemoteConfiguration.cs
/r:System.Web.dll /r:System.Configuration.dll

セキュリティ

リモート サーバー上で構成ファイルにアクセスするには、アプリケーションにリモート コンピュータ上での管理者特権が必要です。

ms228054.alert_note(ja-jp,VS.90).gifメモ :

これは、ローカル構成ファイルにアクセスする場合より厳しい要件です。ローカル ファイルにアクセスする場合は、アプリケーションに必要なのは、読み書き特権と、パスを解決するための IIS メタベースへの読み取り権限のみです。

参照

概念

ASP.NET リモート構成ファイルの編集

構成クラスの使用

ASP.NET 構成の概要

参照

System.Configuration

System.Web.Configuration

ASP.NET IIS 登録ツール (Aspnet_regiis.exe)

その他の技術情報

アプリケーションの設定