编写一个应用程序来加密/解密各种 web.config (vb.net)

发布于 2024-10-18 23:24:04 字数 1446 浏览 4 评论 0原文

我正在尝试编写一个应用程序来加密和解密已选择的 web.config。

我找到了一些代码可以在应用程序中执行此操作,如下所示...

导入系统 导入系统配置 导入 System.Web.Configuration

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 处理 Me.Load

    'encrypt/decrypt identity
    Dim config As Configuration
    Dim configSection As ConfigurationSection

    'encrypt identity
    config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
    If Not (config Is Nothing) Then
        configSection = config.GetSection("system.web/identity")
        If Not (configSection Is Nothing) Then
            If Not (configSection.SectionInformation.IsLocked) Then
                configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")
                config.Save()
            End If
        End If
    End If

    'decrypt identity
    config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
    configSection = config.GetSection("system.web/identity")
    If Not (configSection Is Nothing) Then
        If Not (configSection.SectionInformation.IsLocked) Then
            configSection.SectionInformation.UnprotectSection()
            config.Save()
        End If
    End If

我想做的是将其放入带有几个按钮(btnEncrypt 和 btndecrypt)的 win 表单应用程序中浏览控件(浏览到各种 web.configs)。

然后我们的操作人员可以使用该应用程序,这样他们就可以加密或解密我们所有 Web 应用程序中的所有 web.configs,而无需重新发布等...

任何帮助将不胜感激。

谢谢

I'm trying to write an application that will encrypt and decrypt a web.config which has been selected.

I have found some code to do this within an application like so...

Imports System
Imports System.Configuration
Imports System.Web.Configuration

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'encrypt/decrypt identity
    Dim config As Configuration
    Dim configSection As ConfigurationSection

    'encrypt identity
    config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
    If Not (config Is Nothing) Then
        configSection = config.GetSection("system.web/identity")
        If Not (configSection Is Nothing) Then
            If Not (configSection.SectionInformation.IsLocked) Then
                configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")
                config.Save()
            End If
        End If
    End If

    'decrypt identity
    config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
    configSection = config.GetSection("system.web/identity")
    If Not (configSection Is Nothing) Then
        If Not (configSection.SectionInformation.IsLocked) Then
            configSection.SectionInformation.UnprotectSection()
            config.Save()
        End If
    End If

What i wanted to do was put this in a win forms application with a couple of buttons (btnEncrypt and btndecrypt) and a browse control (to browse to the various web.configs).

The application can then be used by our ops guys so they can encrypt or decrypt all the web.configs in all our web applications without having to re-publish etc...

Any help would be much appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尾戒 2024-10-25 23:24:04

Microsoft 有一种内置方法可以加密部分 web.config 。没有必要重新发明轮子。

如果您想构建一个管理界面来查看这些设置,那么这也很容易。由于它在内存中对其进行解密(在处理时),您可以获取相关部分并将它们转储到管理界面中。请注意,对 web.config 的任何实际更改都将导致 AppPool 回收。

Microsoft has a built in way to already encrypt parts of your web.config. There's no need to re-invent the wheel.

If you wanted to build an admin interface to see those settings, then that's easy too. Since it decrypts it in memory (when it's being processed), you can get the relevant sections and dump them into an admin interface. Be advised that any actual change to the web.config will cause an AppPool recycle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文