Chapter 1__1.2 Storing Connection Strings

來源:互聯網
上載者:User

       為了保證程式的可拓展性,安全性以及操作的簡易性,必須要把串連資料庫的連接字串放在一個好的地方。

1.21 NET Framework 2.0中引入的保護作為加密儲存串連的ASP.NET應用程式中的字串機制配置。它引進了connectionStrings連接字串的集合。在以前的版本中,將連接字串保持到appSettings 節點中。

1.23 幾項串連安全的技術如下:

     注意一下:(1)儘可能的使用整合串連integrated security whenever possible。

          (2)千萬不要使用空密碼和弱密碼。

          (3)一定不要用sa或任何關於管理員的帳號如administrative。

         (4)盡量加密。

1.2.3.1 Application configuration file

    應用程式設定檔是基於XML的文字檔,一個web應用程式可以有多個設定檔的所有名為Web.config的檔案。每個設定檔支援配置這它所在目錄以及子目錄的東西,它可以覆蓋和繼承其他的配置東西。電腦設定檔的Machine.config在。NET運行時安裝的配置位於子目錄,包含配置資訊適用於電腦。

    在ASP.NET中最好放在application configuration file中,既安全又方便。資料庫連接字串通常儲存<connectionStrings>節點中

    <configuration>
    <connectionStrings>
        <add key="ConnectionString"
value="Data Source=(local);Initial Catalog=AdventureWorks;
User ID=sa;password=;"
        />
    </connectionStrings>
</configuration>

ConnectionStrings 屬於System.Configuration 類用來檢索 <connectionStrings>裡面的資料

1.2.3.2特點:

    優點:方便。應用程式設定檔,方便的部署。

    缺點:應用程式設定檔本身並不具有安全可靠,因為它們儲存在文字檔中明確的資訊是通過檔案系統訪問

----所以要對它進行加密和設定存取權限。

1.2.3.3例子

       請確保您的名稱為Windows表單應用程式設定檔為app.config,這是預設值。在產生時。這個檔案會自動複製到由Visual Studio啟動目錄。名為applicationName.exe.config

      (1)Create a new C# console application named StoredConnectionStringConfig。

      (2)加一個應用配置的模板。

   (3) Add a SQL Server connection string within a connectionStrings element in the file App.config

 

      <?xml version="1.0" encoding="utf-8" ?>        <configuration>            <connectionStrings>              <add name="AdventureWorks"               providerName="System.Data.SqlClient"               connectionString="Data Source=(local);              Integrated security=SSPI;Initial Catalog=AdventureWorks;" />          </connectionStrings>      </configuration>
    (4)添加一個對System.Configuration 程式集的引用。
    (5)
Code View:
using System;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace StoreConnectionStringConfig{    class Program    {        static void Main(string[] args)        {            // Enumerate connection strings            Console.WriteLine("---Connection string enumeration---");            foreach (ConnectionStringSettings css in                ConfigurationManager.ConnectionStrings)            {                Console.WriteLine(css.Name);                Console.WriteLine(css.ProviderName);                Console.WriteLine(css.ConnectionString);            }            // Retrieve a connection string and open/close it            Console.WriteLine("\n---Using a connection string---");            Console.WriteLine("-> Retrieving connection string AdventureWorks");            string sqlConnectString =                ConfigurationManager.ConnectionStrings[                "AdventureWorks"].ConnectionString;            SqlConnection connection = new SqlConnection(sqlConnectString);            Console.WriteLine("-> Opening connection string.");            connection.Open(  );            Console.WriteLine("Connection string state = {0}", connection.State);            connection.Close(  );            Console.WriteLine("-> Closing connection string.");            Console.WriteLine("Connection string state = {0}", connection.State);            Console.WriteLine("\nPress any key to continue.");            Console.ReadKey(  );        }    }}
 

一下的瞭解下:

  Hardcode in the application

  Universal data link (UDL) file(在SQL Server。NET資料提供者不支援在其連接字串UDL檔案。 UDL檔案未加  密)

  Windows registry。把串連資料庫的設定檔放到註冊表中。如 HKEY_LOCAL_MACHINE\SOFTWARE

     結束。      

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.