【解決方法】C#クラスライブラリでSQLクエリを実行する安全な方法は?


みなさん、こんにちは私はサイド プロジェクトとして nuget パッケージを開発しています。パッケージを使用すると、ユーザーは SQL クエリを含む関数を実行することで MySQL データベース サーバーからデータを取得できます。これを「GetStuff()」と呼びましょう。

GetStuff コードの例:

public class GetStuff : IDisposable {

      public string? name;


      public void Dispose() {
          //throw new NotImplementedException();
      }

      public GetStuff() {

              Console.WriteLine("Connecting to Server...");

              ConnectionGetter.con.Open();

              String _select = "SELECT stuff FROM information;";
              ConnectionGetter.command = new MySqlCommand(_select, ConnectionGetter.con);

              MySqlDataReader _read = ConnectionGetter.command.ExecuteReader();
              while (_read.Read()) {
                  name = _read.GetString(0);
              }
              _read.Close();
      }

使用例:

String myUsername = "";
     using(GetStuff gs = new GetStuff()) {
         myUsername = gs.name;
     }

ConnectionGetter クラスは、App.Config から接続文字列を取得します。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace officialAPIFS {
    public class ConnectionGetter {
        private static string access = System.Configuration.ConfigurationManager.ConnectionStrings["CONNECTIONSETUP"].ConnectionString;
        public static MySqlConnection con = new MySqlConnection(access);
        public static MySqlCommand command;
    }
}

そして主な問題は、インテリセンスを使用しているときに SQL クエリが公開され、テーブル、列、または関数からの SQL クエリに関連するものがユーザーに公開されないようにしたいことです。これは間違っていますか? ? ユーザーが関数の背後にあるSQLクエリコードを見ることができないようにするためのより安全な方法は何ですか?

私が試したこと:

私は完全に道に迷っており、グーグルは無関係な結果を示しています。

解決策 1

次の例に示すように、接続文字列を暗号化できます。
App.Config ファイルの暗号化/復号化接続文字列[^]

コメント

タイトルとURLをコピーしました