config文件的授权部分

config文件的授权部分

本文介绍了以编程方式更新asp.net web.config文件的授权部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您正在尝试在运行时修改web.config文件。我的应用使用Windows身份验证。我写的代码在我的本地系统上工作正常。但是当我在服务器上的IIS中托管应用程序时,当我尝试在运行时修改web.config文件时,应用程序会抛出错误。

下面是用于修改web的代码.config文件。

Hi am trying to modify the web.config file on run time. My application uses windows authentication. The code which I have written is working fine on my local system. But when i host the application in the IIS on the server, application is throwing an error, when I try to modify web.config file on run time.
below is the code used to modify the the web.config file.

protected void btnAdd_Click(object sender, EventArgs e)
   {
       AuthorizationSection section;
       System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);

       section = (AuthorizationSection)config.GetSection("system.web/authorization");

       AuthorizationRule arule = new AuthorizationRule(AuthorizationRuleAction.Allow);

       string[] strAllowedUsers = GetUsers();


       section.Rules.Clear();

       foreach (string element in strAllowedUsers)
       {
           arule.Users.Add(element);
       }
       arule.Users.Add(txtAddUser.Text.ToString());
       section.Rules.Add(arule);



       config.Save();

   }

private string[] GetUsers()
   {
       string[] strUser = null;
       try
       {
           AuthorizationSection section;
           System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
           section = (AuthorizationSection)config.GetSection("system.web/authorization");

           var rules = section.Rules;
           var allowedUsers = rules
             .OfType<authorizationrule>()
             .Where(r => r.Action == AuthorizationRuleAction.Allow)
             .Select(r => r.Users).First();
           if (allowedUsers.ToString() != null)
               strUser = allowedUsers.ToString().Split(',');
       }
       catch { }
       return strUser;
   }



它会抛出错误 - >


It throws an error -->

Unable to save config to file 'c:\inetpub\wwwroot\myapplication\web.config





注意:我在IIS 5.1上托管应用程序



Note: I am hosting the application on IIS 5.1

推荐答案


这篇关于以编程方式更新asp.net web.config文件的授权部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:09