4

我需要使用我的 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">

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

...但是没有效果。

非常感谢

4

4 回答 4

10

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

内部 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] 属性(或通过为您自己的方案继承它),因为它是保证不能通过备用路由访问资源的唯一方法。

于 2013-08-25T06:55:09.303 回答
1

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

于 2013-08-23T13:53:17.757 回答
0

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

<mvcSiteMapNode title="Rechnungen" controller="Customer/Bills" action="Index" roles="CompanyAdmin"/>
于 2013-11-22T12:45:23.060 回答
0

我只是把

 <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();
    }
 }

那么好吧!

于 2013-11-29T07:24:28.477 回答