如何从外部文件中读取XML appSettings?

时间:2017-06-21 21:01:08

标签: c# asp.net asp.net-mvc

有很多类似的问题,我看过每一个我能找到的问题,但没有用。

我将用于Google+身份验证的API密钥存储在我的解决方案之外的.config文件中(与解决方案文件夹位于同一级别)。

我正在尝试在Startup.Auth.cs中读取值,如下所示:

// Update is called once per frame
public void FixedUpdate ()
{
    if (Sex)
    {
        if (House == null && Money >= 1500)
        {
            GameObject Builder = GameObject.FindWithTag("Builder");
            int LuckyHouse = Random.Range(0,Builder.GetComponent<BuildPoints>().BuildPositions.Count);
            Builder.GetComponent<BuildPoints>().BuildPositions.RemoveAt(LuckyHouse);
            House = Builder.GetComponent<BuildPoints>().BuildPositions[LuckyHouse];
            Money = Money - 1500;
            GameObject HouseWall = House.transform.Find("Road_wall_Builder").gameObject;
            GameObject House1 = House.transform.Find("House_1").gameObject;
            HouseWall.active = false;
            House1.active = true;
        }
    }
}

Root Web.config:

public void ConfigureAuth(IAppBuilder app)
{
    app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
    {
        ClientId = WebConfigurationManager.AppSettings.Get("GoogleClientId"),
        ClientSecret = WebConfigurationManager.AppSettings.Get("GoogleClientSecret")
    });
}

Secrets.config:

<appSettings file="..\Secrets.config"> <!-- Path is correct, relative to Web.config -->
  <add key="webpages:Version" value="3.0.0.0" />
  <add key="webpages:Enabled" value="false" />
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

3 个答案:

答案 0 :(得分:2)

  

在我的解决方案之外的.config文件中(与解决方案文件夹处于同一级别)。

IIS应用程序完全不知道其虚拟应用程序文件夹之外的任何文件夹。没有&#34;与解决方案文件处于同一级别&#34;在Web应用程序的上下文中,因为解决方案文件未随其部署。

如果您想将appSettings放在应用程序文件夹之外,那么您唯一的内置选项是根web.config文件或machine.config文件,它们都是机器的全局文件(但特定于您正在运行的.NET框架版本)。请参阅ASP.NET Configuration File Hierarchy and Inheritance

  

但仅仅是为了记录,如果你在应用程序的web.config文件中保留应用程序设置,那么从长远来看这是最容易管理的。最后,您需要更改/添加一个新的Web服务器,并且您可能会试图弄清楚为什么设置在该时间到来时不再有效,如果它们需要放在虚拟应用程序文件夹之外

答案 1 :(得分:0)

R> set.seed(123) # be reproducible R> data <- data.frame(inp=Sys.Date() + cumsum(runif(10)*10)) R> data$ymd <- format(data$inp, "%Y%m%d") ## as yyyymmdd R> data$int <- as.integer(data$ymd) ## same as integer R> library(anytime) R> data$diff1 <- c(NA, diff(anydate(data$ymd))) # reads YMD R> data$diff2 <- c(NA, diff(anydate(data$int))) # also reads int R> data inp ymd int diff1 diff2 1 2017-06-23 20170623 20170623 NA NA 2 2017-07-01 20170701 20170701 8 8 3 2017-07-05 20170705 20170705 4 4 4 2017-07-14 20170714 20170714 9 9 5 2017-07-24 20170724 20170724 10 10 6 2017-07-24 20170724 20170724 0 0 7 2017-07-29 20170729 20170729 5 5 8 2017-08-07 20170807 20170807 9 9 9 2017-08-13 20170813 20170813 6 6 10 2017-08-17 20170817 20170817 4 4 R>

中删除<configuration>...</configuration>代码

Secrets.config:

Secrets.config

基本上,您将<appSettings> <add key="GoogleClientId" value="shh" /> <add key="GoogleClientSecret" value="shh" /> </appSettings> 部分设为外部appSettings文件的根。

使用Secrets.config属性的连接字符串也可以这样做,除了......

  

安全性 - 与configSource文件不同,外部连接   strings文件必须与root web.config位于同一目录中   文件,所以你必须采取预防措施,以确保你不检查它   进入你的源库。

也可以尝试使用Secrets.config

ConfigurationManager

引用我了解它的文章

Scott Hanselman

Best practices for private config data and connection strings in configuration in ASP.NET and Azure

官方MS文档

Best practices for deploying passwords and other sensitive data to ASP.NET and Azure App Service

答案 2 :(得分:0)

您是否尝试验证在构建之后是否复制了secrets.config?

通过右键单击文件并查看文件的构建操作设置为始终复制的属性来验证。

相关问题