gpt4 book ai didi

asp.net-mvc-2 - 如何覆盖 ActionLink 行为

转载 作者:行者123 更新时间:2023-12-02 06:16:05 24 4
gpt4 key购买 nike

好的,我想通过 ActionLink 方法为我的网站添加一些安全性。如果用户有足够的权限来访问操作/ Controller ,则 ActionLink 应呈现该链接。如果不是,它应该返回一个空字符串。现在,ActionLink 是一个静态方法,这使得一切变得更加困难。有什么办法可以实现我想要做的事情吗?

最佳答案

新的AuthorizeActionLink扩展方法。根据需要重载。

public static MvcHtmlString AuthorizeActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
if (HasActionPermission(helper, actionName, controllerName))
return helper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);

return MvcHtmlString.Empty;
}

public static MvcHtmlString AuthorizeActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
if (HasActionPermission(helper, actionName, controllerName))
return helper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);

return MvcHtmlString.Empty;
}

在确定用户是否获得授权方面做肮脏工作的方法

static bool HasActionPermission(this HtmlHelper htmlHelper, string actionName, string controllerName)
{
ControllerBase controllerToLinkTo = string.IsNullOrEmpty(controllerName)
? htmlHelper.ViewContext.Controller
: GetControllerByName(htmlHelper, controllerName);

ControllerContext controllerContext = new ControllerContext(htmlHelper.ViewContext.RequestContext, controllerToLinkTo);
ReflectedControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(controllerToLinkTo.GetType());
ActionDescriptor actionDescriptor = controllerDescriptor.FindAction(controllerContext, actionName);

return ActionIsAuthorized(controllerContext, actionDescriptor);
}

static bool ActionIsAuthorized(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
if (actionDescriptor == null)
return false;

AuthorizationContext authContext = new AuthorizationContext(controllerContext, actionDescriptor);
foreach (IAuthorizationFilter authFilter in actionDescriptor.GetFilters().AuthorizationFilters)
{
authFilter.OnAuthorization(authContext);

if (authContext.Result != null)
return false;
}

return true;
}

static ControllerBase GetControllerByName(HtmlHelper helper, string controllerName)
{
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();

IController controller = factory.CreateController(helper.ViewContext.RequestContext, controllerName);

if (controller == null)
{
throw new InvalidOperationException(
string.Format(
CultureInfo.CurrentUICulture,
"Controller factory {0} controller {1} returned null",
factory.GetType(),
controllerName));
}

return (ControllerBase)controller;
}

关于asp.net-mvc-2 - 如何覆盖 ActionLink 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5477019/

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