Web.config编辑以进行会员角色授权 [英] Web.config editing for Membership Role Authorization

查看:98
本文介绍了Web.config编辑以进行会员角色授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过web.config文件中的授权"部分为用户提供基于角色的安全性.

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.

我可以通过编程方式更改web.config中的此部分来进行管理吗?如果可以,怎么办?

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

推荐答案

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的写权限才能使其正常工作,因此请小心.

ASP.NET needs web.config write permission for this to work, so be careful.

这篇关于Web.config编辑以进行会员角色授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆