View Full Version : حرفه ای: Protect کردن ConnectionString در web.config
SabaSabouhi
یک شنبه 05 مهر 1388, 17:02 عصر
با سلام
با کد زیر ConnectionString رو توی web.config رمز میکنم، روی localhost به خوبی جواب میده ولی روی host مشتری جواب نمیده. دلیل چیه؟
کد:
Configuration cfg = WebConfigurationManager.OpenWebConfiguration( Request.ApplicationPath );
if ( cfg == null )
return;
ConfigurationSection section = cfg.GetSection( "connectionString" );
if ( section == null )
return;
if ( !section.SectionInformation.IsProtected ) {
section.SectionInformation.ProtectSection( "RSAProtectedConfigurationProvider" );
section.SectionInformation.ForceSave = true;
cfg.Save();
}
هنگام اجرا خطای زیر ایجاد میشه: ( ConfigurationErrorsException هنگام اجرای متد cfg.Save )
An error occurred executing the configuration section handler for connectionStrings
هر جور دسترسی هم که میشد هم به folder و هم به web.config دادم ولی نشد که نشد.
صبا صبوحی
m.hamidreza
یک شنبه 05 مهر 1388, 22:41 عصر
اگر دیتابیستون رو همون هاست هست نیازی به رمزنگاری کانکشن استرینگ نیست دیتابیس در اختیار اوناست و رمزنگاری کانکشن استرینگ عملا تشریفاتی هست.
موفق باشید.
atbin2010
دوشنبه 06 مهر 1388, 11:12 صبح
سلام
چطور اینکار تشریفاتیه! مگه user و pass دیتابیس توی connection string نیست؟
فقط میمونه یه login ساده و.....اطلاعات و بقیه ماجرا
Mahdi-563
دوشنبه 06 مهر 1388, 11:18 صبح
سلام دوست عزیز
من با m.hamidreza موافقم وقتی شما رو هاست خودت می تونی رمز گشایی کنی مطمعا باشی همسایه شما که از آن شرکت هاست گرفته هم میتونه این کار را بکنه
بهتر ConnectionString توی کد ها جا بدی
SabaSabouhi
دوشنبه 06 مهر 1388, 11:39 صبح
با سلام
دوستان گرامی، این که اصولاً این کار درست هست یا نه مقولهای دیگر است. خواهش میکنم راهنمایی کنید چطور این کار رو انجام بدم.
صبا صبوحی
پانوشت: از قول یک دزد حرفهای در تلویزیون " دزدگیر خودرو فقط تازهکارها را میترساند، اما حرفهای ها هم ترجیح میدهند سراغ ماشین بدون دزدگیر بروند "
Mahdi-563
دوشنبه 06 مهر 1388, 19:40 عصر
استاد نصیری در این باره دوتا تابع نوشتن خیلی تو کتابام گشتم ولی پیدا نکردم
آیا دسترسی را چک کردی که واقعا اعمال می شه یا نه؟
می تونی تویه XML ذخیره کنی و آدرسش رو تویه فایل web.config بدی شاید این جوری مشکل حل بشه!
صابر
دوشنبه 06 مهر 1388, 22:05 عصر
http://barnamenevis.org/forum/showthread.php?t=180027
SabaSabouhi
سه شنبه 07 مهر 1388, 17:14 عصر
http://barnamenevis.org/forum/showthread.php?t=180027
با سلام
من هم همین کار رو میکنم، روی ویندوز Vista خودم و Windows 7 همکارم درست جواب میده اما روی host کار نمیکنه. هر چی دسترسی هم بود دادم ولی نشد.
صبا صبوحی
SabaSabouhi
شنبه 11 مهر 1388, 09:47 صبح
با سلام
هیچکی کمک نمیکنه؟
صبا صبوحی
BahmanDB
یک شنبه 12 مهر 1388, 18:01 عصر
سلام دوست عزیز شما می تونید با این کد که من توی کلیک Button گذاشتم.
می تونید هم کد کنید هم دی کد کنید و . . .
using System;
using System.Data;
using System.Configuration;
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;
using System.Security;
using System.Web.Configuration;
using System.Text;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void EncryptConnString()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Reque st.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.Save();
}
}
public void DeEncryptconnectionstring()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Reque st.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save();
}
}
private void ReadFile()
{
string strFileName = Server.MapPath("Web.Config");
StreamReader strmRdr;
string strTemp;
strmRdr = File.OpenText(strFileName);
strTemp = strmRdr.ReadLine();
StringBuilder sb = new StringBuilder(String.Empty);
while (strTemp != null)
{
sb.Append(strTemp + "\n");
strTemp = strmRdr.ReadLine();
}
strmRdr.Close();
//Literal1.Text = HttpUtility.UrlEncode(sb.ToString());
//Response.Write(HttpUtility.UrlEncode(sb.ToString() ));
//Response.Write(ConvertHTMLCharacters(sb.ToString() ));
TextBox1.Text = ConvertHTMLCharacters(sb.ToString());
//Label1.Text = ConvertHTMLCharacters(sb.ToString());
}
private string ConvertHTMLCharacters(string strSourceString)
{
string strTargetString = null;
if (strSourceString != " ")
{
strTargetString = strSourceString.Replace("&", "&");
strTargetString = strSourceString.Replace("<", "<");
strTargetString = strSourceString.Replace(">", ">");
}
return strTargetString;
}
protected void Button1_Click(object sender, EventArgs e)
{
this.EncryptConnString();
this.ReadFile();
}
protected void Button2_Click(object sender, EventArgs e)
{
this.DeEncryptconnectionstring();
this.ReadFile();
}
}
و این هم web.config
<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<appSettings/>
<connectionStrings>
<add name="EstateConnectionString" connectionString="Data Source=.;Initial Catalog=Estate;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="NorthwindConnectionString" connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true"/>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
</configuration>
SabaSabouhi
دوشنبه 13 مهر 1388, 11:08 صبح
با سلام
خوب من هم که همین کار رو کردم. اما خطا میده و کار نمیکنه.
صبا صبوحی
صابر
دوشنبه 13 مهر 1388, 11:33 صبح
با سلام
خوب من هم که همین کار رو کردم. اما خطا میده و کار نمیکنه.
صبا صبوحی
به شما هم خطای دسترسی میده؟ یا خطای دیگهای میگیرید؟
SabaSabouhi
سه شنبه 14 مهر 1388, 13:10 عصر
به شما هم خطای دسترسی میده؟ یا خطای دیگهای میگیرید؟
با سلام
خیر خطای دسترسی نمیده، در اولین پست خطا رو کپی کردم.
صبا صبوحی
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.