gpt4 book ai didi

security - 成员角色授权的 Web.config 编辑

转载 作者:行者123 更新时间:2023-12-04 07:48:54 24 4
gpt4 key购买 nike

我想通过 web.config 文件中的授权部分使用基于角色的安全性。

使用 Membership,我的应用程序将允许创建新角色,因此需要动态设置他们可以访问的页面。

我可以通过编程方式更改 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 写权限才能工作,所以要小心。

关于security - 成员角色授权的 Web.config 编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/266188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com