Il mio problema era molto simile tranne che avevo bisogno della possibilità di scorrere tutte le directory e le sottodirectory correlate e visualizzare i ruoli consentiti per ogni pagina Web e directory di cartelle. Non ero in grado di utilizzare la soluzione di Ronald Wildenberg perché stiamo usando .Net 2.0, quindi non abbiamo la funzionalità Linq.
La sua soluzione mi ha dato la tabella di marcia di cui avevo bisogno. Ho anche trovato aiuto dal team di supporto IIS francese di Microsoft, Managing Forms Authentication Programmatically. Non volevo riscrivere i file di configurazione come quelli pubblicati, ma avevamo bisogno della possibilità di mostrare i ruoli consentiti per tutte le directory e le pagine nella nostra applicazione. La nostra applicazione è piccola. Ha un totale di 15 directory e meno di 100 pagine, quindi funziona molto rapidamente. Il tuo chilometraggio varia a seconda delle dimensioni del tuo sito web.
Sono partito dalla directory principale e ho cercato ricorsivamente tutti i webconfigs. Li ho aggiunti con il loro percorso a una lista di stringhe, poi ripetuto l'elenco e chiamato la funzione ListRoles. Questa funzione apre la configurazione Web e ottiene la raccolta della posizione. Quindi cerca "system.web/authorization" come ha fatto Ronald. Se trova una sezione di autorizzazione, scorre le regole ed esclude qualsiasi regola ereditata e si concentra su AuthorizationRuleAction.Consenti con ruoli associati:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Web.Configuration;
public void DisplayWebPageRoles()
{
//First walk the directories and find folders with Web.config files.
//Start at the root
DirectoryInfo baseDir = new DirectoryInfo(Server.MapPath("~/"));
//Do a little recursion to find Web.Configs search directory and subdirs
List<string> dirs = DirectoriesWithWebConfigFile(baseDir);
//Replace the folder path separator except for the baseDir
for (int i = 0; i < dirs.Count; i++)
{
dirs[i] = dirs[i].Replace(
baseDir.FullName.Replace("\\", "/"),
"/" + baseDir.Name + (i > 0 ? "/" : ""));
}
//Now that we have the directories, we open the Web.configs we
//found and find allowed roles for locations and web pages.
for (int i = 0; i < dirs.Count; i++)
{
//Display on page, save to DB, etc...
ListRoles(dirs[i]);
}
}
public List<string> DirectoriesWithWebConfigFile(DirectoryInfo directory)
{
List<string> dirs = new List<string>();
foreach (FileInfo file in directory.GetFiles("Web.config"))
{
dirs.Add(directory.FullName.Replace("\\","/"));
}
foreach (DirectoryInfo dir in directory.GetDirectories())
{
dirs.AddRange(DirectoriesWithWebConfigFile(dir));
}
return dirs;
}
private void ListRoles(string configFilePath)
{
System.Configuration.Configuration configuration =
WebConfigurationManager.OpenWebConfiguration(configFilePath);
//Get location entries in web.config file
ConfigurationLocationCollection locCollection = configuration.Locations;
string locPath = string.Empty;
foreach (ConfigurationLocation loc in locCollection)
{
try
{
Configuration config = loc.OpenConfiguration();
//Get the location path so we know if the allowed roles are
//assigned to a folder location or a web page.
locPath = loc.Path;
if (locPath.EndsWith(".js")) //Exclude Javascript libraries
{
continue;
}
AuthorizationSection authSection =
(AuthorizationSection)config
.GetSection("system.web/authorization");
if (authSection != null)
{
foreach (AuthorizationRule ar in authSection.Rules)
{
if (IsRuleInherited(ar))
{
continue;
}
if (ar.Action == AuthorizationRuleAction.Allow
&& ar.Roles != null
&& ar.Roles.Count > 0)
{
for (int x = 0; x < ar.Roles.Count; x++)
{
//Display on page, save to DB, etc...
//Testing
//Response.Write(
// configFilePath + "/web.config" + ","
// + configFilePath + "/" + locPath + ","
// + ar.Roles[x] + "<br />");
}
}
}
}
}
catch (Exception ex)
{
//Your Error Handling Code...
}
}
}
From French IIS support Team blog
private bool IsRuleInherited(AuthorizationRule rule)
{
//to see if an access rule is inherited from the web.config above
//the current one in the hierarchy, we look at two PropertyInformation
//objects - one corresponding to roles and one corresponding to
//users
PropertyInformation usersProperty = rule.ElementInformation.Properties["users"];
PropertyInformation rolesProperty = rule.ElementInformation.Properties["roles"];
//only one of these properties will be non null. If the property
//is equal to PropertyValueOrigin.Inherited, the this access rule
//if not returned in this web.config
if (usersProperty != null)
{
if (usersProperty.ValueOrigin == PropertyValueOrigin.Inherited)
return true;
}
if (rolesProperty != null)
{
if (rolesProperty.ValueOrigin == PropertyValueOrigin.Inherited)
return true;
}
return false;
}
fonte
2011-02-21 22:25:54
Potrebbe segnare una delle risposte al di sotto come corrette? –