利用活动目录配置IIS(一)
数字报纸程序终于告一段落了。这是个基于Asp.Net的Web程序,我在网上发现很多朋友在问如何用代码来配置一个网站。正好我的这个程序中所有的配置都是用代码来配置的。在这就把代码分享出来,并做简介要的说明。主要包括以下几个内容:
新建网站。(设置网站的主机头,设置网站的各种属性)
新建虚拟目录。
设置IIS虚拟目录的NET运行时版本。
给指定目录添加Asp.netNetWorkServie帐户。
代码修改Mini类型。
1. 活动目录编程简介.
活动目录为分布式编程提供了一个统一的编程接口. 在Net下活动目录的主要API在System.DirectoryServices;命名空间下.
2.用活动目录创建站点.
首先要实例化一个指个IIS服务器的DirectoryEntry对象.
string IIServerPath = "IIS://localhost/W3SVC";//IIS服务器的路径
string TargetIIServerPath = IIServerPath + "/" + W3SVCId; //要创建的网站ID
if (DirectoryEntry.Exists(TargetIIServerPath)) //如果已经存在
{
TargetIIServer = new DirectoryEntry(TargetIIServerPath);
#region站点访问方式设置
//设置网站的身份验证方式,在这为 启用匿名访问,集成Windows身份
TargetIIServer.Properties["AuthFlags"].Value = 0x00000004 | 0x00000001;
TargetIIServer.Properties["ServerAutoStart"].Value = "TRUE";
TargetIIServer.CommitChanges();
DirectoryEntry website = new DirectoryEntry(TargetIIServerPath + "/Root");
website.Properties["AuthFlags"].Value = 0x00000004 | 0x00000001;
website.Properties["AccessFlags"].Value = 0x00000001 | 0x00000002 | 0x00000200;
website.CommitChanges();
#endregion
}
else
{
System.Xml.XmlNodeList headlist = doc.SelectNodes("/WebSite/HostLHeads/HostHead");
object[] headlistName = new object;
headlistName = "127.0.0.1:80:127.0.0.1";
for (int i = 1; i < headlistName.Length; i++)
{
headlistName = (object)headlist.Attributes["name"].Value;
}
IISServer = new DirectoryEntry(IIServerPath);
//为新建Web站点准备参数 :站点名称,主机头,主目录路径
object[] newsite = new object[] { ServerComment, headlistName, MainCatalog };
//回调Com对象,新建Web站点
object websiteId = (object)IISServer.Invoke("CreateNewSite", newsite);
(doc.SelectSingleNode("/WebSite") as XmlElement).SetAttribute("WebSiteID", websiteId.ToString());
3.新建目录
private static DirectoryEntry ConfigPaper(string VirName, string Path)
{
DirectoryEntry rootsite = ConfigWebSite();
DirectoryEntry NewVir = null;
if (!DirectoryEntry.Exists(rootsite.Path + "/Root/" + VirName ))
{
DirectoryEntry site = new DirectoryEntry(rootsite.Path + "/" + "Root");
NewVir = site.Children.Add(VirName, "IIsWebVirtualDir");
object[] objs = new object[] { true };
//回调Com对象,新建虚拟目录.(要IIS6.0以上版本)
NewVir.Invoke("AppCreate", objs);
NewVir.Properties["AppFriendlyName"] = VirName;
if (!System.IO.Directory.Exists(Path))
{
System.IO.Directory.CreateDirectory(Path);
}
NewVir.Properties["Path"].Value = Path;
NewVir.CommitChanges();
}
else
{
NewVir = new DirectoryEntry(rootsite.Path + "/Root/" + VirName);
}
return NewVir;
}
4 设置IIS虚拟目录的NET运行时版本。
设置运行时版本我在这用的是利用aspnet_regiis.exe工具
System.DirectoryServices.DirectoryEntry AD = TargetIIServer;
string aspnetRegIIS20 = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), @"Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo();
p.StartInfo.FileName = aspnetRegIIS20;
string arg = AD.Path.ToUpper();
arg = arg.Substring(arg.IndexOf("W3SVC"));
p.StartInfo.Arguments = "-s" + arg;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.Start();
页:
[1]