原文:http://www.cnblogs.com/Aiooioo/archive/2011/05/30/cs-iis.html
在.Net中我們可以使用內(nèi)置的類DirectoryEntry來承載IIS服務(wù)器中的任何網(wǎng)站,虛擬路徑或應(yīng)用程序池對(duì)象,例如:
?
DirectoryEntry ent = new?
DirectoryEntry("IIS://localhost/w3svc/1/root");
就創(chuàng)建了一個(gè)IIS路徑為IIS://localhost/w3svc/1/root的虛擬路徑對(duì)象。
?
為了在IIS中創(chuàng)建一個(gè)網(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對(duì)象,也就是網(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)建一個(gè)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)證
//勾選匿名訪問和windows身份驗(yàn)證
/*
* 標(biāo)志
標(biāo)志名AuthBasic
描述指定基本身份驗(yàn)證作為可能的?
Windows 驗(yàn)證方案之一,返回給客戶端作為有效驗(yàn)證方案。
Windows 驗(yàn)證方案之一,返回給客戶端作為有效驗(yàn)證方案。
配置數(shù)據(jù)庫(kù)位掩碼標(biāo)識(shí)符MD_AUTH_BASIC
十進(jìn)制值2
十六進(jìn)制值0x00000002
標(biāo)志名AuthAnonymous
描述指定匿名身份驗(yàn)證作為可能的?
Windows 驗(yàn)證方案之一,返回給客戶端作為有效驗(yàn)證方案。
Windows 驗(yàn)證方案之一,返回給客戶端作為有效驗(yàn)證方案。
配置數(shù)據(jù)庫(kù)位掩碼標(biāo)識(shí)符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)證方案。
身份驗(yàn)證(也稱作質(zhì)詢/響應(yīng)或 NTLM 驗(yàn)證)作為可能的 Windows 驗(yàn)證方案之一,返回給客戶端作為有效驗(yàn)證方案。
配置數(shù)據(jù)庫(kù)位掩碼標(biāo)識(shí)符MD_AUTH_NT
十進(jìn)制值4
十六進(jìn)制值0x00000001
?
標(biāo)志名AuthMD5
描述指定摘要式身份驗(yàn)證和高級(jí)摘要式身份驗(yàn)證作為可能的 Windows?
驗(yàn)證方案之一,返回給客戶端作為有效驗(yàn)證方案。
驗(yàn)證方案之一,返回給客戶端作為有效驗(yàn)證方案。
配置數(shù)據(jù)庫(kù)位掩碼標(biāo)識(shí)符MD_AUTH_MD5
十進(jìn)制值16
十六進(jìn)制值0x00000010
標(biāo)志名AuthPassport
描述true 的值表示啟用了?
Microsoft .NET Passport 身份驗(yàn)證。 詳細(xì)信息,請(qǐng)參閱 .NET Passport 驗(yàn)證。
配置數(shù)據(jù)庫(kù)位掩碼標(biāo)識(shí)符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會(huì)自動(dòng)控制匿名訪問賬戶的密碼。之前我嘗試將匿名訪問用戶的密碼傳給網(wǎng)站,之后發(fā)現(xiàn)創(chuàng)建出來的網(wǎng)站盡管勾選的匿名訪問并且設(shè)置了匿名用戶密碼,瀏覽的時(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í)不需要再次設(shè)置匿名訪問,因?yàn)榫W(wǎng)站下的虛擬路徑會(huì)默認(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;
}
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
