亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

C#使用DirectoryEntry操作IIS創建網站和虛擬路徑

系統 2392 0

原文: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;
} ?

C#使用DirectoryEntry操作IIS創建網站和虛擬路徑


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 午夜在线观看免费影院 | 久久亚洲精品视频 | 99久久精品国产免看国产一区 | 精品国产hd | 亚洲精品乱码久久久久久麻豆 | 欧美一级亚洲一级 | 国产一级久久免费特黄 | 亚洲综合激情另类图片专区 | 阿v视频在线观看免费播放 阿v天堂2017 | 婷婷综合色伊人阁 | 国产一级特黄aa级特黄裸毛片 | 精品国产麻豆 | 国内精品久久影院 | 欧美高清不卡午夜精品免费视频 | 国产成人精品区在线观看 | 老子影院午夜伦不卡 | 成人精品区 | 男人天堂网在线观看 | 九九手机视频 | 国产91一区二这在线播放 | 久久国产精品夜色 | 中文字幕永久视频 | 久久性生活片 | 99久久精品费精品国产一区二区 | 国产综合亚洲专区在线 | 在线毛片免费观看 | 久久午夜网 | 被公侵犯肉体中文字幕一区二区 | 久久综合97色综合网 | 免费看欧美毛片大片免费看 | 日韩一区二区精品久久高清 | 一区二区亚洲精品 | 亚洲好视频 | 久久综合精品不卡一区二区 | 就去色综合 | 91精品国产高清久久久久久io | 亚洲成人视屏 | 午夜欧美精品久久久久久久久 | 色姑娘综合| 国产91精品久久久久久久 | 亚洲欧美日韩高清中文在线 |