locked
How to do page redirect in ASP.net RRS feed

  • Question

  • User-218090889 posted

    Hi friends,

    I am trying to upgrade my web application project by modifying the code format to use the recent ASP.net Login method.

    I am using ASP.net web form built in template.
    What I want to achieve from the template is, to redirect user to a page (MyPage) when user signs in from Login page.
    Below is the Login Code (the template code), and the web config I try to use.

    (A) Template Login code

     protected void LogIn(object sender, EventArgs e)
            {
                if (IsValid)
                {
                    // Validate the user password
                    var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                    var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
    
                    // This doen't count login failures towards account lockout
                    // To enable password failures to trigger lockout, change to shouldLockout: true
                    var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
    
                    switch (result)
                    {
                        case SignInStatus.Success:
                            IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                            break;
                        case SignInStatus.LockedOut:
                            Response.Redirect("/Account/Lockout");
                            break;
                        case SignInStatus.RequiresVerification:
                            Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}", 
                                                            Request.QueryString["ReturnUrl"],
                                                            RememberMe.Checked),
                                              true);
                            break;
                        case SignInStatus.Failure:
                        default:
                            FailureText.Text = "Invalid login attempt";
                            ErrorMessage.Visible = true;
                            break;
                    }
                }
            }

    (B) Web config

      <authentication mode="Forms">
          <forms loginUrl="~/MyPage.aspx"  slidingExpiration="true" timeout="2880"></forms>
        </authentication>

    I used the above code, but when I run it, it keep redirecting me to Default page.

    Please how do I modify the above code to be able to redirect to MyPage.aspx

    Sunday, January 6, 2019 4:51 PM

Answers

  • User475983607 posted

    I tryied this but it still redirect me to the default page

    Then you've done something wrong.  Here is my test code which redirects to Contacts.aspx after a successful login.

    IdentityHelper.RedirectToReturnUrl("/Contact.aspx", Response);
    
    

    Review you code and perhaps try setting a break point and running your code through the debugger.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 6, 2019 6:18 PM

All replies

  • User475983607 posted

    What I want to achieve from the template is, to redirect user to a page (MyPage) when user signs in from Login page.

    The template redirects the user to the secured resource the user tried to access before login.  If you want to redirect the user to a specific page then simply do so.

    IdentityHelper.RedirectToReturnUrl("MyPage.aspx", Response);

    Sunday, January 6, 2019 5:13 PM
  • User-218090889 posted

    IdentityHelper.RedirectToReturnUrl("MyPage.aspx", Response);

    I tryied this but it still redirect me to the default page

    Sunday, January 6, 2019 5:41 PM
  • User475983607 posted

    I tryied this but it still redirect me to the default page

    Then you've done something wrong.  Here is my test code which redirects to Contacts.aspx after a successful login.

    IdentityHelper.RedirectToReturnUrl("/Contact.aspx", Response);
    
    

    Review you code and perhaps try setting a break point and running your code through the debugger.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 6, 2019 6:18 PM