如何在 MVC 4 中使用域组作为具有 Fluent 安全性的角色 ASP.NET



在我的 ASP.NET MVC 4应用程序中,我正在使用内部网模板来实现Windows身份验证。我也在使用Fluent Security。

开箱即用,我可以使用下面显示的注释来限制特定域组或域用户对控制器方法的访问。

[Authorize(Roles=@"DomainGroupName")]
public ActionResult Index()
{
    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
    return View();
}
[Authorize(Users=@"DomainUserName")]
public ActionResult About()
{
    ViewBag.Message = "Your app description page.";
    return View();
}

如何使用 Fluent 安全性将这两种方法限制为同一域组和域用户?如果这更容易的话,我对组比对用户更感兴趣。是否需要构建自定义策略?如果是这样,我不太确定如何检查经过身份验证的用户是否在域组中以返回正确的角色供 Fluent Security 使用?

我已经完成了FluentSecurity

入门,所以我确实知道如何实现FluentSecurity的基础知识,我只是不确定如何使用域组作为角色。

谢谢!

我可能已经找到了一种将域组用于角色的方法。我调整了 Fluent 安全入门页面中的扩展示例。

在 Global.asax 中.cs:

SecurityConfigurator.Configure(configuration =>
{
    // Let Fluent Security know how to get the authentication status of the current user
    configuration.GetAuthenticationStatusFrom(() => HttpContext.Current.User.Identity.IsAuthenticated);
    // Let Fluent Security know how to get the roles for the current user
    configuration.GetRolesFrom(System.Web.Security.Roles.GetRolesForUser);
    // This is where you set up the policies you want Fluent Security to enforce
    configuration.For<HomeController>().Ignore();
    configuration.For<AccountController>().DenyAuthenticatedAccess();
    configuration.For<AccountController>(x => x.ChangePassword()).DenyAnonymousAccess();
    configuration.For<AccountController>(x => x.LogOff()).DenyAnonymousAccess();
    configuration.For<BlogController>(x => x.Index()).Ignore();
    configuration.For<BlogController>(x => x.AddPost()).RequireRole(@"DomainWriters");
    configuration.For<BlogController>(x => x.AddComment()).DenyAnonymousAccess();
    configuration.For<BlogController>(x => x.DeleteComments()).RequireRole(@"DomainWriters");
    configuration.For<BlogController>(x => x.PublishPosts()).RequireRole(@"DomainOwners");
    // To authorize the Home Controller Index Action as in my original question
    configuration.For<HomeController>(c => c.Index()).RequireRole(@"DomainGroupName");
});
GlobalFilters.Filters.Add(new HandleSecurityAttribute(), 0);

在 Web.config 中:

<authentication mode="Windows" />
<authorization>
  <deny users="?" />
</authorization>
<roleManager defaultProvider="WindowsProvider"
      enabled="true"
      cacheRolesInCookie="false">
  <providers>
    <add
      name="WindowsProvider"
      type="System.Web.Security.WindowsTokenRoleProvider" />
  </providers>
</roleManager>

我还没有找到授权单个用户的方法,但我们都知道无论如何使用组通常是最佳实践。

有没有更好的方法可以做到这一点?

最新更新