日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

C#使用DirectoryEntry操作IIS創(chuàng)建網(wǎng)站和虛擬路徑

系統(tǒng) 2409 0

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

在.Net中我們可以使用內(nèi)置的類DirectoryEntry來承載IIS服務(wù)器中的任何網(wǎng)站,虛擬路徑或應(yīng)用程序池對象,例如:
?
DirectoryEntry ent = new? DirectoryEntry("IIS://localhost/w3svc/1/root");
就創(chuàng)建了一個IIS路徑為IIS://localhost/w3svc/1/root的虛擬路徑對象。
?
為了在IIS中創(chuàng)建一個網(wǎng)站,我們首先需要確定輸入的網(wǎng)站路徑在IIS中是否存在,這里主要是根據(jù)網(wǎng)站在IIS中的ServerBindings屬性來區(qū)分:
DirectoryEntry ent;
DirectoryEntry rootEntry;
try
{
  ent = EnsureNewWebSiteAvailable(host + ":" + port + ":" +? webSiteDesc);
  if (ent != null)
  {
    //這里如果用戶輸入的網(wǎng)站在IIS中已經(jīng)存在,那么直接獲取網(wǎng)站的root對象,也就是網(wǎng)站的默認(rèn)應(yīng)用程序
    rootEntry = ent.Children.Find("root",? "IIsWebVirtualDir");
  }
  else
  {
    //如果網(wǎng)站在IIS不存在,那么我們需要首先在IIS中創(chuàng)建該網(wǎng)站,并且為該網(wǎng)站創(chuàng)建一個root應(yīng)用程序
    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身份驗(yàn)證
    /* * 標(biāo)志
    標(biāo)志名AuthBasic
    描述指定基本身份驗(yàn)證作為可能的?
    Windows 驗(yàn)證方案之一,返回給客戶端作為有效驗(yàn)證方案。
    配置數(shù)據(jù)庫位掩碼標(biāo)識符MD_AUTH_BASIC
    十進(jìn)制值2
    十六進(jìn)制值0x00000002
    標(biāo)志名AuthAnonymous
    描述指定匿名身份驗(yàn)證作為可能的?
    Windows 驗(yàn)證方案之一,返回給客戶端作為有效驗(yàn)證方案。
    配置數(shù)據(jù)庫位掩碼標(biāo)識符MD_AUTH_ANONYMOUS
    十進(jìn)制值1
    十六進(jìn)制值0x00000001
    標(biāo)志名AuthNTLM
    描述指定集成 Windows?
    身份驗(yàn)證(也稱作質(zhì)詢/響應(yīng)或 NTLM 驗(yàn)證)作為可能的 Windows 驗(yàn)證方案之一,返回給客戶端作為有效驗(yàn)證方案。
    配置數(shù)據(jù)庫位掩碼標(biāo)識符MD_AUTH_NT
    十進(jìn)制值4
    十六進(jìn)制值0x00000001
?
    標(biāo)志名AuthMD5
    描述指定摘要式身份驗(yàn)證和高級摘要式身份驗(yàn)證作為可能的 Windows?
    驗(yàn)證方案之一,返回給客戶端作為有效驗(yàn)證方案。
    配置數(shù)據(jù)庫位掩碼標(biāo)識符MD_AUTH_MD5
    十進(jìn)制值16
    十六進(jìn)制值0x00000010
    標(biāo)志名AuthPassport
    描述true 的值表示啟用了? Microsoft .NET Passport 身份驗(yàn)證。 詳細(xì)信息,請參閱 .NET Passport 驗(yàn)證。
    配置數(shù)據(jù)庫位掩碼標(biāo)識符MD_AUTH_PASSPORT
    十進(jìn)制值64
    十六進(jìn)制值0x00000040
    */
    rootEntry.Properties["DontLog"][0] = true;
    rootEntry.Properties["AuthAnonymous"][0] = true;
    rootEntry.Properties["AnonymousUserName"][0] =
    XmlSettings.GetWebXmlSettingString("IISAnonymousUserName");
    /*這里AnonymousUserPass屬性如果不去設(shè)置,IIS會自動控制匿名訪問賬戶的密碼。之前我嘗試將匿名訪問用戶的密碼傳給網(wǎng)站,之后發(fā)現(xiàn)創(chuàng)建出來的網(wǎng)站盡管勾選的匿名訪問并且設(shè)置了匿名用戶密碼,瀏覽的時候還是提示要輸入密碼,很是糾結(jié)*/
    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;? //應(yīng)用程序名
  de.Properties["AuthFlags"][0] = 5;
  /*這里在創(chuàng)建虛擬路徑時不需要再次設(shè)置匿名訪問,因?yàn)榫W(wǎng)站下的虛擬路徑會默認(rèn)接受網(wǎng)站的訪問限制設(shè)置*/
  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創(chuàng)建網(wǎng)站和虛擬路徑


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 襄垣县| 宝鸡市| 马鞍山市| 康平县| 雷波县| 女性| 普兰县| 苏尼特左旗| 始兴县| 东乌珠穆沁旗| 淳化县| 博野县| 平安县| 千阳县| 兴义市| 隆子县| 杭锦后旗| 邮箱| 调兵山市| 瑞昌市| 隆回县| 宁化县| 汤阴县| 延长县| 桦川县| 阿合奇县| 彭山县| 乡宁县| 盘锦市| 永善县| 永平县| 右玉县| 敦煌市| 泽州县| 融水| 日土县| 肇州县| 温宿县| 绥芬河市| 临猗县| 浦县|