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

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

系統 2245 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元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 利川市| 项城市| 峨边| 南安市| 县级市| 阜宁县| 诸城市| 繁昌县| 准格尔旗| 通州市| 安新县| 军事| 广水市| 兴山县| 香河县| 拉萨市| 九江市| 行唐县| 蓬安县| 巴彦淖尔市| 苍梧县| 呼伦贝尔市| 施甸县| 永和县| 辉县市| 久治县| 抚顺县| 任丘市| 金溪县| 娄底市| 汕头市| 舒城县| 阿荣旗| 金华市| 兴国县| 嘉义县| 台江县| 深水埗区| 甘德县| 竹山县| 延庆县|