3

web.config ファイルの認証セクションを使用して、ロール ベースのセキュリティを使用したいと考えています。

メンバーシップを使用すると、アプリケーションで新しいロールを作成できるようになるため、ロールがアクセスできるページを動的に設定する必要があります。

これを管理するために、web.config のこのセクションをプログラムで変更できますか? もしそうなら、どのように?

4

1 に答える 1

5
using System.Configuration;
using System.Web.Configuration;

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
AuthorizationSection section = (AuthorizationSection)config.GetSection("system.web/authorization");

AuthorizationRule rule = new AuthorizationRule(AuthorizationRuleAction.Allow);
rule.Roles.Add("admins");
section.Rules.Add(rule);

config.Save();
Imports System.Configuration
Imports System.Web.Configuration

Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
Dim section As AuthorizationSection = CType(config.GetSection("system.web/authorization"), AuthorizationSection)

Dim rule As New AuthorizationRule(AuthorizationRuleAction.Allow)
rule.Roles.Add("admins")
section.Rules.Add(rule)

config.Save()

これを機能させるには、ASP.NET が web.config の書き込み権限を必要とするため、注意が必要です。

于 2008-11-05T22:31:35.347 に答える