有人可以提供一个代码示例或资源,可以帮助我以编程方式获取状态、启用和禁用使用 C# 在 IIS 7/IIS 7.5 中对身份验证的扩展保护吗?
带有 WMI/ADSI 的 C# 是首选。
即我被要求使用 C# 使用System.Management API 或Microsoft.Web.Administration API,我需要确定是否在 Web 服务器级别启用了 EAP(作为所有未来网站的 Web 服务器默认设置)。
也欢迎使用 C# 的任何其他解决方案。
期待有帮助的答案。谢谢
史蒂夫
微软慷慨地提供了一个网页,它不仅解释了这个新概念(即,身份验证的扩展保护,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();
}
}
}