IIS で管理を行う C# コードを書き込もうとしています。
Web アプリケーション用の Microsoft.Web.Administration.Application のインスタンスがあります。
このオブジェクトを使用して、「認証」の下の IIS と同じ情報を取得するにはどうすればよいですか?
次のようなものを含むリストを期待しています:
- 匿名認証 (無効)
- ASP.NET 偽装 (有効)
- フォーム認証 (無効)
- Windows 認証 (有効)
前もってありがとう、スティーブン
Configuration configuration = Application.GetWebConfiguration();
次に使用します
configuration.GetMetadata("availableSections")
...セクションのリストを取得します。認証セクションは「system.webServer/security/authentication/」で始まるので、それらのセクションを検索します。
それから電話する
Application.GetWebConfiguration().GetSection([SECTION]).GetAttributeValue("enabled")
匿名認証のセクションは「anonymousAuthentication」と呼ばれ、Windows 認証のセクションは「windowsAuthentication」です。
フォーム認証もありますが、これについては後で説明します。したがって、コードは次のようになります。
const string authenticationPrefix = "system.webServer/security/authentication/";
private Dictionary<string, string> authenticationDescriptions = new Dictionary<string,string>()
{
{"anonymousAuthentication", "Anonymous Authentication"},
{"windowsAuthentication", "Windows Authentication"},
};
Configuration configuration = application.GetWebConfiguration();
IEnumerable<string> authentications = ((String)configuration.GetMetadata("availableSections")).Split(',').Where(
authentication => authentication.StartsWith(authenticationPrefix));
foreach (string authentication in authentications)
{
string authName = authentication.Substring(authenticationPrefix.Length);
string authDesc;
authenticationDescriptions.TryGetValue(authName, out authDesc);
if(String.IsNullOrEmpty(authDesc))
continue;
authenticationCheckedListBox.Items.Add(authDesc, (bool)configuration.GetSection(authentication).GetAttributeValue("enabled"));
}
フォーム認証のコードは次のとおりです
enum FormsAuthentication { Off = 1, On = 3 };
ConfigurationSection authenticationSection = configuration.GetSection("system.web/authentication");
authenticationCheckedListBox.Items.Add("Forms Authentication", (FormsAuthentication)authenticationSection.GetAttributeValue("mode") == FormsAuthentication.On);