原文:http://www.cnblogs.com/Aiooioo/archive/2011/05/30/cs-iis.html

在.Net中我们可以使用内置的类DirectoryEntry来承载IIS服务器中的任何网站,虚拟路径或应用程序池对象,例如:
 
DirectoryEntry ent = new DirectoryEntry("IIS://localhost/w3svc/1/root");
就创建了一个IIS路径为IIS://localhost/w3svc/1/root的虚拟路径对象。
 
为了在IIS中创建一个网站,我们首先需要确定输入的网站路径在IIS中是否存在,这里主要是根据网站在IIS中的ServerBindings属性来区分:
DirectoryEntry ent;
DirectoryEntry rootEntry;
try
{
  ent = EnsureNewWebSiteAvailable(host + ":" + port + ":" + webSiteDesc);
  if (ent != null)
  {
    //这里如果用户输入的网站在IIS中已经存在,那么直接获取网站的root对象,也就是网站的默认应用程序
    rootEntry = ent.Children.Find("root", "IIsWebVirtualDir");
  }
  else
  {
    //如果网站在IIS不存在,那么我们需要首先在IIS中创建该网站,并且为该网站创建一个root应用程序
    string entPath = string.Format("IIS://{0}/w3svc", Host);
    DirectoryEntry root = GetDirectoryEntry(entPath);
    string newSiteNum = GetNewWebSiteID();
    DirectoryEntry newSiteEntry = root.Children.Add(newSiteNum, "IIsWebServer");
    newSiteEntry.CommitChanges();
    newSiteEntry.Properties["ServerBindings"].Value = host + ":" + port + ":" + webSiteDesc;
    newSiteEntry.Properties["ServerComment"].Value = webSiteComment;
    newSiteEntry.CommitChanges();
    rootEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");
    rootEntry.CommitChanges();
    rootEntry.Properties["Path"].Value = webSitePath;
    rootEntry.Properties["AppPoolId"].Value = appPool;
    rootEntry.Properties["AccessRead"][0] = true; // 勾选读取
    rootEntry.Properties["AuthFlags"][0] = 1+4; 
    //勾选匿名访问和windows身份验证
    /** 标志
    标志名AuthBasic
    描述指定基本身份验证作为可能的 
    Windows 验证方案之一,返回给客户端作为有效验证方案。
    配置数据库位掩码标识符MD_AUTH_BASIC
    十进制值2
    十六进制值0x00000002
    
    标志名AuthAnonymous
    描述指定匿名身份验证作为可能的 
    Windows 验证方案之一,返回给客户端作为有效验证方案。
    配置数据库位掩码标识符MD_AUTH_ANONYMOUS
    十进制值1
    十六进制值0x00000001
    
    标志名AuthNTLM
    描述指定集成 Windows 
    身份验证(也称作质询/响应或 NTLM 验证)作为可能的 Windows 验证方案之一,返回给客户端作为有效验证方案。
    配置数据库位掩码标识符MD_AUTH_NT
    十进制值4
    十六进制值0x00000001
 
    标志名AuthMD5
    描述指定摘要式身份验证和高级摘要式身份验证作为可能的 Windows 
    验证方案之一,返回给客户端作为有效验证方案。
    配置数据库位掩码标识符MD_AUTH_MD5
    十进制值16
    十六进制值0x00000010
    
    标志名AuthPassport
    描述true 的值表示启用了 Microsoft .NET Passport 身份验证。 详细信息,请参阅 .NET Passport 验证。
    配置数据库位掩码标识符MD_AUTH_PASSPORT
    十进制值64
    十六进制值0x00000040
    */
    rootEntry.Properties["DontLog"][0] = true;
    rootEntry.Properties["AuthAnonymous"][0] = true;
    rootEntry.Properties["AnonymousUserName"][0] =
    XmlSettings.GetWebXmlSettingString("IISAnonymousUserName");
    
    /*这里AnonymousUserPass属性如果不去设置,IIS会自动控制匿名访问账户的密码。之前我尝试将匿名访问用户的密码传给网站,之后发现创建出来的网站尽管勾选的匿名访问并且设置了匿名用户密码,浏览的时候还是提示要输入密码,很是纠结*/
    rootEntry.Invoke("AppCreate", true);
    rootEntry.CommitChanges();
  }
  DirectoryEntry de = rootEntry.Children.Add(friendlyName, rootEntry.SchemaClassName);
  de.CommitChanges();
  de.Properties["Path"].Value = virtualPath;
  de.Properties["AccessRead"][0] = true; // 勾选读取
  de.Invoke("AppCreate", true);
  de.Properties["EnableDefaultDoc"][0] = true;
  de.Properties["AccessScript"][0] = true; // 脚本资源访问
  de.Properties["DontLog"][0] = true; // 勾选记录访问
  de.Properties["ContentIndexed"][0] = true; // 勾选索引资源
  de.Properties["AppFriendlyName"][0] = friendlyName; //应用程序名
  de.Properties["AuthFlags"][0] = 5;
  /*这里在创建虚拟路径时不需要再次设置匿名访问,因为网站下的虚拟路径会默认接受网站的访问限制设置*/
  de.CommitChanges();
}
catch (Exception e)
{
  throw e;
}
 
public string GetNewWebSiteID()
{
  ArrayList list = new ArrayList();
  string tempStr;
  string entPath = string.Format("IIS://{0}/w3svc",Host);
  DirectoryEntry ent = GetDirectoryEntry(entPath);
  foreach (DirectoryEntry child in ent.Children)
  {
    if (child.SchemaClassName == "IIsWebServer")
    {
      tempStr = child.Name.ToString();
      list.Add(Convert.ToInt32(tempStr));
    }
  }
  list.Sort();
  var newId = Convert.ToInt32(list[list.Count - 1]) + 1;
  return newId.ToString();
}
 
public DirectoryEntry GetDirectoryEntry(string entPath)
{
  DirectoryEntry ent;
  if (string.IsNullOrEmpty(UserName))
  {
    ent = new DirectoryEntry(entPath);
  }
  else
  {
    ent = new DirectoryEntry(entPath, Host + "\\" + UserName, Password, AuthenticationTypes.Secure);
  }
  return ent;
}
 
public DirectoryEntry EnsureNewWebSiteAvailable(string bindStr)
{
  string entPath = string.Format("IIS://{0}/w3svc",Host);
  DirectoryEntry ent = GetDirectoryEntry(entPath);
  foreach (DirectoryEntry child in ent.Children)
  {
    if (child.SchemaClassName == "IIsWebServer")
    {
      if (child.Properties["ServerBindings"].Value != null)
      {
        if (child.Properties["ServerBindings"].Value.ToString() == bindStr)
        { return child; }
      }
    }
  }
  return null;

最新文章

  1. 安卓Design包下的TextInputLayout和FloatingActionButton的简单使用
  2. html5 前端图片处理(预览、压缩、缩放)
  3. wordpress电子商务插件和主题的使用方法
  4. Linux中vim的简单配置
  5. Spring学习之AOP总结帖
  6. Bag of mice(CodeForces 148D )
  7. ThinkPHP连接sql server数据库
  8. 把当前时间(NSDate)转为字符串 - 获取当前时间的Day
  9. AE CreateFeatureClass 创建shp. 删除shp. 向shp中添加要素
  10. 给IT新男的15点建议:苦逼程序员的辛酸反省与总结
  11. 设置contentType
  12. .net通用权限框架B/S (五)--WEB(3)组织机构
  13. (一)Lua脚本语言入门
  14. Gym101473A Gym101473E Gym101473F-前缀和
  15. jquery实现对div的拖拽功能
  16. 简单的dfs题 --- POJ1321 棋盘问题
  17. 利用itext将html页面转成pdf(不模糊)
  18. Nginx 参数配置相关
  19. IdentityServer4 中文文档 -8- (快速入门)设置和概览
  20. Shiro的认证授权

热门文章

  1. Arc engine - Geodatabase.
  2. SQL Server还原和一些小发现
  3. 优化PHP代码的40条建议(转载)
  4. 在UC浏览器上很炫的一个效果
  5. Database JDBC Developer's Guide
  6. USB 各型插座插头引脚分布
  7. jquery第六期:位置选择器
  8. mysql 增量导入到elasticsearch
  9. CDT+Eclipse代码自动提示
  10. mysql----用户root被删除或忘记root密码的解决方案