简体   繁体   English

我可以将与ASP.NET身份验证相关的配置移出web.config吗

[英]Can I move asp.net authentication related configuration out of web.config

I have an asp.net WebApi application where I would like to move any configuration that is likely to change out of web.config into an external configuration file. 我有一个asp.net WebApi应用程序,我想在其中将任何可能从web.config更改为外部配置文件的配置都移到该文件中。

This will then allow an install update to overwrite the web.config so that it picks up any newer configuration added between version, but preserve other user settings which may vary between deployments. 然后,这将允许安装更新覆盖web.config,以便它选择版本之间添加的所有较新配置,但保留其他用户设置,这些用户设置可能因部署而异。

I have successfully done this with a few sections, eg appSettings . 我已经通过几个部分成功地做到了这一点,例如appSettings

For appSettings, I have the folliwing in web.config... 对于appSettings,我在web.config中有个愚蠢的问题。

<appSettings configSource="config\appSettings.config"/>

And then the external file has the various settings, eg ... 然后,外部文件具有各种设置,例如...

    <?xml version="1.0" encoding="utf-8"?>
    <appSettings>               
        <add key="IISSitePrefix" value="http" />            
        <!--- Set this to True to emit http request debug information to the Event log  -->
        <add key="EnableHttpDebugTracing" value="false" />

        .... etc

I have been trying to do the same with the two configuration settings we need to change to toggle on/off windows authentication, as some deployments will use this, and others will use token based security. 我一直在尝试对我们需要更改的两个配置设置进行相同的操作,以打开/关闭Windows身份验证,因为某些部署将使用此配置,而其他部署将使用基于令牌的安全性。 To do this I need to move the following out of web.config... 为此,我需要将以下内容从web.config中移出...

    <authentication>
        <windowsAuthentication enabled="true" />
     </authentication>

     <system.web>   
       <authentication mode="Windows"/>
     </system.web>

So for the first tag, I tried the following.... 因此,对于第一个标签,我尝试了以下操作。

<authentication configSource="config\authentication.config"/>

with the contents of the external file being.. 与外部文件的内容是..

    <?xml version="1.0" encoding="utf-8"?>

    <authentication>
      <windowsAuthentication enabled="true" />
    </authentication>

However, when I run this, I get the following error... 但是,当我运行此命令时,出现以下错误...

The configuration section 'authentication' cannot be read because it is missing a section declaration 

I get a similar result when I try the other section. 当我尝试其他部分时,也会得到类似的结果。

Does anyone know what this means, or even if it is possible to do the above? 有谁知道这意味着什么,或者即使有可能做到以上几点?

Thanks in advance for any help 预先感谢您的任何帮助

[EDIT 1] [编辑1]

After one of the comments I have realised, perhaps my configuration is not quite correct (it was some time ago I first looked at this, and am now revisiting) 在我意识到其中的一条评论之后,也许我的配置不太正确(前一段时间我第一次查看了此信息,现在正在重访)

Previously, to enable integrated (windows) authentication, I thought you needed two bits of configuration (system.web AND system.webserver)... 以前,要启用集成(Windows)身份验证,我认为您需要进行两点配置(system.web和system.webserver)...

     <system.web>
        <authentication mode="Windows"/>
     </system.web>

      <system.webServer>   
        <security>
          <authentication>
            <windowsAuthentication enabled="true" />
          </authentication>
        </security>
      </system.webServer>

BUT now looking at this post , it appears I only need the <system.webServer> and not <system.web> at all 但是现在看这篇文章 ,看来我只需要<system.webServer>而不是<system.web>

I removed my <system.web> and I could indeed turn off the intergarted authentication using just the <system.webServer> section. 我删除了<system.web> ,确实可以仅使用<system.webServer>部分来关闭交互验证。

So, now, what I want to configure in the external file is just the following.. 因此,现在,我要在外部文件中配置的只是以下内容。

在此处输入图片说明

ie if possible I'd like to just move out the <security> section, and leave the rest of the <system.webServer> in web.config. 即,如果可能的话,我只想移出<security>部分,并将<system.webServer>的其余部分留在web.config中。

I tried the following .. 我尝试了以下..

 <system.webServer>
   <security configSource ="config\authentication.config"/>     
 </system.webServer>

With the contents of authentication.config being... 随着authentication.config的内容...

   <?xml version="1.0" encoding="utf-8"?>
    <security>
      <authentication>
        <windowsAuthentication enabled="true" />
      </authentication>
    </security>

But now when I try to execute a route I get.. 但是现在当我尝试执行一条路线时,我得到了..

Unrecognized attribute 'configSource'

Config Source:
 87: 
 88:     <security configSource ="config\authentication.config"/>
 89:      

So my (modified) question becomes is there a way to move out the above section? 所以我的(修改过的)问题变成了有没有办法摆脱以上部分?

Try changing the external file from 尝试从更改外部文件

<?xml version="1.0" encoding="utf-8"?>
    <authentication>
      <windowsAuthentication enabled="true" />
    </authentication>

to: 至:

<?xml version="1.0" encoding="utf-8"?>
    <authentication mode="Windows"></authentication>

It works for me ;) 这个对我有用 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM