0
您好我正在創建我的第一個項目在Asp.net核心,我需要登錄到他的上一頁後重定向用戶。例如,如果他將觀看頁面「關於」,那麼他將要登錄,登錄後我需要他重定向到「關於」頁面。 這是我在Controller中的代碼。 。如何在登錄後獲取上一頁
//
// GET: /Account/Login
[HttpGet]
[Route("/Account/Login")]
[AllowAnonymous]
public IActionResult Login(string returnUrl = null)
{
try
{
if (_signInManager.IsSignedIn(User))
_signInManager.SignOutAsync();
ViewData["ReturnUrl"] = returnUrl;
return View("Login");
}
catch (Exception e)
{
return ExceptionActionResult(e);
}
}
我試圖使用Request.Headers [ 「Referer的」]的ToString();但問題是前一頁是登錄頁面。我試圖把這個方法//獲取登錄但我不知道如何將字符串發送到//帖子登錄
// POST: /Account/Login
[HttpPost]
[Route("/Account/Login")]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
try
{
//Nesmi byt prihlasen
if (_signInManager.IsSignedIn(User))
return ErrorActionResult("User is already Signed");
// string var = Request.Headers["Referer"];
string var = Request.Headers["Referer"].ToString();
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
return RedirectToLocal(returnUrl);
}
if (result.IsLockedOut)
{
_logger.LogWarning(2, "someString");
ModelState.AddModelError("UserName", "someString");
return View("Lockout");
}
else
{
_logger.LogWarning(2, "someString");
ModelState.AddModelError(string.Empty, "Invalid Password or Mail");
}
return View(model);
}
// If we got this far, something failed, redisplay form
return View(model);
}
catch (Exception e)
{
return ExceptionActionResult(e);
}
}
如果你有什麼需要或者如果你不理解我問我瞭解詳情。我會很樂意提供任何幫助。