加密web.config的方法谁知道呀,,看到答案就结帖~~

aaron_lly 2006-03-10 09:16:57
加密web.config的方法谁知道呀,,看到答案就结帖~~
...全文
204 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
buaawjh 2006-03-10
  • 打赏
  • 举报
回复
1、打开记事本,然后将下面的代码复制到一个新文件中。
<%@ Page Language="C#" %><%Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);%> 保存 my.aspx 到你的web目录 ,运行一下窗体显示 “ NT AUTHORITY\NETWORK SERVICE ”。成功!2、(关键一步)运行cmd,执行以下 aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE" 说明:注册默认的 RsaProtectedConfigurationProvider 的RSA 密钥容器, NetFrameworkConfigurationKey 是 RsaProtectedConfigurationProvider 的默认provider。 成功!3、现在,可以加密web.config ,运行: 加密:aspnet_regiis -pe "connectionStrings" -app "/Myweb" 说明:"connectionStrings" 是要加密的节,"/Myweb"是的web目录 解密:aspnet_regiis -pd "connectionStrings" -app "/Myweb" 成功! 4、这样就可以在程序里调用了(不用解密, 哈哈): ... string connstr= ConfigurationManager.ConnectionStrings["myConnstr"].ConnectionString.ToString(); ... 同样的,也可以用创建自己的RSA 密钥容器,如下:
(1)、创建 "MyKeys" 密钥容器,运行:aspnet_regiis -pc "MyKeys" -exp (2)、在web.config里加入以下: <protectedData> <providers> <add name="MyProvider" type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0. 0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d0a3a, processorArchitecture=MSIL" keyContainerName="MyKeys" useMachineContainer="true" /> </providers> </protectedData> 保存。 (3)、授予帐户对计算机级别的 "MyKeys" RSA 密钥容器的访问权限,运行: aspnet_regiis -pa "MyKeys" "NT AUTHORITY\NETWORK SERVICE" (4)、现在,可以加密web.config ,运行: 加密:aspnet_regiis -pe "connectionStrings" -app "/Myweb" -prov "MyProvider" 说明:"connectionStrings" 是要加密的节,"/Myweb"是的web目录,"MyProvider" 自己密钥容器 解密:aspnet_regiis -pd "connectionStrings" -app "/Myweb" -prov "MyProvider"
aaron_lly 2006-03-10
  • 打赏
  • 举报
回复
:(
jiezhi 2006-03-10
  • 打赏
  • 举报
回复
参考:
http://jiezhi.cnblogs.com/archive/2005/09/01/227962.html
aaron_lly 2006-03-10
  • 打赏
  • 举报
回复
OK ~~~~ ...............
flashicp 2006-03-10
  • 打赏
  • 举报
回复
这个是我练习的列子或者对你有用
flashicp 2006-03-10
  • 打赏
  • 举报
回复
代码如下
using System;
using System.Web.Configuration;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class EncDecWebConfig : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// load web.config into TextBox on first page visit...
if (!Page.IsPostBack)
{
RefreshWebConfig();
}
}

private void RefreshWebConfig()
{
// Read in the value of Web.config into the TextBox...
using (StreamReader reader = File.OpenText(Server.MapPath("~/Web.config")))
{
webConfig.Text = reader.ReadToEnd();
}

// Programmatically read in the value of the connection string
PlainTextConnectionString.Text = ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ConnectionString;
}

protected void btnRefreshWebConfig_Click(object sender, EventArgs e)
{
RefreshWebConfig();
}

protected void btnEncrypt_Click(object sender, EventArgs e)
{
ProtectSection("connectionStrings", "DataProtectionConfigurationProvider");
RefreshWebConfig();
}

protected void btnDescrypt_Click(object sender, EventArgs e)
{
UnProtectSection("connectionStrings");
RefreshWebConfig();
}

protected void btnEncAuthentication_Click(object sender, EventArgs e)
{
ProtectSection("system.web/authentication", "DataProtectionConfigurationProvider");
RefreshWebConfig();
}

protected void btnDecAuthentication_Click(object sender, EventArgs e)
{
UnProtectSection("system.web/authentication");
RefreshWebConfig();
}

#region "Protect/Unprotect Methods"
// Code taken verbatim from David Hayden's blog [http://davidhayden.com/blog/dave/]
// Entry: Encrypt Connection Strings AppSettings and Web.Config in ASP.NET 2.0 - Security Best Practices
// [http://davidhayden.com/blog/dave/archive/2005/11/17/2572.aspx]

private void ProtectSection(string sectionName,
string provider)
{
Configuration config =
WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath);

ConfigurationSection section =
config.GetSection(sectionName);

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

private void UnProtectSection(string sectionName)
{
Configuration config =
WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath);

ConfigurationSection section =
config.GetSection(sectionName);

if (section != null &&
section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save();
}
}
#endregion
}
<%@ Page ValidateRequest="False" Language="C#" AutoEventWireup="true" CodeFile="EncDecWebConfig.aspx.cs" Inherits="EncDecWebConfig" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Encrypting Configuration Information Demo</h1>
<p>Note how the plain-text value of the MembershipConnectionString is available
programmatically using the exact same code, regardless of whether the <code><connectionStrings></code>
section is encrypted or not...</p>
<b>Value of the MembershipConnectionString:</b>
<asp:Label ID="PlainTextConnectionString" runat="server"></asp:Label><br /><br />
<b>Contents of Web.config:</b>
<asp:TextBox ID="webConfig" TextMode="MultiLine" Width="97%" Rows="20" runat="server"></asp:TextBox>
<p>
<asp:Button ID="btnRefreshWebConfig" runat="server" Text="Refresh" OnClick="btnRefreshWebConfig_Click" />
   
<asp:Button ID="btnEncrypt" runat="server" Text="Encrypt Connection Strings" OnClick="btnEncrypt_Click" />
   
<asp:Button ID="btnDescrypt" runat="server" Text="Decrypt Connection Strings" OnClick="btnDescrypt_Click" />
 </p>
<p>
<asp:Button ID="btnEncAuthentication" runat="server" OnClick="btnEncAuthentication_Click"
Text="Encrypt Authentication Section" />
 
<asp:Button ID="btnDecAuthentication" runat="server" OnClick="btnDecAuthentication_Click"
Text="Decrypt Authentication Section" /></p>
</div>
</form>
</body>
</html>
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings/>

<connectionStrings>
<add name="MembershipConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

<system.web>

<compilation debug="true"/>

<authentication mode="Forms" />

</system.web>
</configuration>

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧