gpt4 book ai didi

asp.net-core - ASP.NET Core 2.0 从 AuthorizationHandler、HandleRequirementAsync 方法重定向用户

转载 作者:行者123 更新时间:2023-12-03 18:29:22 33 4
gpt4 key购买 nike

我正在尝试在 .net core 2.0 中实现 AuthorizationHandler 在那里我需要授权用户并根据条件想要重定向到我的应用程序验证中的不同操作方法工作正常但是我如何将用户重定向到拒绝访问或登录页面时授权失败。

 protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasPermissionRequirement requirement)
{
var controllerContext = context.Resource as AuthorizationFilterContext;

if (sessionManager.Session.sysUserID <= 0)
{
controllerContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Account", action = "Login", area = "" }));

return Task.FromResult(0);
}


if (Utilities.GetInt32Negative(PermissionID) == 1 || Utilities.GetInt32Negative(PermissionID) == -1)
{
if (!PagePath.Equals("~/"))
controllerContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Home", action = "NoAccess", area = "" }));
}

context.Succeed(requirement);
}
else
{
if (!PagePath.Equals("~/"))
controllerContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Home", action = "NoAccess", area = "" }));
}

return Task.FromResult(0);
}

最佳答案

我找到了解决方案,我希望这能帮助寻找类似的人,在自定义授权中,我们可以使用 重定向到任何所需的 Controller 操作。 AuthorizationFilterContext RedirectToActionResult

protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasPermissionRequirement requirement)
{
// Get the context
var redirectContext = context.Resource as AuthorizationFilterContext;
//check the condition
if (!result)
{
redirectContext.Result = new RedirectToActionResult("AccessDenied", "Home", null);
context.Succeed(requirement);
return Task.CompletedTask;
}
context.Succeed(requirement);
return Task.CompletedTask;
}

关于asp.net-core - ASP.NET Core 2.0 从 AuthorizationHandler、HandleRequirementAsync 方法重定向用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46089315/

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