2013-12-20 52 views
13

我正在構建一個公司環境內部使用的MVC4應用程序。我使用Windows身份驗證,這工作正常,但我有麻煩使用Active Directory組作爲授權角色。使用活動目錄角色提供者的授權MVC4

我的web.config看起來是這樣的:

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

當我使用用戶權限正常工作:

[Authorize(Users = @"DOMAIN\User1, DOMAIN\User2")] 
public ActionResult Create() 
{ 
    return View(); 
} 

但是當我使用的角色,它只是不要讓用戶組訪問此操作:

[Authorize(Roles = @"Domain\Group")] 
public ActionResult Create() 
{ 
    return View(); 
} 

我也嘗試指定無域的組,因爲我在其他閱讀但沒有運氣......我想我錯過了Web.config中的某些東西,但我不知道是什麼...

我避免使用自定義角色提供程序,因爲MVC4應該實現這沒有一個自定義角色提供者(或至少這就是我想的)

任何人都可以幫助我嗎?

提前致謝!

回答

16

我發現哪個是問題。閱讀了關於machine.config的一些信息here我檢查了我已經應用了正確的配置。

Fianlly我得到它的工作就像這樣:

[Authorize(Roles = "Domain\\Group")] 
public ActionResult Create() 
{ 
    return View(); 
} 

問題是我輸入組的方式。

我希望這可以幫助其他人。

+0

啊,雙反斜槓!我一直在爲此工作數小時 - 歡呼 – 2014-02-28 10:43:24

+1

@ D.Mac - 它的標準C#字符串格式化/轉義:「域\\組」或@「域\組」。 https://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx –

+3

是的,反彈不能是原因或語義差異,因爲它只是一個語法問題 – Mzn