2012-11-30 23 views
3

您好我有一個問題,當使用網頁表單,我有一個母版頁包含我的導航,我想問是否有可能後我登錄,我的導航巴可在不同的角色(管理員,學生,員工)來定製基地。隱藏導航欄在母版頁取決於我的用戶角色

 <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" > 
     <nav> 
      <ul id="menu" style="text-align:center;"> 
       <li><a href="~/">Moderator</a></li> 
       <li><a href="~/About.aspx">Supervisor</a></li> 
       <li><a href="~/Contact.aspx">Student</a></li> 
      </ul> 
     </nav> 
    </asp:ContentPlaceHolder> 

所以這是我的SITEMASTER的ContentPlaceHolder的樣本,我的想法是隱藏主持人和監督員當我與學生登錄,我希望使用檢查角色「學生」,那麼主持人和主管的Sitemaster導航欄可以隱藏用戶,請告訴我那裏有更好的解決方案,因爲這只是我知道的..謝謝..

+0

,你可以設置一個枚舉並在頁面加載根據用戶屬於哪個組或ACCESSLEVEL你可以設置的可見性,我已經做了這個MenuItems多次之前在過去..我會建議這樣做與MenuItems本人 – MethodMan

+0

謝謝,我會嘗試研究枚舉.. – willie

+0

我會張貼一個我正在談論的例子about和on Page_Load事件是代碼去MasterPage.cs的地方 – MethodMan

回答

4

針對您的問題,我有改變你的HTML代碼都是如下

<asp:ContentPlaceHolder runat="server" ID="FeaturedContent" > 
     <nav> 
      <ul id="menu" style="text-align:center;"> 
       <li id="li_moderator" runat="server"><a href="~/">Moderator</a></li> 
       <li id="li_supervisor" runat="server"><a href="~/About.aspx">Supervisor</a></li> 
       <li><a href="~/Contact.aspx">Student</a></li> 
      </ul> 
     </nav> 
    </asp:ContentPlaceHolder> 

比登錄驗證和 學生,您可以隱藏或顯示菜單的權利後,烏爾服務器端的編碼。使用下面的代碼

li_moderator.style.add("display","none") 
    li_supervisor.style.add("display","none") 

同樣的事情,如果你想在其他情況下,顯示出比使用

li_moderator.style.add("display","inherit") 
    li_supervisor.style.add("display","inherit") 
1

設置我的枚舉這樣的..

public enum AccessRights 
{ 
    LogisticsCoordinator_RW, //Read-Write, 
    LogisticsCoordinator_R, //Read only Purchasing and Inventory 
    ProcurementManager_RW, // Read-Write access to track purchase of Sand on monitor on hand Inventory 
    ProcurementManager_R, //read access to Create Frac Jobs , Dispatch and reroute 
    SystemAdministrator, //Full Rights to Vertex_Personnel only 
    StudentManagement_R, //Read Access Only 
    AppAdministrator_R, 
    NonAdmin, 
    None, 
    Full, 
    Default 
} 

現在我用LDAP/ActiveDirectory中,以確定用戶的訪問權限,你可以使用SQL Server或檢查用戶權限

一些其他手段這只是代碼的一個例子,我現在不會粘貼整個代碼,但你應該明白我的意思

_ADPath = ConfigurationManager.AppSettings["ADPath"]; 
_Domain = ConfigurationManager.AppSettings["ISDDomain"]; 
_UserId = ((BasePage)Page).CurrentUser; 
string[] strUser = _UserId.Split('\\'); 
if (strUser.Length == 2) 
{ 
    _UserId = strUser[1]; 
} 

// uxLBLoginError.Text = ""; 
try 
{ 
    LdapAuthentication la = new LdapAuthentication(_ADPath); 

    if (!AdPassRequired) 
    { 
     //use this if password not required 
     _authenticated = la.IsUserGroupMember(_UserId, AdGroupToPass); 
    } 
    //else 
    //{ 
    // // use this if password is required 
    // _authenticated = la.IsAuthenticated(_Domain, _UserId, strPassword); 
    //} 
    if (_authenticated) 
    { 
     //test the roles functionality 
     ((BasePage)Page).GetDBRoleNames(_UserId); 
     Session["User_Initial_Validated"] = true; 
     Session["isDefault_Loaded"] = true; 
     Session["AccessRights"] = AccessRights.Default; 
     Session["UserId"] = _UserId; 
     //Response.Redirect("~/Default.aspx"); 
    } 
3

嘗試表明這:

<asp:ContentPlaceHolder runat="server" ID="FeaturedContent"> 
      <nav> 
       <ul id="menu" style="text-align: center;"> 
        <li id="menuModerator" runat="server"><a href="~/">Moderator</a></li> 
        <li id="menuSupervisor" runat="server"><a href="~/About.aspx">Supervisor</a></li> 
        <li id="menuStudent" runat="server"><a href="~/Contact.aspx">Student</a></li> 
       </ul> 
      </nav> 
</asp:ContentPlaceHolder> 

在裏面的Page_Load Site.Maste.cs:

if (!Page.User.IsInRole("Moderator")) 
    { 
     menuModerator.Visible = false; 
    } 

對於域組Page.User.IsInRole(@ 「域名\組名」)

+0

哇感謝您的解決方案!另一個問題,對不起很新手..當我點擊另一個頁面(例如:我點擊menuStudent然後它鏈接到Contact.aspx,但問題,當我嘗試點擊任何菜單,它將我鏈接到錯誤的頁面,我可以知道任何好的解決方案來替換〜/ Contact.aspx? – willie

+0

如果我的答案有幫助,請將其標記爲正確。關於第二個問題,對不起,不能得到什麼問題。 – dkrzh