有人可以提供代码示例或资源来帮助我以编程方式获取状态、启用和禁用使用 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/