2012-11-15 25 views
8

在我的ASP.NET MVC 4應用程序中,我使用Intranet模板來實現Windows身份驗證。我也在使用Fluent Security。如何在ASP.NET MVC 4中將域組用作流暢安全的角色?

開箱即用,我可以使用下面顯示的註釋來限制對控制器方法對特定域組或域用戶的訪問。

[Authorize([email protected]"Domain\GroupName")] 
public ActionResult Index() 
{ 
    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application."; 

    return View(); 
} 

[Authorize([email protected]"Domain\UserName")] 
public ActionResult About() 
{ 
    ViewBag.Message = "Your app description page."; 

    return View(); 
} 

如何將這兩種方法限制在使用Fluent Security的相同域組和域用戶?如果這更容易,我對用戶羣比用戶更感興趣。我是否需要制定自定義政策?如果是這樣,我不太清楚如何檢查經過身份驗證的用戶是否在域組中以便爲Fluent Security返回適當的角色以供使用?

我已經通過FluentSecurity入門,所以我知道如何實現FluentSecurity的基礎知識,我只是不確定如何使用域組作爲角色。

謝謝!

回答

3

我可能已經找到了一種方法來爲角色使用域組。我已經從Fluent Security入門頁面調整了擴展示例。

在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(@"Domain\Writers"); 
    configuration.For<BlogController>(x => x.AddComment()).DenyAnonymousAccess(); 
    configuration.For<BlogController>(x => x.DeleteComments()).RequireRole(@"Domain\Writers"); 
    configuration.For<BlogController>(x => x.PublishPosts()).RequireRole(@"Domain\Owners"); 

    // To authorize the Home Controller Index Action as in my original question 
    configuration.For<HomeController>(c => c.Index()).RequireRole(@"Domain\GroupName"); 
}); 

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> 

我還沒有找到授權單個用戶的一種方式,但我們都知道這是一般無論如何,最好的做法是使用團體。

有沒有更好的方法來做到這一點?