gpt4 book ai didi

asp.net-mvc - “Security aware” Action 链接?

转载 作者:行者123 更新时间:2023-12-03 11:50:18 24 4
gpt4 key购买 nike

如何创建一个“安全意识”操作链接,以检测用户是否有权单击(调用)该操作?
如果不允许用户使用该操作,则隐藏链接...

取决于

  • web.config(授权)和
  • [授权]操作
  • 上的属性

    聚苯乙烯
    我猜这是不好的做法,将这两个MVC混合在一起?

    最佳答案

    这是从MvcSitemap项目中窃取的一些代码,并已修改为我自己使用。如果我没记错的话,此代码已针对MVC2进行了修改,并且某些功能可能必须反向移植到MVC1。

    将MVC和FormsAuthentication混合在一起完全不是坏习惯,MVC的默认身份验证方法围绕现有的Asp.net安全基础结构构建。

    用于确定用户是否具有权限的代码:

    public static class SecurityTrimmingExtensions 
    {

    public static bool HasActionPermission( this HtmlHelper htmlHelper, string actionName, string controllerName )
    {
    //if the controller name is empty the ASP.NET convention is:
    //"we are linking to a different controller
    ControllerBase controllerToLinkTo = string.IsNullOrEmpty(controllerName)
    ? htmlHelper.ViewContext.Controller
    : GetControllerByName(htmlHelper, controllerName);

    var controllerContext = new ControllerContext(htmlHelper.ViewContext.RequestContext, controllerToLinkTo);

    var controllerDescriptor = new ReflectedControllerDescriptor(controllerToLinkTo.GetType());

    var actionDescriptor = controllerDescriptor.FindAction(controllerContext, actionName);

    return ActionIsAuthorized(controllerContext, actionDescriptor);
    }


    private static bool ActionIsAuthorized(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
    if (actionDescriptor == null)
    return false; // action does not exist so say yes - should we authorise this?!

    AuthorizationContext authContext = new AuthorizationContext(controllerContext);

    // run each auth filter until on fails
    // performance could be improved by some caching
    foreach (IAuthorizationFilter authFilter in actionDescriptor.GetFilters().AuthorizationFilters)
    {
    authFilter.OnAuthorization(authContext);

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

    return true;
    }

    private static ControllerBase GetControllerByName(HtmlHelper helper, string controllerName)
    {
    // Instantiate the controller and call Execute
    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;
    }

    }

    HTML帮助程序
    public static class SecurityTrimmedLink
    {
    public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkName, string actionName)
    {
    return htmlHelper.HasActionPermission(actionName, "")
    ? htmlHelper.ActionLink(linkName, actionName)
    : MvcHtmlString.Create("");
    }

    public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkName, string actionName, RouteValueDictionary routeValueDictionary )
    {
    return htmlHelper.HasActionPermission(actionName, "")
    ? htmlHelper.ActionLink(linkName, actionName, routeValueDictionary)
    : MvcHtmlString.Create("");
    }

    public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkName, string actionName, object routeValues, object htmlAttributes )
    {
    return htmlHelper.HasActionPermission(actionName, "")
    ? htmlHelper.ActionLink(linkName, actionName, routeValues, htmlAttributes)
    : MvcHtmlString.Create("");
    }

    public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkName, string actionName, string controllerName)
    {
    return htmlHelper.HasActionPermission(actionName, controllerName)
    ? htmlHelper.ActionLink(linkName, actionName, controllerName)
    : MvcHtmlString.Create("");
    }

    public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkName, string actionName, string controllerName, object routeValues, object htmlAttributes)
    {
    return htmlHelper.HasActionPermission(actionName, controllerName)
    ? htmlHelper.ActionLink(linkName, actionName, controllerName, routeValues, htmlAttributes)
    : MvcHtmlString.Create("");
    }
    }

    警告:这在MVC 5中不起作用,因为对FindAction()的调用从不返回操作描述符

    我试图找到问题,但是找不到并最终编写了解决方案。 :(

    关于asp.net-mvc - “Security aware” Action 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2721869/

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