2008-10-17 50 views
6

我在我的web.config以下:在我的codebehind類中,如何檢索授權角色?

<location path="RestrictedPage.aspx"> 
    <system.web> 
     <authorization> 
      <allow roles="Group1Admin, Group3Admin, Group7Admin"/> 
      <deny users="*"/> 
     </authorization> 
    </system.web> 
</location> 

在RestrictedPage.aspx.cs,我該如何找回允許的角色集合,其中包含Group1Admin,Group3Admin和Group7Admin?

這就是爲什麼我問:

web.config中被處理的授權頁面。這工作正常。但是我將有幾個這樣的頁面(比如說RestrictedPage.aspx,RestrictedPage2.aspx,RestrictedPage3.aspx)。這些頁面中的每一個都會有我的自定義webcontrol。每個頁面都有不同的角色。我的webcontrol有一個下拉列表。下拉菜單中的選項取決於用戶角色與頁面允許角色的交集。

如下所述,使用XPath搜索web.config可能會起作用。我只是希望有更多的框架。有點像SiteMap。當我將角色放入web.sitemap時,我可以使用SiteMap.CurrentNode.Roles(我的網站使用Windows身份驗證,因此我無法使用web.sitemap進行安全修整,我寧願只保留一個角色文件)。

回答

3
// set the configuration path to your config file 
string configPath = "??"; 

Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath); 

// Get the object related to the <identity> section. 
AuthorizationSection section = (AuthorizationSection)config.GetSection("system.web/authorization"); 

從部分對象獲得AuthorizationRuleCollection對象,你然後可以提取角色。

注意:從「location path =」RestrictedPage.aspx「」開始,您可能需要稍微修改該部分的路徑,但我沒有嘗試該方案。

+0

完美!這是我的最後一行: AuthorizationSection section =(AuthorizationSection)WebConfigurationManager.GetSection(「system.web/authorization」,Request.Path); Request.Path導航到location =「RestrictedPage.aspx」(當它是當前頁面)。 謝謝! – 2008-10-20 13:30:13

0
if {User.IsInRole("Group1Admin"){//do stuff} 

這就是你要求的嗎?

0

我不確定,但我會認爲這是在你的頁面被處理之前檢查過的,所以如果用戶不在角色中,他們永遠不會到達你的頁面。最終會在頁面中顯示冗餘。

+1

但是,如果用戶是一個授權角色,您可能仍然想知道他們在哪個角色,對吧? – DOK 2008-10-17 21:23:49

0

我確信有更好的方法來讀取這些信息,但這裏有一種方法可以從web.config文件讀取允許值。

XmlDocument webConfigReader = new XmlDocument(); 
webConfigReader.Load(Server.MapPath("web.config")); 

XmlNodeList root = webConfigReader.SelectNodes("//location[@path="RestrictedPage.aspx"]//allow//@roles"); 

foreach (XmlNode node in root) 
{ 
    Response.Write(node.Value); 
} 

當然,ASP.NET角色提供者將處理這個給你,所以讀取這些值是唯一真正相關的,如果你打算做他們的東西在後臺代碼授權用戶身邊,你可能正在做。

希望這會有所幫助 - 您可能必須使用,角色分割您的結果。

0

通常什麼情況是這樣的......

當用戶點擊你的頁面,如果認證/授權是有效的,則提高了Application_Authentication事件。除非您對Active Directory使用Windows身份驗證,否則IPrincipal和Identity對象將無法使用,因此您無法訪問User.IsInRole()方法。但是,您可以通過添加以下代碼到您的Global.asax文件做到這一點:

Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs) 

     Dim formsAuthTicket As FormsAuthenticationTicket 
     Dim httpCook As HttpCookie 
     Dim objGenericIdentity As GenericIdentity 
     Dim objMyAppPrincipal As CustomPrincipal 
     Dim strRoles As String() 

     Log.Info("Starting Application AuthenticateRequest Method...") 

     httpCook = Context.Request.Cookies.Get("authCookieEAF") 
     formsAuthTicket = FormsAuthentication.Decrypt(httpCook.Value) 
     objGenericIdentity = New GenericIdentity(formsAuthTicket.Name) 
     strRoles = formsAuthTicket.UserData.Split("|"c) 
     objMyAppPrincipal = New CustomPrincipal(objGenericIdentity, strRoles) 
     HttpContext.Current.User = objMyAppPrincipal 

     Log.Info("Application AuthenticateRequest Method Complete.") 

End Sub 

這將會把一個cookie與您可以在Web應用程序訪問正確的用戶和角色證書的瀏覽器會話。

理想情況下,您的用戶只會在應用程序中擔任一個角色,所以我相信這就是爲什麼您可以使用角色檢查方法。這將是很容易爲你寫一個輔助方法,將通過角色的應用程序和測試列表循環,看看他們是什麼樣的角色。