Asp.net mvc Redirect to same page after login

Shazoo

I have a partial view which contains inputs for logging in. The partial view is present on every page of the website so that a user can login to the website from any page.

I have a base controller which is then inherited by all other controllers as below.

When the logon info is submitted, it goes to the logon action in the base controller.

How do I return the view that the logon info was submitted from?

public class BaseController : Controller
{
    [HttpPost]
    public ActionResult logon(string tx_username, string tx_password)
    {
    //Verify login details

    }
}


public class HomeController : BaseController
{
    public ActionResult Index()
    {

        return View();
    }

    public ActionResult About()
    {

        return View();
    }

    public ActionResult Contact()
    {
        return View();
    }
}
Ehsan Sajjad

Add another parameter ReturnUrl in the @Html.BeginForm which can be populated using the ViewConext.Controller.ValueProvider, and post the current action and controller name on which user is in the login post action like:

View:

First Way:

@using (Html.BeginForm("logon", 
                       "Home", 
                       FormMethod.Post, 
 new { ReturnUrl = Url.Action(@ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString(),
 @ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString()) }))
{


}

2nd Way:

@using (Html.BeginForm("logon", 
                       "Home", 
                       FormMethod.Post, 
         new { ReturnUrl = this.Request.RawUrl }))
        {


        }

Action:

[HttpPost]
public ActionResult logon(string tx_username, string tx_password,string ReturnUrl)
{
 //If login successful

 return Redirect(ReturnUrl);

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Redirect back to a page after a login

How to redirect to another page after submitting an alert in ASP MVC

MVC Redirect after login

ASP.NET MVC 5 : Endless redirect to the login page using the site template

Asp.net MVC 5 redirect to Account/Login

Redirect user after authentication with OpenIdConnect in ASP.Net MVC

Redirect to custom Windows Authentication login page if user anonymous in ASP.NET MVC

After Login User redirect different Page according to Role In asp.net

Using Asp.Net Core Identity in MVC, Authorize attribute is rebouncing to login page after succesfull login

How to redirect to ASP.NET Ideneity login page?

How to redirect to login page from AuthorizationFilter in asp net core?

ASP.NET MVC Core 3.0 after login shows login page again at the browser back button click

Username auto fill in Login Page after Register success (Redirect to Login) in asp.net mvc

ASP.NET MVC 3 Redirect Requests to Root Page

Asp.Net MVC - redirect to same page without passing parameter

ASP.NET MVC Redirect to target page or redirect to previous page after login

Redirect to login page asp.net

Redirect Page based on dropdown information in ASp.NET mvc

ASP.NET MVC SqlException does not redirect to error page

Redirect to error page from ajax call in ASP.net MVC

Redirect to login page in asp.net core idenity in ViewComponent

Asp.net Mvc 5, two account controller and Area but redirect to wrong login page

How to redirect to area after login asp.net core 3.0?

Disallow and redirect user to a different page after social login in ASP.NET Core

Redirect to another page after login

Redirect to page OnActionExecuting method ASP.NET Core 5 MVC

ASP.NET Core MVC5 - Redirect to the same section in the view after finishing the action?

Login Page with ASP.NET Core (MVC)

How to return back to the same page after login redirect in Svelte?