2012-03-29 15 views
6

ServerManagerクラス(Microsoft.Web.Administration)を使用してIIS 7を実行しているサーバー上にアプリケーションを作成しています。アプリケーションが匿名認証を使用するかWindows認証アプリケーションベースでは、単にルートサイトの設定を変更するようにITに依頼することはできません。アプリケーションの内容は第三者に帰属しますので、アプリケーション内のweb.configファイルを変更することはできません。ServerManagerクラスを使用してIIS認証設定を構成する

Applicationクラスは有用なプロパティを公開していませんが、ServerManagerのGetApplicationHostConfigurationメソッドを使用して何かを取得できますか?

答えて

9

サイトのインターネットインフォメーションシステムの設定を変更したいと思うようですが、それが正しい場合は、次のような何かが動作するはずです。

using (ServerManager serverManager = new ServerManager()) 
{ 
    Configuration config = serverManager.GetWebConfiguration("Contoso"); 
    ConfigurationSection authorizationSection = config.GetSection("system.webServer/security/authorization"); 
    ConfigurationElementCollection authorizationCollection = authorizationSection.GetCollection(); 

    ConfigurationElement addElement = authorizationCollection.CreateElement("add"); 
    addElement["accessType"] = @"Allow"; 
    addElement["roles"] = @"administrators"; 
    authorizationCollection.Add(addElement); 

    serverManager.CommitChanges(); 
} 

上記のコードでは、グループ内の特定のユーザーが特定のサイトにアクセスできるようにする認可ルールを作成できます。この場合、サイトはContosoです。

これで、サイトの匿名認証が無効になります。その後、サイトの基本& Windows認証を有効にします。

using(ServerManager serverManager = new ServerManager()) 
{ 
    Configuration config = serverManager.GetApplicationHostConfiguration(); 

    ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Contoso"); 
    anonymousAuthenticationSection["enabled"] = false; 

    ConfigurationSection basicAuthenticationSection = config.GetSection("system.webServer/security/authentication/basicAuthentication", "Contoso"); 
    basicAuthenticationSection["enabled"] = true; 

    ConfigurationSection windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", "Contoso"); 
    windowsAuthenticationSection["enabled"] = true; 

    serverManager.CommitChanges(); 
} 

それともたい場合は、単にIISマネージャユーザーアカウントを追加することができます。他のアプリケーションを操作および管理するための特定の権限を設定することができます。

using (ServerManager serverManager = new ServerManager()) 
{ 
    Configuration config = serverManager.GetAdministrationConfiguration(); 

    ConfigurationSection authenticationSection = config.GetSection("system.webServer/management/authentication"); 
    ConfigurationElementCollection credentialsCollection = authenticationSection.GetCollection("credentials"); 
    ConfigurationElement addElement = credentialsCollection.CreateElement("add"); 
    addElement["name"] = @"ContosoUser"; 
    addElement["password"] = @"[email protected]"; 
    addElement["enabled"] = true; 
    credentialsCollection.Add(addElement); 

    serverManager.CommitChanges(); 
} 

インターネットインフォメーションシステムには多くの柔軟性があります。その非常に強力な。そこを参照しているドキュメントもかなり深いです。この例は、あなたの特定の使用法に適合させるのにまったく適していないか、少なくともあなたが望むことをするために理解のレベルを提供しています。

このヘルプは、hereから

関連する問題