微软慷慨地提供了一个网页,它不仅解释了这个新概念(即,身份验证的扩展保护,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();
}
}
}