Hướng dẫn rewrite url trong ASP.NET

Admin

AdminAdmin is verified member.

Well-Known Member
Staff member
Administrator
Kỹ thuật rewrite URL là kỹ thuật dùng để che giấu url thật nhằm chống lại khả năng tấn công vào url.

Ví dụ: thật sự bạn cần đưa ra 1 url như sau:
(1) domain/?product.aspx?productID=123&ProductType=Hardware

Nhưng thực tế trên thanh địa chỉ của trình duyệt thì phải là:
(2) domain/Product/Hardware/123 chẳng hạn.

Lúc này RewiteURL đã làm cái việc chuyển đổi (2) -> (1) theo một quy tắc người lập trình quy định.

Ví dụ dưới kèm theo sẽ minh họa cách rewrite những Url aspx thành aspvn

Cách bước thực hiện như sau:

  1. Tạo 1 class có tên là RewriteUrlClass thừa kế từ IHttpModule
    Code:
    [COLOR=black][FONT=Courier New] [COLOR=blue]using[/COLOR] System;
     [COLOR=blue]using[/COLOR] System.Web;
      
     [COLOR=blue]public[/COLOR] [COLOR=blue]class[/COLOR] RewriteUrlClass : IHttpModule
     {
     [COLOR=blue]    #region[/COLOR] IHttpModule Members
      
         [COLOR=blue]public[/COLOR] [COLOR=blue]void[/COLOR] Dispose()
         {
         }
      
         [COLOR=blue]public[/COLOR] [COLOR=blue]void[/COLOR] Init(HttpApplication context)
         {
             context.BeginRequest += Context_BeginRequest;
         }
      
         [COLOR=blue]private[/COLOR] [COLOR=blue]static[/COLOR] [COLOR=blue]void[/COLOR] Context_BeginRequest([COLOR=blue]object[/COLOR] sender, EventArgs e)
         {
             HttpApplication httpApplication = (HttpApplication) sender;
             [COLOR=blue]string[/COLOR] url = httpApplication.Request.RawUrl.ToLower();
      
             [COLOR=green]// Nếu là Url ảo như sau"[/COLOR]
             [COLOR=blue]if[/COLOR] (url.Contains("/default.aspvn"))
             {
                 [COLOR=green]// Thì Url thực mà Server cần xử lý là:[/COLOR]
                 httpApplication.Context.RewritePath("Default.aspx");
             }
      
             [COLOR=green]// Nếu là Url ảo như sau"[/COLOR]
             [COLOR=blue]if[/COLOR] (url.Contains("/login.aspvn"))
             {
                 [COLOR=green]// Thì Url thực mà Server cần xử lý là:[/COLOR]
                 httpApplication.Context.RewritePath("Login.aspx");
             }
      
             [COLOR=green]// Tùy thuộc vào quy tắt Rewrite mà chúng ta xử lý.[/COLOR]
             [COLOR=green]// Một trong những cách hiệu quả nhất là dùng Regex Expression.[/COLOR]
      
         }
      
     [COLOR=blue]    #endregion[/COLOR]
     }
     [/FONT][/COLOR]
  2. Đăng kývào httpModules trong Web.config như dưới đây:

    Code:
    [COLOR=black][FONT=Courier New][COLOR=black][FONT=Courier New] [COLOR=blue]<[/COLOR]system.web[COLOR=blue]>[/COLOR]
     [COLOR=blue]        <[/COLOR]httpModules[COLOR=blue]>[/COLOR]
     [COLOR=blue]            <!--[/COLOR][COLOR=green] BEGIN: MY URL REWRITE [/COLOR][COLOR=blue]-->[/COLOR]
     [COLOR=blue]            <[/COLOR]add[COLOR=blue] [/COLOR][COLOR=red]name[/COLOR][COLOR=blue]=[/COLOR]"[COLOR=blue]MyUrlRewriter[/COLOR]"[COLOR=blue] [/COLOR][COLOR=red]type[/COLOR][COLOR=blue]=[/COLOR]"[COLOR=blue]RewriteUrlClass[/COLOR]"[COLOR=blue]/>[/COLOR]
     [COLOR=blue]            <!--[/COLOR][COLOR=green] END: MY URL REWRITE [/COLOR][COLOR=blue]-->[/COLOR]
     [COLOR=blue]        </[/COLOR]httpModules[COLOR=blue]>[/COLOR]
             .
             .
             .
     [/FONT][/COLOR]
      [/FONT][/COLOR]
        [COLOR=black][FONT=Courier New] [COLOR=blue]</[/COLOR]system.web[COLOR=blue]>[/COLOR]
    [/FONT][/COLOR]
  3. Chạy thử với các url có nằm trong quy tắc rewite RewriteUrlClass trong class có phần mở rộng là aspvn
Download tuoitreit.vn_RewiteUrlExample.zip (3.62 KB)
 

Facebook Comments

Similar threads

Admin
Replies
0
Views
2K
AdminAdmin is verified member.
Admin
C
Replies
3
Views
1K
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
984
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
1K
AdminAdmin is verified member.
Admin
V
Replies
1
Views
995
dobietlai
D
Admin
Replies
0
Views
1K
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
1K
AdminAdmin is verified member.
Admin
Back
Top