我想通过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写权限才能工作,所以要小心.