2012-04-11 23 views
0

我試圖隱藏我的主頁上的一些管理員特定的按鈕從沒有管理員的用戶。從我的主頁面調用我的HomeController中的方法

我的代碼看起來像這樣至今:

<% if (Request.IsAuthenticated) 
    {%> 
     <%: Html.ActionLink("Administrer", "Index", "User", new { Area = "Users" }, new { @class = "menubutton", @id = "settingsbutton" })%> 
     <%} else { }%> 

現在我想要做的IF()語句檢查如果當前用戶是管理員。即時通訊使用ASP.NET會員系統,併爲每個用戶提供一個特定類別,並提供一些關於他是否是管理員(布爾)的信息。

我的問題是我如何去調用一個方法來檢查這個或類似的東西?

+0

我在想這將是像if(Request.IsAuthenticated &&「檢查管理員的方法」) – AronChan 2012-04-11 18:36:35

+0

我只是不知道如何從主頁面調用這種方法到控制器 – AronChan 2012-04-11 18:37:54

+1

請參閱http:// stackoverflow。 com/questions/409213/how-can-i-create-a-view-that-has-different-displays-according-the-role-the-us或http://stackoverflow.com/questions/4610749/ asp-net-mvc-check-role-inside-view – 2012-04-11 18:48:22

回答

3
<% if (Request.IsAuthenticated && User.IsInRole("Administrator")) 
    {%> 
     <%: Html.ActionLink("Administrer", "Index", "User", new { Area = "Users" }, new { @class = "menubutton", @id = "settingsbutton" })%> 
     <%} else { }%> 

如果您使用的是ASP.NET成員提供程序和ASP.NET角色提供給用戶鏈接到自己的角色,你可以調用視圖的IPrincipal User對象的IsInRole(string)方法。

+0

看來我沒有IPrincipal用戶對象。我究竟該如何去做到這一點? – AronChan 2012-04-11 21:28:15

+0

我設法訪問我的用戶通過使用:HttpContext.Current.User.IsInRole(「管理員」) 這是像你的例子一樣好,或有一個特定的原因,你這樣做? – AronChan 2012-04-11 22:20:27

+0

'HttpContext.Current.User.IsInRole(「administrator」)'很好。也許它只是MVC3或Razor視圖引擎,默認在視圖頁面中提供@User。 – danludwig 2012-04-11 22:27:47

相關問題