gpt4 book ai didi

asp.net-core - 如何从 ActionFilter 重定向到 Asp.net Core 2.2 中的另一个操作或路由?

转载 作者:行者123 更新时间:2023-12-04 11:33:59 26 4
gpt4 key购买 nike

如何从 ActionFilter 重定向到 Asp.net Core 2.2 中的另一个 Controller / Action
上下文.异常处理;//不显示
我已经尝试了这些线程的所有选项:

  • How to get filter to redirect to another action?
  • Redirecting to specified controller and action in asp.net mvc action filter
  • Redirect from exception filter

  • 这是我的代码
    public class ValidaEmpresaActionFilter : IActionFilter
    {
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly IHttpContextAccessor _contextAccessor;

    public ValidaEmpresaActionFilter(UserManager<ApplicationUser> userManager, IHttpContextAccessor contextAccessor)
    {
    _userManager = userManager;
    _contextAccessor = contextAccessor;
    }

    public async void OnActionExecuting(ActionExecutingContext context)
    {
    string username = _contextAccessor.HttpContext.User.Identity.Name;
    var user = await _userManager.FindByNameAsync(username);

    // all options i tried Option 1
    if (user.EmpresaPrincipal == null || user.EmpresaPrincipal < 1)
    {

    RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
    redirectTargetDictionary.Add("action", "Index");
    redirectTargetDictionary.Add("controller", "Home");

    context.Result = new RedirectToRouteResult(redirectTargetDictionary);
    }
    // option #2
    if (user.EmpresaPrincipal == null || user.EmpresaPrincipal < 1)
    context.Result = new RedirectResult("~/Home/index");

    // option #3
    if (user.EmpresaPrincipal == null || user.EmpresaPrincipal < 1)
    {
    context.Result = new RedirectToRouteResult(
    new RouteValueDictionary
    {
    { "controller", "Home" },
    { "action", "Index" }
    });
    }
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {

    }
    }
    在启动/配置服务中
    services.AddScoped<ValidaEmpresaActionFilter>();
    更新:我找到了答案
    刚刚添加了这一行 await context.Result.ExecuteResultAsync(context);这是结果代码:
    public async void OnActionExecuting(ActionExecutingContext context)
    {
    string username = _contextAccessor.HttpContext.User.Identity.Name;
    var user = await _userManager.FindByNameAsync(username);

    if (user.EmpresaPrincipal == null || user.EmpresaPrincipal < 1)
    {
    RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
    redirectTargetDictionary.Add("action", "Index");
    redirectTargetDictionary.Add("controller", "Home");
    redirectTargetDictionary.Add("area", "");

    context.Result = new RedirectToRouteResult(redirectTargetDictionary);
    await context.Result.ExecuteResultAsync(context);
    }
    }

    最佳答案

    听说我做 [MyAuth] Action 过滤器,如果身份验证通过继续否则重定向到“拒绝访问”页面

        public class MyAuth : ActionFilterAttribute
    {

    public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
    var headers = context.HttpContext.Request.Headers;
    if(headers.ContainsKey("Authentication"))
    {
    var value = AuthenticationHeaderValue.Parse(headers["Authentication"]);
    var bytes = Convert.FromBase64String(value.Parameter);
    var crendentials = Encoding.UTF8.GetString(bytes).Split(":");
    var username = crendentials[0];
    var password = crendentials[1];

    if(Validate(username, password))
    {
    // user is valid, huuuray
    base.OnActionExecutionAsync(context, next);
    return;

    }

    }
    // should redirect to somewhere
    context.Result = new RedirectResult("~/api/access_deny");
    await context.Result.ExecuteResultAsync(context);

    }
    }
    请注意添加这一行:
                await context.Result.ExecuteResultAsync(context);
    在我的 Api Controller 中的某处,在任何操作之前需要我的自定义 [MyAuth] :
    ...
    // GET: api/<ValuesController>
    [HttpGet]
    [MyAuth] // <-------------------
    public IEnumerable<string> Get()
    {
    return new string[] { "value1", "value2", "value3" };
    }
    ...

    关于asp.net-core - 如何从 ActionFilter 重定向到 Asp.net Core 2.2 中的另一个操作或路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55194463/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com