c# - 如何使用 WMI/ADSI 和 C# 在 IIS 中为身份验证的扩展保护启用/禁用/获取状态?

标签 c# iis-7 iis-7.5 wmi adsi

有人可以提供代码示例或资源来帮助我以编程方式获取状态、启用和禁用使用 C# 在 IIS 7/IIS 7.5 中对身份验证的扩展保护吗?

带有 WMI/ADSI 的 C# 是首选。

即我被要求使用 System.Management API 或 Microsoft.Web.Administration API 使用 C#,我需要确定是否启用了 EAP在网络服务器级别(作为所有 future 网站的默认网络服务器)。

也欢迎使用 C# 的任何其他解决方案。

期待有用的答案。谢谢

史蒂夫

最佳答案

微软 graciously provided a web page它不仅解释了这个新概念(即身份验证的扩展保护,flag=extendedProtection),而且还提供了多种语言的示例代码(复制在下面)。这是他们在 IIS7/7.5 中启用 EAP 的 C# 代码。

通过 WMI 实现此功能需要使用显式凭据并设置 impersonationLevel=Impersonate。 Frank White 最近在 SO 上创建了一种替代方法,我在这里详细介绍了它的完整代码:https://stackoverflow.com/a/11948096/1569434

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

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();

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

         ConfigurationElement extendedProtectionElement = windowsAuthenticationSection.GetChildElement("extendedProtection");
         extendedProtectionElement["tokenChecking"] = @"Allow";
         extendedProtectionElement["flags"] = @"None";

         ConfigurationElementCollection extendedProtectionCollection = extendedProtectionElement.GetCollection();

         ConfigurationElement spnElement = extendedProtectionCollection.CreateElement("spn");
         spnElement["name"] = @"HTTP/www.contoso.com";
         extendedProtectionCollection.Add(spnElement);

         ConfigurationElement spnElement1 = extendedProtectionCollection.CreateElement("spn");
         spnElement1["name"] = @"HTTP/contoso.com";
         extendedProtectionCollection.Add(spnElement1);

         serverManager.CommitChanges();
      }
   }
}

关于c# - 如何使用 WMI/ADSI 和 C# 在 IIS 中为身份验证的扩展保护启用/禁用/获取状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13144657/

相关文章:

c# - 集成测试(纯文本文件填充数据库)

c# - 如何在 ASP.NET 缓存应用程序中超过 IIS7 的 60% 内存限制

iis-7 - CC.Net 访问被拒绝错误

asp.net - 如何让我的 ASP.NET 应用程序始终保持 "alive",如果这是一个坏主意,为什么我不应该这样做呢?

asp.net-mvc - 如何使自定义错误页面出现在生产服务器上

coldfusion - CF10 开发人员在 IIS7.5/WIN7 上 - CFM 文件运行正常 - 但看不到 .GIF

c# - 自动完成文本框,如 Stack Overflow 上的 "Tags"文本框

c# - 如何在新流程中启动第二个项目的新实例

c# - 平滑 Lerp 运动?

asp.net-mvc - 是什么原因导致503服务无法用于asp.net mvc3应用程序的报告区域?