When you use the DirectoryEntry class to operate IIS, an error in denial

xiaoxiao2021-03-06  91

Permission issues, additional code Using system; using system.data; using system.collections; namespace aspcn.management {////

/// iismanager's summary description. /// public class IISManager {private string _server // need to use defined, _website; private VirtualDirectories _virdirs; protected System.DirectoryServices.DirectoryEntry rootfolder; private bool _batchflag; public IISManager () {// Use default Localhost, that is, accessed the local machine_Server = "localhost"; _ Website = "1"; _ Batchflag = false;} public iismanager (String strserver) {_ server = strserver; _Website = "1"; _batchflag = false;} /// < Summary> // / Define Public Properties ///

// Server property defines the name of the access machine, which can be IP and calculation name public string server {get {returver;} set {_server = value;}} // Website property definition, for a number, for convenience, use String / / Generally, the first host is 1, the second host is 2, and push public string Website {get {return_website;} set {_Website = value;}}

// Virtual directory name public virtualdirectories virdirs {get {_virdirs = value;}} ///

/// definition public method ///

// Connect the server public void connection () {ConnectTOSERVER ();} // For easy to overload public void connect (String strserver) {_ server = strser; connectionToServer ();} // For easy heavy duty PUBLIC VOID Connect (String string STRSERVER , string strWebSite) {_ server = strServer; _website = strWebSite; ConnectToServer ();} // determines whether the virtual storage directory public bool Exists (string strVirdir) {return _virdirs.Contains (strVirdir);} // add a virtual directory public Void create (VirtualDirectory newdir) {string strpath = "IIS: //" _Server "/ w3svc /" _Website "/ root /" newdir.name; if (! _ virdirs.contains (newdir.name) || _batchflag) {try {// added to the ROOT Children set to DirectoryEntry newVirDir = rootfolder.Children.Add (newdir.Name, "IIsWebVirtualDir"); newVirDir.Invoke ( "AppCreate", true); newVirDir.CommitChanges (); rootfolder.CommitChanges (); // then the update data UpdateDirInfo (newVirDir, newdir);} catch (Exception ee) {throw new Exception (ee.ToString ());}} else {throw new Exception ( "This virtual directory is already EXIST. ");}} // Get a virtual directory public virtualdirectory getVirdir (String Strvir) {VirtualDirectory TMP = NULL; if (_VirdiRS. Contains (strVirdir)) {tmp = _virdirs.Find (strVirdir); ((VirtualDirectory). _Virdirs [strVirdir]) flag = 2;} else {throw new Exception ( "This virtual directory is not exists");} return tmp; }

/ / Update a virtual directory public void update (VirtualDirectory Dir) {// Judging whether the virtual directory that needs to be changed does IF (_VIRDIRDIRS.CONTAINS (Dir.Name)) {DirectoryEntry Ode = rootfolder.children.find (Dir.name, " IIsWebVirtualDir "); UpdateDirInfo (ode, dir);} else {throw new Exception (" This virtual directory is not exists ");.}} // delete a virtual directory connect public void delete (string strVirdir) {if (_virdirs .Contains (STRVIRDIR)) {Object [] Paras = new object [2]; Paras [0] = "IisWebVirtualDir"; // Indicates the virtual directory Paras [1] = Strvird; Rootfolder.Invoke ("delete", PARAS); rootfolder.commitchanges ();} else {throw new exception ("Can't delete" strvirdir ", Because it isn't exists");}} // Batch update public void updatebatch () {BatchUpdate (_VIRDIRS);} // Overload one :-) public void updatebatch (VirtualDirectories vds) {BatchUpdate (VDS);} ///

/// Private method ///

// Connection Server Private Void ConnectTOSERVER () {string strpath = "IIS: //" _Server "/ w3svc /" _Website "/ root"; try {this.rootfolder = new DirectoryEntry (strpath); _ virdirs = getVirdirs (} catch (exception e) {throw new exception ("Can't Connect to the Server [" _Server "] ...", E);}} // Execute Batch Update Private void BatchUpdate (VirtualDirectories vds) {_ batchflag = true; foreach (object item in vds.Values) {VirtualDirectory vd = (VirtualDirectory) item; switch (vd.flag) {case 0: break; case 1: Create (vd); break ; case 2: update (vd); break;}} _ batchflag = false;} // update stuff private void UpdateDirInfo (DirectoryEntry de, VirtualDirectory vd) {de.Properties [ "AnonymousUserName"] [0] = vd.AnonymousUserName; De.properties ["anonymoususeuserpass"] [0] = vd.anonymoususeuserpass; de.properties ["accessread"] [0] = vd.accessread; de.properties ["accessexecute"] [0] = vd.accessExecute; de. Properties ["AccessWrite"] [0] = vd.accessWrite; de.properties ["authbasic"] [0] = vd.authbasic; de.properties ["AuthnTlm"] [0] = vd.authntlm; de.properties [ " ContentIndexed "] [0] = vd.ContentIndexed; de.Properties [" EnableDefaultDoc "] [0] = vd.EnableDefaultDoc; de.Properties [" EnableDirBrowsing "] [0] = vd.EnableDirBrowsing; de.Properties [" AccessSSL " ] [0] = vd.accessssl; de.properties ["accessscript"] [0] = vd.accesssScript; de.properties ["defaultdoc"] [0] = vd.defaultdoc; de.properties ["path"] [ 0] = vd.path; de.commitchanges ();

// Gets the virtual directory collection of private VirtualDirectories GetVirDirs (DirectoryEntries des) {VirtualDirectories tmpdirs = new VirtualDirectories (); foreach (DirectoryEntry de in des) {if (de.SchemaClassName == "IIsWebVirtualDir") {VirtualDirectory vd = new VirtualDirectory (); vd.name = de.name; vd.accessRead = (bool) de.properties ["accessread"] [0]; vd.accessexecute = (bool) de.properties ["accessexecute"] [0]; vd.accessWrite = (BOOL) de.properties ["accesswrite"] [0]; vd.anonymoususername = (string) de.properties ["anonymoususername"] [0]; vd.anonymoususerpass = (string) de.properties ["anonymoususeusername"] [ 0]; vd.authbasic = (bool) de.properties ["authbasic"] [0]; vd.authnTLM = (bool) de.properties ["authnTLM"] [0]; vd.contentIndexed = (bool) de. Properties ["ContentIndexed"] [0]; vd.enableDefaultdoc = (bool) de.properties ["enabledefaultdoc"] [0]; vd.enableDirBrowsing = (bool) de.properties ["enabledirBrowsing"] [0]; vd. Accessssl = (bool) de.properties ["accessssl"] [0]; vd.accessscript = (bool) de.properties ["accessscript"] [0]; vd.path = (string) de.propertent IES ["PATH"] [0]; vd.flag = 0; vd.defaultdoc = (string) de.properties ["defaultdoc"] [0]; tmpdirs.add (vd.name, vd);}} Return TMPDIRS }

} ///

/// VirtualDirectory type /// public class VirtualDirectory {private bool _read, _execute, _script, _ssl, _write, _authbasic, _authntlm, _indexed, _endirbrow, _endefaultdoc; private string _ausername, _auserpass , _name, _path; private int _flag; private string _defaultdoc; /// /// constructor /// public VirtualDirectory () {SetValue ();} public VirtualDirectory (string strVirDirName) {_ name = strVirDirName; SetValue ();} private void SetValue () {_ read = true; _execute = false; _script = false; _ssl = false; _write = false; _authbasic = false; _authntlm = false; _indexed = false; _endirbrow = false; _endefaultdoc = false; _flag = 1; _defaultdoc = "default.htm, default.aspx, default.asp, index.htm"; _ path = "c: //"; _AUSERNAME = "; _AUSERPASS =" "; _ name =" " } /// /// Define attributes, iisvirtualdir too many attributes /// I only have more important, other big guys need to be added. ///

public int flag {get {return _flag;} set {_flag = value;}} public bool AccessRead {get {return _read;} ​​set {_read = value;}} public bool AccessWrite {get {return _write;} set {_write = value;}} public bool AccessExecute {get {return _execute;} set {_execute = value;}} public bool AccessSSL {get {return _ssl;} set {_ssl = value;}} public bool AccessScript {get {return _script;} set {_script = value;}} public bool AuthBasic {get {return _authbasic;} set {_authbasic = value;}} public bool AuthNTLM {get {return _authntlm;} set {_authntlm = value;}} public bool ContentIndexed {get { return _indexed;} set {_indexed = value;}} public bool EnableDirBrowsing {get {return _endirbrow;} set {_endirbrow = value;}} public bool EnableDefaultDoc {get {return _endefaultdoc;} set {_endefaultdoc = value;}} public string name {get {return _name;} set {_name = value;}} public string Path {get {return _path;} set {_path = value;}} public string DefaultDoc {get {return _defaultdoc;} set {_defaultdoc = value; }} public string anonymoususeusername {get {RET urn _ausername;} set {_ausername = value;}} public string AnonymousUserPass {get {return _auserpass;} set {_auserpass = value;}}} ///

/// set VirtualDirectories /// public class VirtualDirectories: System.Collections.Hashtable {public VirtualDirectories () {} // Add a new method of public VirtualDirectory Find (string strName) {return (VirtualDirectory) this [strName];}}}

Http://community.9cbs.net/expert/topic/3405/3405980.xml?temp=.7124292

转载请注明原文地址:https://www.9cbs.com/read-105598.html

New Post(0)