Web.config editing for Membership Role Authorization

☆樱花仙子☆ 提交于 2019-12-12 09:54:45

问题


I want to user Role based security through the authorization section in the web.config file.

Using Membership, my application will allow for new Roles to be created, and thus, the pages they can access need to be set dynamically.

Can I programatically alter this section in the web.config to manage this? If so, how?


回答1:


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 needs web.config write permission for this to work, so be careful.



来源:https://stackoverflow.com/questions/266188/web-config-editing-for-membership-role-authorization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!