Analysis of file system operation risks of ASP. NET Virtual Hosts

Source: Internet
Author: User

The file system operation hidden danger of ASP. net vm is that before we write code, we need to know several main classes we need. These classes are all in the System. IO namespace. The System. IO namespace contains classes that allow synchronous and asynchronous read/write on data streams and files.

At the beginning of the application, we need to know the System information of the server. This requires the System. Environment class, which provides information about the current Environment and platform and methods for operating on them. We use System. the Environment class can get the current directory and system directory of the system, which allows us to quickly find several key directories. We can also get the username of the running process to help us understand ASP.. NET program, and further set user permissions to avoid this security problem.

The other classes that use the System. IO namespace are:

System. IO. Directory: provides classes for creating, moving, and enumerating static methods through directories and subdirectories

System. IO. File: provides a class for creating, copying, deleting, moving, and opening files.

System. IO. FileInfo: class that provides instance methods for creating, copying, deleting, moving, and opening files

System. IO. StreamReader: implements a TextReader to read characters from the byte stream with a specific encoding.

The specific usage of the attributes and methods of each class we use will be described in the program as code annotations.

The System. IO namespace is in mscorlib. dll provided by. net framework. You need to reference this Dll to this handler before using VS. Net programming.

All programs we write use the Codebehind method, that is, each aspx program has a corresponding aspx. in the cs program and aspx program, only Code related to page display is written, and all logic implementation code is placed in the corresponding aspx. in the cs file, the display and logic can be better separated. Since our purpose is not to discuss Codebehind technology, we will not discuss it more.

In this article, we will only introduce the usage of several main classes and their key methods. For details about the program, please refer to the included source code.

Program 1: listdrivers. aspx program that displays the current information of the server and the names of all logical drives

Main Method 1: We use the GetSysInf () method to obtain information about the current environment and platform of the server.

 
 
  1. // Method for obtaining system information, which is in the listdrivers. aspx. cs File 
  2. Public VoidGetSysInf (){
  3. // Obtain the operating system type 
  4. QDrives = Environment. OSVersion. ToString ();
  5. // Obtain the system folder 
  6. QSystemDir = Environment. SystemDirectory. ToString ();
  7. /* Obtain the amount of physical memory mapped to the process context. With this memory ing, you can understand the amount of system physical memory required for ASP. NET programs to run,
  8. This helps us better plan our entire application, because the physical memory volume is measured in bytes,
  9. Therefore, we divide this value by 1024 to obtain the physical memory size in KB */ 
  10. QMo = (Environment. WorkingSet/1024). ToString ();
  11. // Obtain the fully qualified path of the current directory, that is, the directory from which the process is started. 
  12. QCurDir = Environment. CurrentDirectory. ToString ();
  13. // Obtain the network domain name of the host 
  14. QDomName = Environment. UserDomainName. ToString ();
  15. // Obtain the number of milliseconds after the system is started 
  16. QTick = Environment. TickCount;
  17. // Calculate the number of minutes after the system is started 
  18. QTick/= 60000;
  19. // Obtain the machine name 
  20. QMachine = Environment. MachineName;
  21. // Obtain the user name for running the current process 
  22. QUser = Environment. UserName;
  23. /* Retrieve the name of the logical drive in the format of "<drive letter >:\" on this computer and return a string array, which is the key to the next step */ 
  24. AchDrives = Directory. GetLogicalDrives ();
  25. // Obtain the dimension of the string array and determine the number of logical drives. 
  26. NNumOfDrives = achDrives. Length;
  27. }

The file system operation risks of ASP. NET virtual hosts do not need to be operated on in the system information. We simply use asp: Label to display them. The number of logical drives varies on different servers. Therefore, the name of the logical drive is stored in an indefinite array, and the name of the logical drive is also the basis for browsing directories and files, therefore, we use the data grid DataGrid to display and process it.

Code for displaying and processing the DataGrid of the logical drive name in the listdrivers. aspx file ):

 
 
  1. <Asp: DataGrid id ="Drivers> Grid"Runat ="Server"AutoGenerateColumns ="False">
  2. <Columns
  3. <Asp: BoundColumn HeaderText ="ID"DataField ="ID"/>
  4. <Asp: BoundColumn HeaderText ="Disk name"DataField ="Drivers"/>
  5. <Asp: HyperLinkColumn
  6. HeaderText ="Details"
  7. DataNavigateUrlField ="Drivers"DataNavigateUrlFormatString ="Listdir. aspx? Dir = {0 }"
  8. DataTextField ="Detail"
  9. Target ="_ New"/>
  10. </Columns>
  11. </Asp: DataGrid>

The first two BoundColumn columns show the sequence number and the actual logical drive name. The third column must be noted, before entering the display directory and file of each logical drive, we need to pass the name of the selected logical drive to the file of the display directory. Therefore, a special hyperlink line HyperLinkColumn is required, we set DataNavigateUrlField to the URL field of the hyperlink to be bound to the HyperLinkColumn in the data source, where it is the name of the logical drive. Then, set DataNavigateUrlFormatString to the display format of the hyperlink URL in the HyperLinkColumn when the URL data is bound to a field in the data source, that is, the next level of processing page to be linked. Here it is listdir. aspx? Dir = {name of the logical drive you clicked on}

The information about the file system operation risks of ASP. NET virtual hosts is introduced here.

  1. Analysis of Date and Time Processing in ASP. NET Programming
  2. Analysis of the pop-up window alarm prompt for ASP. NET Programming
  3. Meanings and benefits of ASP. NET stored procedure calls
  4. Analysis of ASP. NET application Resource Access Security Model
  5. Major security risks of ASP. NET Virtual Hosts

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.