質問

web.configファイルの承認セクションを介してロールベースのセキュリティを使用したい。

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

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

役に立ちましたか?

解決

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の書き込み権限が必要です。注意してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top