简体   繁体   English

通过global.asax从外部类获取web.config请求

[英]Getting the web.config request from external class via global.asax

My web-application uses an external class library for certain procedures that I use in many places. 我的Web应用程序将外部类库用于我在许多地方使用的某些过程。 One of the things I'd like to add to my library is this configurator class to allow me to encrypt parts of my web.config file. 我想添加到库中的一件事是此配置程序类,该类允许我加密web.config文件的某些部分。

Now, I am calling the class from global.asax , it compiles, and intellisense doesn't have any issues, but am getting this error upon execution of the web-application: 现在,我从global.asax调用该类,该类已编译,并且intellisense没有任何问题,但是在执行Web应用程序时遇到此错误:

Request is not available in this context 在这种情况下请求不可用

How do I fix this? 我该如何解决?

public class configurator {
private Configuration _webconfig;
public const string DPAPI = "DataProtectionConfigurationProvider";

public Configuration webconfig {
    get { return _webconfig; } 
    set { _webconfig = webconfig; } 
}

public configurator() {
    webconfig = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
}

public void ProtectSection(string sectionName, string provider = DPAPI) {
    ConfigurationSection section = webconfig.GetSection(sectionName);

    if (section != null && !section.SectionInformation.IsProtected) {
        section.SectionInformation.ProtectSection(provider);
        webconfig.Save();
    }
}

public void EncryptConnString(string protectionMode) {
    ConfigurationSection section = webconfig.GetSection("connectionStrings");
    section.SectionInformation.ProtectSection(protectionMode);
    webconfig.Save();
}

public void DecryptConnString() {
    ConfigurationSection section = webconfig.GetSection("connectionStrings");
    section.SectionInformation.UnprotectSection();
    webconfig.Save();
}
}

The class is being called first thing in the global.asax (sorry for the mix; I prefer c#, but started the other project in vb before I started with c#!) : 该类在global.asax中被称为第一件事(对不起,混合;我更喜欢c#,但是在我开始使用c#之前在vb中启动了另一个项目!)

<%@ Application Language="VB" %>
<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs on application startup - this will encrypt the web.config
    Dim thisconfigurator As mydll.configurator = New orsus.configurator()
    If ConfigurationManager.AppSettings("con") = "production" Then
        thisconfigurator.ProtectSection("AppSettings")
        thisconfigurator.ProtectSection("connectionStrings")
        thisconfigurator.ProtectSection("system.net/mailSettings/smtp")
    End If
End Sub
</script>

David Hoerster was right, Request hasn't been initialized yet, so it will error out. David Hoerster是对的, Request尚未初始化,所以它会出错。 If you only need to access the root config, this works: 如果只需要访问根配置,则可以使用:

webconfig = WebConfigurationManager.OpenWebConfiguration("~");

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

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