ASP. NET 2.0 HttpHandler implements permission protection for certain file types

Source: Internet
Author: User
Original article: http://blog.csdn.net/ChengKing/archive/2007/01/05/1475121.aspx

I sorted it out.

(1 ). httpHandlers can process requests for a specific file type. for example, in machine. by default, a majority of Handlers are processed in the config file: 2 using System. Data;
3 using System. Configuration;
4 using System. Web;
5 using System. Web. Security;
6 using System. Web. UI;
7 using System. Web. UI. WebControls;
8 using System. Web. UI. WebControls. WebParts;
9 using System. Web. UI. HtmlControls;
10
11 /// <summary>
12 // only admin users can directly view JPG and JPEG images.
13 /// </summary>
14 public class JpgHandler: IHttpHandler
15 {
16 public JpgHandler ()
17 {
18}
19 public void ProcessRequest (HttpContext hc)
20 {
21 string strFileName = hc. Server. MapPath (hc. Request. FilePath );
22 if (hc. User. IsInRole ("admin") // whether the current User is admin
23 {
24 hc. Response. ContentType = "image/JPEG ";
25 hc. Response. WriteFile (strFileName );
26}
27 else
28 {
29 hc. Response. ContentType = "image/JPEG ";
30 hc. Response. Write ("No Right ");
31}
32}
33 public bool IsReusable
34 {
35 get
36 {
37 return true;
38}
39}
40}
41 2. front page *. aspx code 1 <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs "Inherits =" _ Default "%>
2
3 <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 6 7 <title> HttpHandler validate users right </title>
8 9 <body>
10 <form id = "form1" runat = "server">
11 <div>
12 <asp: LinkButton ID = "LinkButton1" runat = "server" PostBackUrl = "a.jpeg" ToolTip = "Click me! "OnClick =" linkbutton#click "Width =" 149px "> A.jpeg </asp: LinkButton>
13 & nbsp;
14 </div>
15 </form>
16 </body>
17 18

3. register your own handler class configuration in the Web. Config file

1 <system. web>
2 3 <add verb = "*" path = "*. jpg, *. jpeg" type = "JpgHandler"/>
4 5 </system. web>
6

Here I will handle the program class JpgHandler. put cs under the App_Code folder. If this class is not placed under this class, it is set *. dll format, you should put this Assembly under the bin directory and configure it as follows:

1 <system. web>
2 3 <add verb = "*" path = "*. jpg, *. jpeg" type = "JpgHandler, YourDll"/>
4 5 </system. web>
6

(3) Download Sample Code

JpgHttpHandler.rar

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.