Use ASP. NET to manage IIS

Source: Internet
Author: User
Using ASP. NET to operate IIS allows you to create virtual directories, virtual sites, delete virtual directories, and lists.
Key: ASP. NET users are not authorized to operate IIS, so they must be granted a high level of permissions.
Method 1: add this line to the Web. config file <identity impersonate = "true"/>, but it is risky.
Method 2: Counterfeit is used. I use this method, but it is still risky,
Other methods, omitted

Using system;
Using system. IO;
Using system. directoryservices;

Namespace com. todayisp. iismanager
{
/// <Summary>
/// Summary description for iismanager.
/// </Summary>
Public class iismanager
{
Private directoryentry _ iisserver;
// Public int counter = 10; // limit the number of virtual directories created

// Create a virtual directory
Public String createvdir (string websitename, string namedirectory, int counter)
{

String siteid = getsiteid (websitename );
If (siteid = NULL) Return "error: the site does not exist .";

_ Iisserver = new directoryentry ("IIS: // localhost/w3svc/" + siteid );
Try
{
Directoryentry web = _ iisserver. Children. Find ("root", "iiswebvirtualdir ");
Directoryentry Vd = web. Children. Find (namedirectory, "iiswebvirtualdir ");
Return "error: the virtual directory you want to create already exists .";
}
Catch
{
Try
{
Directoryentry folderroot = _ iisserver. Children. Find ("root", "iiswebvirtualdir ");

Int thecount = 0;
Foreach (directoryentry B1 in folderroot. Children)
{
Thecount = thecount + 1;
}

// If the number of virtual directories does not exceed the specified number, create.
If (thecount <counter)
{
String thepath;
Thepath = folderroot. properties ["path"]. Value + "\" + namedirectory;

// If this directory does not exist, create a real directory
Directoryinfo di = directory. createdirectory (thepath );

Directoryentry newvirdir = folderroot. Children. Add (namedirectory, "iiswebvirtualdir ");
Newvirdir. commitchanges ();
// Set Properties
Newvirdir. properties ["accessread"]. Add (true );
Newvirdir. properties ["path"]. value = thepath;
// Create a application; don't use invoke Method
// Save changes
Newvirdir. commitchanges ();
Folderroot. commitchanges ();
_ Iisserver. commitchanges ();
Newvirdir. Invoke ("appcreate", true );
Return "successful: You have successfully created a virtual directory:" + namedirectory;
}
Else
{
Return "error: the virtual directory of your site is already full" + counter + ", so you cannot create more .";
}
}
Catch (exception E)
{
Return "error: failed to enter the site." + E. message;
}
}

}

// Delete a virtual directory
Public String delvirtualdirectory (string websitename, string namedirectory)
{
Try
{
String siteid = getsiteid (websitename );
If (siteid = NULL) Return "error: the site does not exist .";


Directoryentry deroot = new directoryentry ("IIS: // localhost/w3svc/" + siteid + "/root ");
Try
{
Directoryentry devdir = deroot. Children. Find (namedirectory, "iiswebvirtualdir ");
Deroot. refreshcache ();
Devdir = deroot. Children. Find (namedirectory, "iiswebvirtualdir ");
Devdir. Invoke ("appdelete", null );
Deroot. Children. Remove (devdir );
Deroot. commitchanges ();
Deroot. Close ();
Return "successful: deleting the virtual directory" + namedirectory + "successful! ";
}
Catch
{
Return "error: the virtual directory does not exist .";
}
}
Catch (exception E)
{
Return "error: failed to delete directory." + E. message;
}
}

// Find the corresponding virtual site.
Public String getsiteid (string websitename)
{
Directoryentry root = new directoryentry ("IIS: // localhost/W3SVC ");
Try
{
String siteid = NULL;
String hostname;
Foreach (directoryentry BB in root. Children)
{
Try
{
// If (convert. toint32 (BB. Name. Trim () <0) continue;
Propertyvaluecollection PVC = BB. properties ["serverbindings"];

String [] srvbindings = (string) PVC [0]). Split (New char [] {':'});
Hostname = srvbindings [2]. Trim ();

// Judge, replace hostname
// If (websitename = BB. properties ["servercomment"]. value. tostring () siteid = BB. Name;
If (websitename = hostname) siteid = BB. Name;
// Clear variable
Hostname = "";
}
Catch {}
}
If (siteid = NULL) return NULL;
Return siteid;
}
Catch
{
Return NULL;
}
}

// Rename the virtual directory
Public bool renamevirtualdirectory (string websitename, string namedirectory, string newname)
{
Try
{
Directoryentry root = new directoryentry ("IIS: // localhost/W3SVC ");

String siteid = "";
String hostname;
Foreach (directoryentry BB in root. Children)
{
Try
{
// If (convert. toint32 (BB. Name. Trim () <0) continue;
Propertyvaluecollection PVC = BB. properties ["serverbindings"];
String [] srvbindings = (string) PVC [0]). Split (New char [] {':'});
Hostname = srvbindings [2]. Trim ();
If (hostname. Equals ("") hostname = "localhost ";

// Judge, replace hostname
// If (websitename = BB. properties ["servercomment"]. value. tostring () siteid = BB. Name;
If (websitename = hostname) siteid = BB. Name;
// Clear variable
Hostname = "";
}
Catch {}
}

Directoryentry deroot = new directoryentry ("IIS: // localhost/w3svc/" + siteid + "/root ");
Directoryentry devdir = deroot. Children. Find (namedirectory, "iiswebvirtualdir ");
Deroot. refreshcache ();
Devdir = deroot. Children. Find (namedirectory, "iiswebvirtualdir ");
Devdir. Rename ("yeah ");
Deroot. commitchanges ();
Deroot. Close ();
Return true;
}
Catch
{
Return false;
}
}

// Create a virtual site class. Enter "site name", "site root directory address", and "Port ".
Public bool createwebsite (string sitename, string path, string port)
{
Try
{
Directoryentry root = new directoryentry ("IIS: // localhost/W3SVC ");
// Find unused id value for new web site
Int siteid = 1;
Foreach (directoryentry EF in root. Children)
{
If (ef. schemaclassname = "iiswebserver ")
{
Int id = convert. toint32 (ef. Name );
If (ID> = siteid)
{
Siteid = ID + 1;
}
}
} // Create web site
Directoryentry site = (directoryentry) root. Invoke ("CREATE", "iiswebserver", siteid );
Site. Invoke ("put", "servercomment", sitename );
Site. Invoke ("put", "keytype", "iiswebserver ");
Site. Invoke ("put", "serverbindings", ":" + port + ":");
Site. Invoke ("put", "serverstate", 2 );
Site. Invoke ("put", "frontpageweb", 1 );
Site. Invoke ("put", "defaultdoc", "default. aspx ");
Site. Invoke ("put", "securebindings", ": 443 :");
Site. Invoke ("put", "serverautostart", 1 );
Site. Invoke ("put", "serversize", 1 );
Site. Invoke ("setinfo ");
// Create application virtual
Directoryentry sitevdir = site. Children. Add ("root", "iiswebvirtualdir ");
Sitevdir. properties ["appisolated"] [0] = 2;
Sitevdir. properties ["path"] [0] = path;
Sitevdir. properties ["accessflags"] [0] = 513;
Sitevdir. properties ["frontpageweb"] [0] = 1;
Sitevdir. properties ["approot"] [0] = "/lm/w3svc/" + siteid + "/root ";
Sitevdir. properties ["appfriendlyname"] [0] = "root ";
Sitevdir. commitchanges ();
Site. commitchanges ();
Return true;
}
Catch
{
Return false;
}
}

// list of virtual directories of a virtual site
Public String dirall (string websitename, string vdirname, string path)
{< br> directoryentry root = new directoryentry ("IIS: // localhost/W3SVC");
string siteid = "";
string hostname;
foreach (directoryentry BB in root. children)
{< br> try
{< br> // If (convert. toint32 (BB. name. trim () <0) continue;
propertyvaluecollection PVC = BB. properties ["serverbindings"];
string [] srvbindings = (string) PVC [0]). split (New char [] {':'});
hostname = srvbindings [2]. trim ();
If (hostname. equals ("") hostname = "localhost";

// Judge, replace hostname
// If (websitename = BB. properties ["servercomment"]. value. tostring () siteid = BB. Name;
If (websitename = hostname) siteid = BB. Name;
// Clear variable
Hostname = "";
}
Catch {}
}

Directoryentry deroot = new directoryentry ("IIS: // localhost/w3svc/" + siteid + "/root ");

If (vdirname = NULL)
{
String realpath = deroot. properties ["path"]. value. tostring ();
String vpath = deroot. path;
Try
{
String alldir = "";
String cc = "<br> ";

Foreach (directoryentry A in deroot. Children)
{
Alldir = alldir + "<a href =? Sitename = "+ websitename +" & vdir = "+ A. Name +"> "+ A. Name +" <br> ";
}
Directoryinfo di = new directoryinfo (realpath );
Directoryinfo [] subdirectoryentries = Di. getdirectories ();
Foreach (directoryinfo subdirectory in subdirectoryentries)
{
Cc = CC + subdirectory. Name + "<br> ";
}

return alldir;
}< br> catch
{< BR >}< br> return "end ";
}< br> else
{< br> directoryentry subroot = deroot. children. find (vdirname, "iiswebvirtualdir");
string realpath = subroot. properties ["path"]. value. tostring () + "\" + path + "\";
try
{< br> string cc = "
";
string filename = "
";

Directoryinfo di = new directoryinfo (realpath );
Directoryinfo [] subdirectoryentries = Di. getdirectories ();
Foreach (directoryinfo subdirectory in subdirectoryentries)
{
Cc = CC + "<a href = '? Sitename = "+ websitename +" & vdir = "+ vdirname +" & Path = "+ path + @" \ "+ subdirectory. name + "'>" + subdirectory. name + "</a> <br> ";
}

// Create an array representing the files in the current directory.
Fileinfo [] Fi = Di. getfiles ();
Foreach (fileinfo fitemp in FI)
{
Filename = filename + fitemp. Name + "<br> ";
}

Return CC + filename;
}
Catch
{
Return "no" + path;
}

}
}

}
}

The usage is as follows: In the iismanager. aspx File

<% @ Page Language = "C #" autoeventwireup = "false" %>
// <% @ Import namespace = "com. todayisp. Identity" %>
<% @ Import namespace = "com. todayisp. iismanager" %>
<%
String website = request. Params ["website"];
String vdir = request. Params ["vdir"];
String managetype = request. Params ["managetype"];
Int counter = 2; // limit the number of virtual directories created
// String username = request. Params ["username"];
// String Password = request. Params ["password"];

If (website = NULL & vdir = NULL)
{
Response. Write ("error: enter the site name or directory name .");
Return;
}

// This part uses the RSA encryption and decryption method.
/* If (username = NULL & Password = NULL)
{
Response. Write ("error: the user name and password are empty .");
Return;
}

Try
{
Iden key = new iden ();
String passwordkey = key. changekey (password );
If (passwordkey = NULL) return;
}*/

Try
{/*
// Start with a fake identity
Iden identity = new iden ();
Bool in = identity. changerolein (username, passwordkey );
If (! In ){
Response. Write ("error: failed to Change User Permissions ");
Return;
}*/

Iismanager ismang = new iismanager ();
String test = "";
Switch (managetype)
{
Case "Del ":
Test = ismang. delvirtualdirectory (website, vdir );
Break;
Case "CREATE ":
Test = ismang. createvdir (website, vdir, counter );
Break;
Default:
Response. Write ("error: no operation type is selected .");
Break;
}

Response. Write (test );
/*
// End with a fake identity
Identity. changeroleout ();
*/
}
Catch (exception e ){
Response. Write ("error:" + E. Message );
}

%>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.