5

Tôi có một thư mục có nhiều trang .aspx mà tôi muốn giới hạn quyền truy cập. Tôi đã thêm web.config vào thư mục đó với <deny users="?"/>.Cách xử lý thanh lịch ReturnUrl khi sử dụng UrlRewrite trong ASP.NET 2.0 WebForms

Vấn đề là ReturnUrl được tạo tự động với đường dẫn vật lý đến tệp .aspx trong khi tôi đang sử dụng UrlRewrite.

Có cách nào để thao tác ReturnUrl mà không thực hiện kiểm tra và chuyển hướng xác thực thủ công không? Có cách nào để thiết lập ReturnUrl từ mã phía sau hoặc từ web.config?

EDIT: Ứng dụng đang sử dụng ASP.NET 2.0 WebForms. Tôi không thể sử dụng định tuyến 3.5.

EDIT 2: Có vẻ như mã trạng thái 401 không bao giờ bị bắt. Nó trả về 302 cho trang được bảo vệ và chuyển hướng đến trang đăng nhập bằng ReturnUrl. Nó không trả lại 401 cho trang được bảo vệ. Hmm ... Thú vị ... Ref: http://msdn.microsoft.com/en-us/library/aa480476.aspx

Điều này làm cho mọi việc trở nên khó khăn hơn ... Tôi có thể phải viết lại quy tắc lập bản đồ để regex khớp ngược ReturnUrl và thay thế nếu nó không trả về 401 ... Nếu nó trả về 401 Tôi có thể đặt RawUrl thành Response.RedirectLocation hoặc thay thế ReturnUrl bằng RawUrl.

Còn ai khác có ý tưởng nào khác không?

+0

Bất kỳ ai khác có đề xuất khác? –

Trả lời

1

tôi đã kết thúc kiểm tra sự tồn tại của ReturnUrl trong Url và thay thế nó bằng RawUrl trong giai đoạn EndRequest trong Global.asax. Tính năng này hoạt động cho tôi bây giờ ...

blog post này đã giúp tôi thiết lập.

protected void Application_EndRequest(object sender, EventArgs e) 
{ 
    string redirectUrl = this.Response.RedirectLocation; 
    if (!this.Request.RawUrl.Contains("ReturnUrl=") && !string.IsNullOrEmpty(redirectUrl)) 
    { 
     this.Response.RedirectLocation = Regex.Replace(redirectUrl, "ReturnUrl=(?'url'[^&]*)", delegate(Match m) 
     { 
      return string.Format("ReturnUrl={0}", HttpUtility.UrlEncode(this.Request.RawUrl)); 
     }, RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); 
    } 
} 
1

Kiểm tra xem. Hi vọng điêu nay co ich.

#region [ Imports ] 

using System; 
using System.Web; 
using System.Web.Security; 

#endregion 

namespace Foo.Handlers 
{ 

    public class AuthModule : IHttpModule 
    { 

     #region IHttpModule Members 

     public void Init(HttpApplication application) 
     { 
      application.PostReleaseRequestState += delegate(object s, EventArgs e) 
       { 
        if (application.Response.StatusCode == 401) 
         application.Response.Redirect(FormsAuthentication.LoginUrl + "?ReturnUrl=" + HttpUtility.UrlEncode(application.Request.RawUrl), true); 
       }; 
     } 

     public void Dispose() { } 

     #endregion 

    } 

} 

<modules> 
    <add name="AuthModule" type="Foo.Handlers.AuthModule, Foo"/> 
</modules> 
+0

Cảm ơn bạn. Tôi sẽ thử cái này. –

+0

Không có cơ hội để đánh chặn ở giữa để thay thế ReturnUrl bằng RawUrl. –

+0

Mã trạng thái 401 không bao giờ bị bắt ... Trang được bảo vệ (302) -> đăng nhập bằng ReturnUrl (200). –

1

Tạo adapter kiểm soát sau đây để viết lại toàn bộ thẻ form với cho thuộc tính hành động. Tôi đã sử dụng ứng dụng ASP.NET 2.0 này cùng với trình ghi đè url Intelligencia. Tôi nhận được từ số này blog post from the Gu.

Đặt lớp này trong thư mục App_Code của bạn:

using System.IO; 
using System.Web; 
using System.Web.UI; 

public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter 
{ 
    protected override void Render(HtmlTextWriter writer) 
    { 
     base.Render(new RewriteFormHtmlTextWriter(writer)); 
    } 
} 

public class RewriteFormHtmlTextWriter : HtmlTextWriter 
{ 
    public RewriteFormHtmlTextWriter(TextWriter writer) : base(writer) 
    { 
     base.InnerWriter = writer; 
    } 

    public RewriteFormHtmlTextWriter(HtmlTextWriter writer) : base(writer) 
    { 
     this.InnerWriter = writer.InnerWriter; 
    } 

    public override void WriteAttribute(string name, string value, bool fEncode) 
    { 

     // If the attribute we are writing is the "action" attribute, and we are not on a sub-control, 
     // then replace the value to write with the raw URL of the request - which ensures that we'll 
     // preserve the PathInfo value on postback scenarios 

     if ((name == "action")) 
     { 
      if (HttpContext.Current.Items["ActionAlreadyWritten"] == null) 
      { 

       // Because we are using the UrlRewriting.net HttpModule, we will use the 
       // Request.RawUrl property within ASP.NET to retrieve the origional URL 
       // before it was re-written. You'll want to change the line of code below 
       // if you use a different URL rewriting implementation. 
       value = HttpContext.Current.Request.RawUrl; 

       // Indicate that we've already rewritten the <form>'s action attribute to prevent 
       // us from rewriting a sub-control under the <form> control 
       HttpContext.Current.Items["ActionAlreadyWritten"] = true; 

      } 
     } 
     base.WriteAttribute(name, value, fEncode); 
    } 
} 

Sau đó, tạo tập tin .browser này trong thư mục App_Browsers của bạn:

<browsers> 
    <browser refID="Default"> 
    <controlAdapters> 
     <adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="FormRewriterControlAdapter" /> 
    </controlAdapters> 
    </browser> 
</browsers> 
+0

Có vẻ như trang này sẽ xử lý URL của trang khi đăng lại.Những gì tôi đang tìm kiếm là Url viết lại sạch sẽ cho chuỗi truy vấn ReturnUrl được tạo tự động khi bạn nhấn một trang bị hạn chế chưa được xác thực. –

+0

Tôi tin rằng nó cũng nên làm điều đó, nhưng tôi không chắc chắn. – craigmoliver

Các vấn đề liên quan