检测所有选定的IIS身份验证模式

时间:2015-01-13 09:38:00

标签: c# asp.net authentication iis

出于某些特殊原因,我需要检测IIS(7)上运行的Web应用程序的所有选定/启用身份验证模式。检查IPersonal对象或System.Web.Configuration.AuthenticationMode枚举没有帮助,原因如下:它们仅在未使用身份验证,表单身份验证或Windows身份验证时显示。 Web.config也是如此。

我真正需要的是"所有启用的身份验证模式列表"。例如,如果同时启用了Anonymous AuthenticationForms Authentication,我需要以某种方式找到它。

我还试图使用Microsoft.Web.Administration命名空间来阅读这些设置,但我无法找到有用的东西。

有没有办法以编程方式获取特定Web应用程序的身份验证模式?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

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

private void CheckAuthenticationModes()
{
  using(ServerManager serverManager = new ServerManager()) { 
     Configuration config = serverManager.GetApplicationHostConfiguration();
     //replace myWebApp with your webapplication name.   
     ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "myWebApp");
     var anonymousAuthentication= anonymousAuthenticationSection["enabled"];

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

  }
}