Reputation: 1296
I have an ASP.net site with C# backend code. We have the following (abridged) code in it, which suits our needs, but it could be better. This is on a page called SearchResults.aspx. If the user is not logged in, the links will redirect them to the login page. If they are logged in, it will redirect them to a lookup page for that item. What I want it to do is to redirect them to the corresponding item page after they log in if they click the "not logged in link". In what way would I need to supply the returnURL to the login page? Every way I've tried, it just redirects me to the default page after login.
<AnonymousTemplate>
<!--Want to change the link below so that the return URL will take me to
ItemInformation.aspx-->
<%# DataBinder.Eval(Container.DataItem, "itemnumber").ToString().Trim() %><br/>
<asp:HyperLink runat="server" ID="HyperLink1" NavigateUrl='Account/Login.aspx'>
Please login to review information for this item.</asp:HyperLink>
</AnonymousTemplate>
<LoggedInTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#
"~/ItemInformation.aspx?&ItemID=" + DataBinder.Eval(Container.DataItem,
"itemnumber").ToString().Trim() + "&itemdept=" + DataBinder.Eval(
Container.DataItem, "department").ToString()%>'
Text='<%# DataBinder.Eval(Container.DataItem, "itemnumber")%>'>
</asp:HyperLink>
</LoggedInTemplate>
Edit - I'm using the default login structure for an ASP.net Web Application template, so this is all that is in the Login backend.
protected void Page_Load(object sender, EventArgs e)
{
string returnUrl = Request.QueryString["ReturnUrl"];
RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" +
HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
}
Upvotes: 1
Views: 26089
Reputation: 1296
In one of my attempts to try to get it to work, I was only attempting to set the DestinationPageUrl attribute for my asp:Login control. For some reason, it required me to use both this and the OnLoggedIn event together (I was not aware of this events existence before Zerkey pointed it out). The Additional question mark in the return URL was also causing issues, so here's what I did in Login.aspx.
Markup:
<asp:Login ID="LoginUser" runat=server" EnableViewState="false"
RenderOuterTable="false" OnLoggedIN="UserLoginOnLoggedIn">...</asp:Login>
Code:
protected void UserLoginOnLoggedIn(object sender, EventArgs e)
{
string itemid, itemdept;
try
{
s1 = Request.QueryString["ItemID"].Trim();
s2 = Request.QueryString["Dept"].Trim();
}
catch
{
//makes strings null if querystrings aren't present
s1 = "";
s2 = "";
}
string returnUrl = Request.QueryString["ReturnUrl"] + "&ItemID=" +
Request.QueryString["ItemID"] + "&Dept=" +
Request.QueryString["Dept"];
if ((!String.IsNullOrEmpty(returnUrl) && !String.IsNullOrEmpty(s1) &&
!String.IsNullOrEmpty(s2)))
LoginUser.DestinationPageUrl = returnUrl;
else
LoginUser.DestinationPageUrl = "~/Default.aspx";
Response.Redirect(LoginUser.DestinationPageUrl);
}
Upvotes: 0
Reputation: 795
On SearchResults.aspx
if (!Request.IsAuthenticated)
{
Response.Redirect("/Login.aspx/?ReturnURL="+HttpContext.Current.Request.Url.AbsoluteUri); // dont forget to use urlencode
}
On Login.aspx
protected void Login_Click()
{
if (Request.QueryString["ReturnURL"] != null)
{
Response.Redirect(Request.QueryString["ReturnURL"]);
}
else
{
Response.Redirect("/Home.aspx");
}
}
Upvotes: 3