日期:2010-03-08 浏览次数:20483 次
这玩意儿花了老子3天时间才搞定,用了几个小时写代码,而且当中还花了不少时间解决Win32: Access denied error的问题。当然我要指出的是,无论NETWORK SERVER帐户还是IUSR_<servername>帐户都不要设置过大的权限。对于WMI和IIS metabase的安全机理,我还是一无所知的。我只不过解决问题而已。
首先要从Internet信息服务(IIS)管理器中获取网站标识符,点击“网站”根节点,右侧“标识符”显示的就是网站的ID。默认网站的标识符通常是1。
获取网站标识的功能,我们要用到一个namespace,代码如下:
using System.Management;
下文所有’ServerName’都表示你的服务器名称,或者如果你的代码是本地运行的,也可以用一个点来表示。
创建一个站点,你会用到如下函数。这个函数返回新网站的ID,这样你可以进一步对这个网站进行操作。
public static string CreateWebsite(string serverName, string appPoolName, string ip,string pathToRoot, string hostName, string domainName, int port) { ConnectionOptions options = new ConnectionOptions(); options.Authentication = AuthenticationLevel.Connect; options.EnablePrivileges = true; options.Impersonation = ImpersonationLevel.Impersonate; ManagementScope scope = new ManagementScope(string.Format(@\\{0}\root\MicrosoftIISv2, serverName), options); scope.Connect(); ManagementObject oW3SVC = new ManagementObject(scope, new ManagementPath(@"IIsWebService='W3SVC'"), null); ManagementBaseObject[] serverBindings = new ManagementBaseObject[1]; serverBindings[0] = CreateServerBinding(scope, string.Format("{0}.{1}", hostName, domainName), ip, port); ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters("CreateNewSite"); inputParameters["ServerComment"] = string.Format("{0}.{1}", hostName, domainName); inputParameters["ServerBindings"] = serverBindings; inputParameters["PathOfRootVirtualDir"] = pathToRoot; ManagementBaseObject outParameter = oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null); string siteId = Convert.ToString( outParameter.Properties["ReturnValue"].Value).Replace( "IIsWebServer='W3SVC/", "").Replace("'", ""); ManagementObject oWebVirtDir = new ManagementObject(scope, new ManagementPath(string.Format( @"IIsWebVirtualDirSetting.Name='W3SVC/{0}/root'", siteId)), null); oWebVirtDir.Properties["AppFriendlyName"].Value = string.Format("{0}.{1}", hostName, domainName); oWebVirtDir.Properties["AccessRead"].Value = true; oWebVirtDir.Properties["AuthFlags"].Value = 5; // Integrated Windows Auth. oWebVirtDir.Properties["AccessScript"].Value = true; oWebVirtDir.Properties["AuthAnonymous"].Value = true; oWebVirtDir.Properties["AppPoolId"].Value = appPoolName; oWebVirtDir.Put(); ManagementObject site = new ManagementObject(scope, new ManagementPath(Convert.ToString( outParameter.Properties["ReturnValue"].Value)), null); site.InvokeMethod("Start", null); return siteId; }
创建一个虚拟目录:
public static void AddVirtualFolder(string serverName, string websiteId,string name, string path) { ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", serverName)); scope.Connect(); string siteName = string.Format("W3SVC/{0}/Root/{1}", websiteId, name); ManagementClass mc = new ManagementClass(scope,new ManagementPath("IIsWebVirtualDirSetting"), null); ManagementObject oWebVirtDir = mc.CreateInstance(); oWebVirtDir.Properties["Name"].Value = siteName; oWebVirtDir.Properties["Path"].Value = path; oWebVirtDir.Properties["AuthFlags"].Value = 5; // Integrated Windows Auth. oWebVirtDir.Properties["EnableDefaultDoc"].Value = true; // date, time, size, extension, longdate ; oW