質問

アクセスを制限したい複数の .aspx ページが含まれるフォルダーがあります。そのフォルダーに web.config を追加しました <deny users="?"/>.

問題は、UrlRewrite を使用しているときに、ReturnUrl が .aspx ファイルへの物理パスを使用して自動生成されることです。

手動の認証チェックとリダイレクトを行わずに ReturnUrl を操作する方法はありますか?コードビハインドまたは web.config から ReturnUrl を設定する方法はありますか?

編集:アプリケーションは ASP.NET 2.0 WebForms を使用しています。3.5 ルーティングは使用できません。

編集2:401ステータスコードは決してキャプチャされないようです。保護されたページの場合は 302 を返し、ReturnUrl を使用してログイン ページにリダイレクトします。保護されたページの場合は 401 を返しません。ふーむ...面白い...参照: http://msdn.microsoft.com/en-us/library/aa480476.aspx

これにより事態はさらに困難になります...正規表現で ReturnUrl に一致するように逆書き換えマッピング ルールを作成し、401 が返されない場合は置き換える必要があるかもしれません...401 が返された場合は、RawUrl を Response.RedirectLocation に設定するか、ReturnUrl を RawUrl に置き換えることができます。

他に何かアイデアがある人はいますか?

役に立ちましたか?

解決 3

結局、URL に ReturnUrl が存在するかどうかを確認し、Global.asax の EndRequest ステージで RawUrl に置き換えました。今のところこれでうまくいきます...

これ ブログ投稿 セットアップを手伝ってくれました。

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);
    }
}

他のヒント

それをチェックしてください。お役に立てれば。

#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>

次のコントロール アダプターを作成して、form タグを for action 属性で書き換えます。私はこれを ASP.NET 2.0 アプリと組み合わせて使用​​しました。 インテリジェンシア URLリライタ。ここから分かりました Gu からのブログ投稿.

このクラスを App_Code フォルダーに配置します。

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);
    }
}

次に、この .browser ファイルを App_Browsers フォルダーに作成します。

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

ASP.NET 3.5 を使用している場合は、代わりに ASP.NET UrlRouting を使用してください。ただし、認可を手動で確認する必要があります。

http://chriscavanagh.wordpress.com/2008/03/11/aspnet-routing-goodbye-url-rewriting/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top