简体   繁体   中英

How to redirect after authentication with MVC5 views

I have an MVC5 project and a Razor view which shows people their login name. What I want to do is give people the Not me option to sign in as a different user and then be redirected back to this view.

Here's what I have currently:

@if (Request.IsAuthenticated)
{
    <div class="alert alert-dismissable alert-warning">
    You are signed in as: @HttpContext.Current.User.Identity.Name
    @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "" }))
    {
        @Html.AntiForgeryToken()
        <a href="javascript:document.getElementById('logoutForm').submit()">Change</a>
    }
    </div>
}

The only problem with this is that after signing in, I would like the user to be automatically redirected back to this page rather than to the home page. How can I do that? Thank you.

You may use the UrlReferrer property of Request object in your loggof method to get the referring url and pass that to your login action method and use that for redirecting back the user after successful login.

You can pass the returnurl either as a querystring to your login action method or use TempData .

[HttpPost]
public ActionResult LogOff()
{
   var prevUrl = Request.UrlReferrer.AbsoluteUri;
   //Do the things to end the session       
   return RedirectToAction("Login", new { returnUrl=prevUrl});
}
public ActionResult Login(string returnUrl="")
{
  var loginVM=new LoginVM();
  if(!String.IsNullOrEmpty(returnUrl))
     loginVM.ReturnUrl=returnUrl;

  return View(loginVM);
}

If you prefer TempData approach

   var prevUrl = Request.UrlReferrer.AbsoluteUri;
   //Do the things to end the session   
   TempData.ReturnUrl=prevUrl;    
   return RedirectToAction("Login");

And in the Login Action method read it from TempData .

Make sure you have the ReturnURL property of your LoginVM as a part of the form(in a hidden variable) so that when user post the login form, it will be available in the Login HttpPost action method

@model LoginVM
@using(Html.BeginForm())
{
  // you other login form elements (username,password) here
  @Html.HiddenFor(s=>s.ReturnUrl)
  <input type="submit" />
}

and in your HttpPost action

[HttpPost]
public ActionResult Login(LoginVM model)
{
  //if login was successful, use model.ReturnUrl to redirect
}

You need to do a little work in your login controller and your links to achieve this. Basically you will pass in the page you came from to the login controller as a parameter, and then use that link to forward the user back to that page on successful login.

Add return url in Beginform of your login view.

@using (Html.BeginForm("LoginActionABC", "LoginControllerABC", new { **ReturnUrl** = 'your url' }))

Add new parameter to your LoginActionABC.

public ActionResult LoginActionABC(string returnUrl)

this may help you

Here is the code I used which got this working in the end.

In the Razor view I just needed to add a hidden input with the returnUrl value:

@if (Request.IsAuthenticated)
{
    <div class="alert alert-dismissable alert-warning">
    You are signed in as: @HttpContext.Current.User.Identity.Name
    @using (Html.BeginForm("ChangeLogin", "Account", FormMethod.Post, new { id = "logoutForm", @class = ""}))
    {
        @Html.AntiForgeryToken()
        <input type="hidden" name="returnUrl" value="/Item/Bid/@Model.ID" />
        <a href="javascript:document.getElementById('logoutForm').submit()">Change</a>
    }
    </div>
}

And in the Account controller I added a new method to perform the logout and redirect to the login page with the return URL:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ChangeLogin(string returnUrl)
    {
        AuthenticationManager.SignOut();
        return RedirectToAction("Login", "Account", new System.Web.Routing.RouteValueDictionary { { "returnUrl", returnUrl } });
    }

This successfully navigates the user back to the original view after changing the authentication. Many thanks for other suggestions which helped me get to this point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM