指定匿名访问用户,IIS6比较简单,在IIsWebVirtualDirSetting有两个属性可以设置到:AnonymousUserName、AnonymousUserPass,代码如下
ConnectionOptions co = new ConnectionOptions();
co.Authentication = AuthenticationLevel.PacketPrivacy;
ManagementPath iisPath = new ManagementPath();
iisPath.NamespacePath = @"root/MicrosoftIISv2";
ManagementScope scope = new ManagementScope(iisPath, co);
scope.Connect();
string website="esintest6" ;//站点名
int siteId=1;//站点Id,IIS6很多操作都需要得到该ID才能继续
//获取website
ObjectQuery query = new ObjectQuery(
string.Format(@"SELECT Name FROM IIsWebServerSetting where ServerComment='{0}' ", website));
ManagementObjectSearcher searcher =new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject mo in queryCollection)
{
siteId = mo.Properties["Name"].Value.ToString().Replace("W3SVC/", "");
break;
}//开始设置
ManagementObject siteObj=new ManagementObject(Scope,new ManagementPath(string.Format(
@"{0}.Name='{1}'", "IIsWebServerSetting ", string.Format("W3SVC/{0}/root", siteId))), null);
siteObj.Properties["AnonymousUserName"].Value = "iusr_esintest6";//这里是系统用户
siteObj.Properties["AnonymousUserPass"].Value = "1qaz@wsx#edc";//对应的密码
siteObj.Put();
ConnectionOptions co = new ConnectionOptions();
co.Authentication = AuthenticationLevel.PacketPrivacy;
ManagementPath iisPath = new ManagementPath();
iisPath.NamespacePath = @"root/webadministration";
ManagementScope scope = new ManagementScope(iisPath, co);
scope.Connect();
string website="esintest6" ;//站点名
//开始设置
ManagementObject website = new ManagementObject(scope, new ManagementPath(string.Format("Site.name='{0}'",website)), null);
ManagementBaseObject inParams = website.GetMethodParameters("GetSection");
inParams["SectionName"] = "AnonymousAuthenticationSection";
ManagementBaseObject oo = website.InvokeMethod("GetSection", inParams, null) as ManagementBaseObject;
ManagementBaseObject returnedSection = oo["Section"] as ManagementBaseObject;
ManagementObject retval = new ManagementObject(scope, new ManagementPath("AnonymousAuthenticationSection.Path='" +
returnedSection["Path"].ToString().Replace("/"+website, "") + "',Location='"+website+"'"), null);
retval.Get();
retval.Properties["Enabled"].Value = true;
retval.Properties["Location"].Value = website;
retval.Properties["UserName"].Value = "iusr_esintest6";//这里是系统用户
retval.Properties["Password"].Value = "1qaz@wsx#edc";//对应的密码
retval.Put();
搞掂^_^