Return url not redirect after user login

Ali Khan asked on January 9, 2024 16:42

In my application I am validating the user if user has rights then we allow to user to access the page. When user click on page I am redirected to to login page(login?ReturnUrl=%2Fcart) after login user must need to go that return url(Cart) page but it not moving to return url page.

Recent Answers


Brenden Kehren answered on January 17, 2024 15:11

You may have to show some code, but in your Startup.cs file have you set your cookie to define the ReturnUrlParameter?

services.ConfigureApplicationCookie(c =>
{
    c.LoginPath = new PathString("/Account/SignIn"); // calls a controller and action
    c.AccessDeniedPath = new PathString("/Account/PermissionDenied"); // calls a controller and action
    c.ExpireTimeSpan = TimeSpan.FromDays(10);
    c.SlidingExpiration = true;
    c.Cookie.Name = AUTHENTICATION_COOKIE_NAME;
    c.ReturnUrlParameter = "ReturnUrl"; // this line right here?
});

What about some error handling?

app.Use(async (context, next) =>
{
    switch (context.Response.StatusCode)
    {
        case (int)HttpStatusCode.NotFound:
            // page in the content tree
            context.Request.Path = "/page-not-found";
            break;
        case (int)HttpStatusCode.Unauthorized:
            // account controller
            context.Request.Path = URLHelper.AddParameterToUrl("/Account/SignIn", "ReturnUrl", context.Request.Path);
            break;
    }
    await next();
});

Notice how we add the return URL if the user is unauthorized (not logged in)?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.