[英]How to pass route data from an ActionFilterAttribute to an action method?
I'm routing users to a login-view if the session has expired, using an ActionFilterAttribute
, like shown below. 如果会话已过期,我将使用ActionFilterAttribute
将用户路由到登录视图,如下所示。
Now, I want to keep the route data from the original request, so that I can route the user back to that view after logging in. 现在,我想保留原始请求中的路线数据,以便我可以在登录后将用户路由回到该视图。
How can I send the original route data to the Login
action method? 如何将原始路线数据发送到“ Login
操作方法?
public class BaseController : Controller
{
public int? BranchId {get => HttpContext.Session.GetInt32("BranchId") as int?;}
public string Admin {get => HttpContext.Session.GetString("Admin") as string;}
public BaseController() {}
}
public class AdminOrBranchesAccessAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
base.OnActionExecuting(context);
if ((context.Controller as BaseController).Admin == null &&
(context.Controller as BaseController).BranchId == null)
{
context.Result = new RedirectToRouteResult(new RouteValueDictionary(new
{
controller = "Home",
action = "Login"
}));
}
}
}
[AdminOrBranchesAccess]
public async Task<IActionResult> Details(int? id)
{
// Some stuff going on
return View();
}
For rediecting to previous action after login successfully, you need to provide return url for login action and set its value in OnActionExecuting
. 为了成功登录后重新定向到先前的操作,您需要为登录操作提供返回URL,并在OnActionExecuting
设置其值。
Change Login
method like below with parameter returnUrl
使用参数returnUrl
更改如下Login
方法
[HttpGet] [AllowAnonymous] public async Task<IActionResult> Login(string returnUrl = null) { // Clear the existing external cookie to ensure a clean login process await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); ViewData["ReturnUrl"] = returnUrl; return View(); } [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: true); if (result.Succeeded) { _logger.LogInformation("User logged in."); return RedirectToLocal(returnUrl); } if (result.RequiresTwoFactor) { return RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe }); } if (result.IsLockedOut) { _logger.LogWarning("User account locked out."); return RedirectToAction(nameof(Lockout)); } else { ModelState.AddModelError(string.Empty, "Invalid login attempt."); return View(model); } } // If we got this far, something failed, redisplay form return View(model); }
AdminOrBranchesAccessAttribute
public class AdminOrBranchesAccessAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); if ((context.Controller as BaseController).Admin == null && (context.Controller as BaseController).BranchId == null) { context.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Home", action = "Login", returnUrl = context.HttpContext.Request.Path })); } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.