Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET LoginUser.DestinationPageUrl not working properly

The HTML is like:

  <asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false" OnLoggedIn="LoginUser_LoggedIn"
                    OnLoginError="LoginUser_LoginError">
</asp:Login>

On LoginUser_LoggedIn event I am setting the destination URL like:

LoginUser.DestinationPageUrl = "~/mycustompage.aspx";
FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, true);

Here when there is NO ReturnUrl in page URL then it is redirecting to "~/mycustompage.aspx" page but if there is any ReturnUrl specified in the URL then it is redirecting to the ReturnUrl page.

I always want to redirect to "~/mycustompage.aspx" page whether ReturnUrl is there or not. How to achieve this?

Many many thanks in advance!

like image 231
Dukhabandhu Sahoo Avatar asked Dec 04 '13 08:12

Dukhabandhu Sahoo


1 Answers

DestinationPageUrl is the default destination page after Login.

?ReturnUrl is stronger than DestinationPageUrl so, if it is set it wins.

Response.Redirect in LoginUser_LoggedIn is stronger than ReturnUrl so, if you want to override ReturnUrl you must use this code:

 void LoginUser_LoggedIn(Object sender, EventArgs e)
    {
      Response.Redirect("~/mycustompage.aspx");
      ....
    }

DestinationPageUrl should be set in the aspx page

 <asp:Login DestinationPageUrl="~/mycustompage.aspx" ... />

or in Page_Load event to work properly.

void PageLoad(Object sender, EventArgs e)
    {
      LoginUser.DestinationPageUrl = "~/mycustompage.aspx";
      ....
    }

If you must change the destination page after login (in example if the page changes depending on the user or the user's role) you must use Server.trasfer() ore Response.Redirect().

void LoginUser_LoggedIn(Object sender, EventArgs e)
    {
      Response.Redirect("~/mycustompage.aspx");
      ....
    }
like image 67
Emanuele Greco Avatar answered Nov 09 '22 10:11

Emanuele Greco