简体   繁体   English

使用MVC5视图进行身份验证后如何重定向

[英]How to redirect after authentication with MVC5 views

I have an MVC5 project and a Razor view which shows people their login name. 我有一个MVC5项目和一个Razor视图,该视图向人们显示他们的登录名。 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. 您可以在loggof方法中使用Request对象的UrlReferrer属性来获取引用URL,并将该URL传递给您的登录操作方法,并使用该URL在成功登录后将用户重定向回。

You can pass the returnurl either as a querystring to your login action method or use TempData . 您可以将returnurl作为查询字符串传递给登录操作方法,也可以使用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 如果您喜欢TempData方法

   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 . 然后在Login Action方法中从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 确保您具有LoginVM的ReturnURL属性作为表单的一部分(在隐藏变量中),以便当用户发布登录表单时,该表单将在Login HttpPost操作方法中可用。

@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操作中

[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. 在您的登录视图的Beginform中添加返回URL。

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

Add new parameter to your LoginActionABC. 将新参数添加到您的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: 在Razor视图中,我只需要添加一个带有returnUrl值的隐藏输入:

@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: 在帐户控制器中,我添加了一种新方法来执行注销并使用返回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. 非常感谢您提出的其他建议,帮助我达到了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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