【问题标题】:How implement security with the mvcSiteMapProvider?如何使用 mvcSiteMapProvider 实现安全性?
【发布时间】:2013-08-26 13:24:47
【问题描述】:

我需要使用我的 mvcSiteMapProvider V4 软件实现角色安全性。我将它与 MVC3 一起使用。

示例 mvcSiteMap 代码:

      <mvcSiteMapNode roles="Admin" title="Your Subscription (All Users)" controller="SOU" action="ListSubscribers">

此角色属性值无效:

      <mvcSiteMapNode roles="NoAdmin" title="Your Subscription (All Users)" controller="SOU" action="ListSubscribers">

这是一样的。如果管理员已登录,我希望上述内容不起作用?如果只有用户登录,我希望第一个示例能够工作。

...但是没有效果。

非常感谢

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 mvcsitemapprovider asp.net-mvc-sitemap


    【解决方案1】:

    默认情况下不启用安全修整。您需要做的第一件事就是打开它。

    内部 DI (web.config):

    <add key="MvcSiteMapProvider_SecurityTrimmingEnabled" value="true"/>
    

    外部 DI(在 MvcSiteMapProvider 模块中):

    bool securityTrimmingEnabled = true; // First line in the module
    

    然后您应该将 MVC [Authorize] 属性放在您想要保护的每个操作方法上。在MVC4+中,你也可以把它放在控制器级别,或者全局注册,然后使用[AllowAnonymous]属性选择性地允许非认证用户允许操作方法。

    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new AuthorizeAttribute());
        }
    }
    
    [Authorize(Roles="Admin,Manager")]
    public class MyController
    {
        // Everyone has access
        [AllowAnonymous]
        public ActionResult Index()
        {
            return View();
        }
    
        // Only Admin and Manager roles have access, everyone else is denied
        public ActionResult About()
        {
            return View();
        }
    }
    

    XML 中的角色属性是为了向后兼容 ASP.NET。对于 MVC,唯一真正的安全性是使用 [Authorize] 属性(或通过为您自己的方案继承它),因为它是保证资源不能通过备用路由访问的唯一方法。

    【讨论】:

    • 非常感谢
    • 我发现站点地图中的 Roles 属性对于指向外部资源的菜单链接很有用。它似乎与表单身份验证完美配合。
    • @MattiasÅslund - 我更正了我的帖子。角色属性适用于表单身份验证,因为它是为 ASP.NET 设计的。但是,ASP.NET 安全性基于文件系统和/或 URL,这对于 MVC 来说还不够好(一个资源可以从多个 URL 链接到,并且不一定涉及文件系统),所以你应该只使用与 ASP.NET 互操作性的角色属性。
    【解决方案2】:

    我只是放了

     <add key="MvcSiteMapProvider_SecurityTrimmingEnabled" value="true"/>
    

    在 Web.config 的 appSettings 中,像这样:

    <appSettings>
        <add key="webpages:Version" value="2.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="PreserveLoginUrl" value="true" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
        <add key="jqueryTheme" value="redmond" />
        <add key="MvcSiteMapProvider_IncludeAssembliesForScan" value="Cost3" />
        <add key="MvcSiteMapProvider_UseExternalDIContainer" value="false" />
        <add key="MvcSiteMapProvider_ScanAssembliesForSiteMapNodes" value="true" />
    
        <add key="MvcSiteMapProvider_SecurityTrimmingEnabled" value="true"/>
    
      </appSettings>
    

    并将 [Authorize] 属性放在每个控制器或操作上,如下所示:

    [Authorize(Roles = "Administrator")]
    public class UserManagementController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
     }
    

    那么好吧!

    【讨论】:

      【解决方案3】:

      如果您使用站点地图,您可以/必须(上述方法对我不起作用)在站点地图中指定角色。

      <mvcSiteMapNode title="Rechnungen" controller="Customer/Bills" action="Index" roles="CompanyAdmin"/>
      

      【讨论】:

      • 我不确定您从哪里获得了 controller="Customer/Bills" 语法,但它不受支持。如果您确实打算这样做,请使用 area="Customer" controller="Bills"。
      【解决方案4】:

      在 SOUController 上,您是否在某处添加了 [Authorize] 属性? MvcSiteMapProvider 使用那个来确定 ACL。

      【讨论】:

        猜你喜欢
        • 2012-01-15
        • 2011-05-25
        • 1970-01-01
        • 2011-02-15
        • 2016-08-23
        • 2014-07-06
        • 1970-01-01
        • 1970-01-01
        • 2011-10-05
        相关资源
        最近更新 更多