Vấn đề của tôi rất giống nhau, ngoại trừ tôi cần khả năng lặp qua tất cả các thư mục và thư mục con liên quan và hiển thị vai trò được phép cho từng trang web và thư mục thư mục. Tôi không thể sử dụng giải pháp của Ronald Wildenberg vì chúng tôi đang sử dụng .Net 2.0 nên chúng tôi không có chức năng LINQ.
Giải pháp của anh ấy đã cho tôi lộ trình tôi cần. Tôi cũng tìm thấy sự trợ giúp từ Đội hỗ trợ IIS của Microsoft, Managing Forms Authentication Programmatically. Tôi không muốn viết lại các tập tin cấu hình như chúng được đăng, thay vào đó chúng tôi cần khả năng hiển thị các vai trò được phép cho tất cả các thư mục và các trang trong ứng dụng của chúng tôi. Ứng dụng của chúng tôi là nhỏ. Nó có tổng cộng 15 thư mục và ít hơn 100 trang để điều này chạy khá nhanh chóng. Số dặm của bạn thay đổi tùy thuộc vào kích thước trang web của bạn.
Tôi bắt đầu từ thư mục gốc và tìm kiếm đệ quy tất cả các cấu hình web. Tôi đã thêm chúng với đường dẫn của chúng vào danh sách chuỗi sau đó được lặp qua danh sách và được gọi là hàm ListRoles của tôi. Chức năng này mở cấu hình web và nhận bộ sưu tập vị trí. Sau đó, nó tìm kiếm "system.web/ủy quyền" như Ronald đã làm. Nếu nó tìm thấy phần ủy quyền, nó sẽ lặp qua các quy tắc và loại trừ bất kỳ quy tắc thừa kế nào và tập trung vào AuthorizationRuleAction.Cho phép với vai trò liên quan:
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;
}
Nguồn
2011-02-21 22:25:54
Ông có thể đánh dấu một trong những câu trả lời dưới đây là đúng? –