如何:远程访问和修改 ASP.NET 配置文件
更新:2007 年 11 月
System.Configuration 和 System.Web.Configuration 类型允许应用程序访问远程服务器上的配置文件。具体来说,可以打开和修改远程服务器上任意 Microsoft Internet 信息服务 (IIS) 应用程序或其子目录中的 Machine.config 或 Web.config 配置文件。本主题:
演示如何设置远程服务器以允许客户端计算机访问服务器配置文件。
提供可读取或修改服务器配置文件的控制台应用程序,以在客户端计算机上运行。
讨论要考虑的安全注意事项。
若要访问远程服务器上的配置文件,客户端计算机必须能够与该服务器进行通信。若要实现此通信,必须在该服务器上安装远程配置组件。
然后,客户端计算机通过远程配置组件调用 ASP.NET 配置 API,从而访问服务器配置文件。有关更多信息,请参见编辑 ASP.NET 远程配置文件。
说明: |
---|
如果请求的配置文件不存在,则正在服务器上运行的 .NET Framework 将返回一个配置文件,该文件完全由应用于指定路径的继承设置组成。如果应用程序请求更新,则会创建一个新文件。在运行控制台应用程序时,必须输入一个有效的服务器名称。这是因为 ASP.NET 会将此名称直接传递给操作系统。同样,必须输入一个带有域名前缀的完全限定的用户名。 |
若要设置远程服务器
带 config+ 命令使用 ASP.NET IIS 注册工具 (Aspnet_regiis.exe) 在服务器上安装远程配置组件,如下面的代码所示。
Aspnet_regiis config+
以编程方式或手动确保 IIS 服务器“默认网站”的 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.config 文件。此示例假定运行客户端应用程序的用户具有对远程服务器的管理权限。可以使用下面两个命令中的任意一个。
>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());
}
}
}
}
上一代码示例读取并修改为服务器“默认网站”配置的 appSettings 元素的值。然后,在控制台上显示这些值。
该代码使用了下列方法:
OpenWebConfiguration,用于将 Web 应用程序的配置文件作为 Configuration 对象打开。注意,涉及的用户必须具有远程服务器上的管理权限。
OpenWebConfiguration,用于将 Web 应用程序的配置文件作为 Configuration 对象打开。注意,该参数列表中指定的用户必须具有远程服务器上的管理权限。
AppSettings,用于访问默认的站点相关节。
GetSection,用于访问默认的站点标识节。
编译代码
若要编译控制台应用程序,必须使用以下命令。
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
安全性
若要访问远程服务器上的配置文件,应用程序必须具有该远程服务器上的管理权限。
说明: |
---|
此要求比访问本地配置文件所需的要求更严格。若要访问本地文件,应用程序只需要读/写权限以及对 IIS 元数据库的读访问权限以解析路径。 |
请参见
概念
参考
ASP.NET IIS 注册工具 (Aspnet_regiis.exe)