Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Security 为成员身份角色授权编辑Web.config_Security_Asp.net Membership_Web Config_Roles - Fatal编程技术网

Security 为成员身份角色授权编辑Web.config

Security 为成员身份角色授权编辑Web.config,security,asp.net-membership,web-config,roles,Security,Asp.net Membership,Web Config,Roles,我想通过web.config文件中的“授权”部分使用基于角色的安全性 使用成员资格,我的应用程序将允许创建新角色,因此,他们可以访问的页面需要动态设置 我是否可以通过编程方式更改web.config中的此部分来管理它?如果是,怎么做 using System.Configuration; using System.Web.Configuration; Configuration config = WebConfigurationManager.OpenWebConfiguration(Requ

我想通过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();
ASP.NET需要web.config写入权限才能使其工作,因此请小心

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()