Share some experience on ASP. NET moderate Security Mode

Source: Internet
Author: User
Tags emit website hosting

If you are developing a general-purpose Web product, such as BBS, CMS, and blog, you are advised to read the following article.

Non-General web programs or products generally do not deal with the moderate security mode of ASP. NET, because the user group experience is relatively fixed, or the deployment environment can be determined by the program provider.

But when you are doing general-purpose Web products, you have to deal with a variety of people. Some webmasters use foreign space, such as Godaddy. Foreign space vendors usually use ASP.. Net code execution permission control in medium security mode.

In the medium-security mode, many things we are accustomed to cannot be done.

What is the medium security mode?

 

Many people may have never been familiar with the moderate security mode. Before I joined the bbsmax project, I did not know about the moderate security mode.

To put it simply, ASP. NET provides a simple code execution permission setting solution called "trust level ".

It provides five trust levels by default: fulltrust, high, medium, low, and minimal.

Each trust-level setting corresponds to a set of code permission settings.

This solution allows the website deployer to quickly set website hosting code execution permissions through web. config.

By setting the level Attribute Value of the <system. Web>/<trust> node of Web. config, you can set ASP. NET programs to different trust levels.

After ASP. NET is installed, all websites are trusted by fulltrust by default, which is also the highest level of trust.

The medium security mode mentioned in this Article corresponds to the medium trust level.

Because the managed code execution permission model is not the focus of this article, I will only give a brief description here, not to discuss ASP in depth. net Security Level settings implementation principle, the implementation principle can refer to the last few connections provided in this Article.

What are the effects of the moderate security mode?

 

My colleagues and I have encountered some problems in ASP. NET moderate security mode:

1. The template Mechanism Based on virtualpathprovider cannot be used, because virtualpathprovider must run in high mode at least.

2. buildprovider is unavailable, which means you cannot add your own language implementation. However, most projects won't use such advanced features.

3. codedom and emit cannot be used. This is terrible. IOC, AOP, and dynamic injection are all useless. These are based on emit instead of codedom.

4. It is not possible to take over the file download through the ASPX page. The higher code execution permission is required for response to write the file to the client.

5. Don't worry about uploading large files, because the uploading of large files is inseparable from httpworkrequst, and the Code for obtaining httpworkrequst must be in fulltrust mode.

6. SQLite cannot be used, because in moderate security mode, the permission to call unmanaged code is not available, so all requests involving non-hosted code calls besides SQLite are also discarded.

7. Access data cannot be connected with oledb, because oledbclient is unavailable in medium security mode. You can only use ODBC data sources.

Therefore, if you want to allow the user to deploy the program in medium security mode, the earlier the compatibility test in medium security mode, the better.

Because a lot of useless things involve the basic structure.

For example, SQLite and access cannot be used. If your program happens to only use the versions of these two databases, what should you do?

For example, you cannot use response to write a file stream to download a file, and your program uses anti-leech protection. What should you do?

What should I do if the medium security mode is so demanding?

 

You can only bypass the road. What else should you do? There are still some methods, so you have to keep trying.

Virtualpathprovider cannot be used, nor buildprovider, but you need to have your own template syntax. So we have to generate An ASPX page before accessing the page, and then rewrite the URL.

It's easy to say, just one sentence. However, I don't know how much code I have written or how many tests I have performed to find the best solution.

Therefore, you must find your own method.

The following code determines whether a program runs in moderate security mode:

123
if (SecurityManager.IsGranted(new AspNetHostingPermission(AspNetHostingPermissionLevel.Medium))){}

If you encounter a logic that is not required, such as getting the program memory usage or uploading large files, you can first determine whether to call it.

According to the data, deploying the Assembly to GAC can obtain fulltrust-level permissions, but I have not actually tried it.

Appendix

 

Reference link:

Msdn trust element (ASP. NET setting Architecture)

Msdn how to: use medium trust in ASP. NET 2.0

Check code access security permissions granted to your Asp.net Web Application

Appendix B

 

The file download solution provided by Chen:

123456789101112131415161718
protected override void OnInit(EventArgs e){    Response.ContentType = "application/octet-stream";     using (FileStream stream = File.Open(Server.MapPath("~/test.txt"), FileMode.Open))    {        BinaryWriter writer = new BinaryWriter(Response.OutputStream);         byte[] buffer = new byte[1024];         int l = 0;         while ((l = stream.Read(buffer, 0, buffer.Length)) > 0)        {            writer.Write(buffer, 0, l);        }    }
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.