第一次的代码如下,问题是,本地调试可以,但是通过web访问却总是 提示,无法为本地创建连接凭证 或者是 访问被拒绝。
public
addsite(
string
virdirpath,
string
virdirname,
string
srvip,
string
webname,
string
port,
string
uname,
string
domain,
string
pwd)

...
{
// path to root of virtual directory, is not created by creating the site!!
string rootVirtualDir = virdirpath;// "d:/NewSite";
string site = virdirname;// "MySpecialSite";
ConnectionOptions oConn = new ConnectionOptions();
oConn.EnablePrivileges = true;
oConn.Authentication = AuthenticationLevel.Default;
oConn.Impersonation = ImpersonationLevel.Impersonate;
//oConn.Authority = "NTLMDOMAIN:HCTECH-DQ1TF02Y";
//oConn.Username = "wmi";
//oConn.Password = "wmi";

System.Web.HttpContext.Current.Response.Write("Authentication=" + oConn.Authentication.ToString() + "<hr>");
System.Web.HttpContext.Current.Response.Write("Username=" + oConn.Username + "<hr>");
System.Web.HttpContext.Current.Response.Write("EnablePrivileges=" + oConn.EnablePrivileges.ToString() + "<hr>");
System.Web.HttpContext.Current.Response.Write("Authority=" + oConn.Authority + "<hr>");
System.Web.HttpContext.Current.Response.Write("Impersonation=" + oConn.Impersonation.ToString() + "<hr>");
System.Web.HttpContext.Current.Response.Write("Locale=" + oConn.Locale + "<hr>");

ManagementPath myPath = new ManagementPath();
myPath.Server = srvip;// "192.168.0.10";
myPath.NamespacePath = @"rootMicrosoftIISv2";
myPath.RelativePath = "IIsWebService.Name='W3SVC'";//"//./root/MicrosoftIISv2"
ManagementScope scope = new ManagementScope(myPath, oConn);
using (ManagementObject nac = new ManagementObject(scope, myPath, null))

...{
// create a ServerBinding WMI object
ManagementObject Bindings = CreateInstance(oConn, srvip);
Bindings["IP"] = srvip;// "192.168.0.10";
Bindings["Hostname"] = webname;// "hctech-dq1tfo2y";
Bindings["Port"] = port;// "8083";
Bindings.Put(); //to commit the new instance.
ManagementBaseObject[] ServerBindings = new ManagementBaseObject[1];
ServerBindings[0] = Bindings as ManagementBaseObject;
// create the site using the new bindings
string ret = CreateNewSite(nac, rootVirtualDir, ServerBindings, site);
//Response.Write(ret);
// continue to configure and eventually manage the web server (start,
// stop etc..)
}
}



//
Create bindings object in WMI repository
public
static
ManagementObject CreateInstance(ConnectionOptions
oConn,
string
srvip)

...
{
ManagementScope mScope = null;
ManagementPath mPath = new ManagementPath();
mPath.ClassName = "ServerBinding";
mPath.NamespacePath = "root/MicrosoftIISv2";
mScope = new ManagementScope(mPath, oConn);
mScope.Path.Server = srvip;// "192.168.0.10";
return new ManagementClass(mScope, mPath,
null).CreateInstance();
}
//
Create new site
public
static
string
CreateNewSite(ManagementObject nac,
string
PathOfRootVirtualDir, ManagementBaseObject[] ServerBindings,

string
ServerComment
/**/
/*, int ServerId*/
)

...
{
ManagementBaseObject inP = null;
inP = nac.GetMethodParameters("CreateNewSite");
inP["PathOfRootVirtualDir"] = PathOfRootVirtualDir;
inP["ServerBindings"] = ServerBindings;
inP["ServerComment"] = ServerComment;
// inP["ServerId"] = ServerId; // Let the system create an ID
//(calculated hash of ServerComment)
ManagementBaseObject outP = nac.InvokeMethod("CreateNewSite", inP, null);
return System.Convert.ToString(outP.Properties["ReturnValue"].Value);
// Returns something like: IIsWebServer='W3SVC/1273343373'
// this can be used to further configure/customize the webserver
}
其中加了些调试的代码。
后来找了好久,总算把最后的问题解决了。使用模拟账户。这样比在web.config里添加<identity />节点要安全些。参考文章地址,http://support.microsoft.com/?kbid=306158#4
如果使用上文中提到的第三种方法,则会出现(匿名标识不能执行模拟)的错误。搞了半天才知道这个原因。因为第三种方法中的User.Identity是匿名用户的登陆。
我的平台是IIS6.0,WIN2K3,vs2005
最后的代码难得贴了,占页面得很。附上工程文件:http://download1.youkuaiyun.com/down3/20070601/01141423195.rar
reg.aspx是调用文件,addsite.cs是类文件。全部功能的实现。default.aspx是不相关的文件。
我的配置:IIS6.0,WIN2K3,.NET FRAMEWORK2.0
服务器上有个管理员叫 wmi 密码也是 wmi
我还在完整这个 工程,要做一个虚拟主机管理系统。创建网站这块已经解决了。ftp使用servu的ODBC功能.
不知道还有哪些安全方面的设置要设置。
网上的人解决了都没看到具体的解决方案,这里给出来。希望你对这个有更好的改进的时候,发一份工程文件到我的邮箱debug1984@126.com