R – IIS7 url rewrite w/ ASP.Net 3.5 SP1 + asp:Login Form does not work

.net-3.5asp.netauthenticationurl-rewriting

This question was kind of touched before but not the answer I was looking for.

I am using the IIS7 URL Rewrite module to rewrite my pages, and now my asp.net login form does not work!!!

On my master page I have this (ASP.Net 3.5 SP1 feature)…

    if (!String.IsNullOrEmpty(Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
    {
        form1.Action = Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
    }

Which makes the page post back to the currently rewritten page.

However my login control just post backs without firing ANY events. Therefore it does not log in, the onlogginerror etc events don't fire, nothing!!

I have tried this…

    if (!String.IsNullOrEmpty(Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
    {
        Login Login1 = LoginView1.FindControl("Login1") as Login;
        if (Login1 != null)
            Login1.DestinationPageUrl = Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
    }

ith no avail…

Please also note I am using the CSS Friendly Adapters for my login control, and even tried changing this line here from…

PostBackOptions options = new PostBackOptions(btn, "", "", false, false, false, clientSubmit, true, login.UniqueID);

to…

PostBackOptions options = new PostBackOptions(btn, "", HttpContext.Current.Request.ServerVariables["HTTP_X_ORIGINAL_URL"], false, false, false, clientSubmit, true, login.UniqueID);

with no avail…

Please help 🙁

Best Answer

Sorry spoke too soon...

As mentioned in another topic, this web site has the solution.

Just basically add the App_Browser file, and create the form rewriter file.

Form.browser:

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

</browsers>

FormRewriter.cs:

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

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

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

    public RewriteFormHtmlTextWriter(System.IO.TextWriter writer) : base(writer)
    {
        base.InnerWriter = writer;
    }

    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")) {

            HttpContext Context = default(HttpContext);
            Context = HttpContext.Current;

            if (Context.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 = Context.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

                Context.Items["ActionAlreadyWritten"] = true;
            }
        }

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