简体   繁体   中英

Encode hash in ReturnUrl ASP.NET Login control

We have a ASP.NET login control in our aspx:

<asp:Login ID="LogOnControl" runat="server" OnLoginError="OnLogOnError" OnLoggedIn="OnLoggedIn">

and in OnLoggedIn we do:

protected void OnLoggedIn(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
    {
        Response.Redirect(Request.QueryString["ReturnUrl"]);
    }
}

and our ReturnUrl should be something like Default.aspx#/Projects

The problem is that all after hash is trimmed. We need to pass entire URL to server (so encode it somehow).

Jabko87 - you cannot get values after # because it is not being sent to the server. You must develop your own mechanism to handle it.

Compare: Pass URL's with hash value for redirection

And: How to get Url Hash (#) from server side

I achieved it by defining hidden input on aspx page:

<input type="hidden" id='<%= ReturnUrlHash %>' name='<%= ReturnUrlHash %>' />

and filling it with JS:

document.getElementById('<%= ReturnUrlHash %>').value = window.location.hash;

and on server side in Logon.aspx.cs I did:

public const string ReturnUrlHash = "returnUrlHash";
protected void OnLoggedIn(object sender, EventArgs e)
{
    string returnUrl = Request.QueryString["ReturnUrl"];
    if (!string.IsNullOrEmpty(returnUrl))
    {
        if (Request.Form.AllKeys.Contains(ReturnUrlHash))
        {
            returnUrl += Request.Form[ReturnUrlHash];
        }

        Response.Redirect(returnUrl);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM