如何配置数据库连接牢固 [英] How to configure database connection securely

查看:123
本文介绍了如何配置数据库连接牢固的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相似但不相同的:

  • How to securely store database connection details
  • Securely connecting to database within a application

大家好,我有连接到数据库服务器的C#WinForms应用程序。数据库连接字符串,其中包括一个通用的用户名/密码,被放置在一个NHibernate的配置文件,该文件位于同一目录下的EXE文件。

Hi all, I have a C# WinForms application connecting to a database server. The database connection string, including a generic user/pass, is placed in a NHibernate configuration file, which lies in the same directory as the exe file.

现在我有这个问题:在运行应用程序不应该去了解一般数据库用户的用户名/密码,因为我不想让他翻找直接在数据库周围的用户

Now I have this issue: The user that runs the application should not get to know the username/password of the general database user because I don't want him to rummage around in the database directly.

我也可以硬编码的连接字符串,如果数据库是移动这是不好的,因为管理员必须能够改变它,或者,如果他想开发/测试/生产环境之间进行切换。

Alternatively I could hardcode the connection string, which is bad because the administrator must be able to change it if the database is moved or if he wants to switch between dev/test/prod environments.

这么久,我已经找到了三种可能性:

So long I've found three possibilities:


  1. 第一个引用的问题是一般的使运行应用程序

该文件只读取了用户但是这还不是不够的,在我的情况下(运行应用程序的用户是一个人该数据库的用户名/密码是通用的,甚至不应该被人访问。)

But that's not not enough in my case (the user running the application is a person. The database user/pass are general and shouldn't even be accessible by the person.)

第一个答案还提出了加密连接数据写入到文件

通过这种方法之前,管理员不能够再配置的连接字符串,因为他不能用手加密。

With this approach, the administrator is not able anymore to configure the connection string because he cannot encrypt it by hand.

第二个引用问题提供这种非常情况下的做法,但似乎很复杂。

The second referenced question provides an approach for this very scenario but it seems very complicated.

我的问题给你:


  1. 这是一个非常普遍的问题,所以不存在任何一般性的如何做 - 执行 - 它的方式,在某种程度上是设计模式?

  1. This is a very general issue, so isn't there any general "how-to-do-it" way, somehow a "design pattern"?

有没有在.NET中的配置基础设施,一些支持?

Is there some support in .NET's config infrastructure?

(可选,也许超出范围)我可以结合起来,很容易与NHibernate的配置机制,

(optional, maybe out of scope) Can I combine that easily with the NHibernate configuration mechanism?

更新:

在回应第一答案:有几个原因,我想直接连接到数据库和不使用Web服务

In response to the first answers: There are several reasons why I would want to connect to the database directly and not use a web service:


  • (N)Hibernate可以只用一个数据库使用,而Web服务(是吗?)

  • 我们计划提供离线功能,即如果数据库或网络应了下来,用户可以继续他的工作。要管理这一点,我想有一个地方,在进程内的数据库,例如SQL Server精简,并使用MS同步框架,因为它再次到它与服务器数据库,尽快同步。

你有任何进一步的想法考虑到这一点?

Do you have any further ideas taking this into account?

推荐答案

其实WebService的方法(在其他答复中提到)是指移动NHibernate和其逻辑到web服务。然后,WebService的,公开提供给使用WebService的方法的应用数据库的功能。

Actually the WebService approach (mentioned in some other answer) means that you move NHibernate and its logic to the web-service. The WebService then, exposes the db functionality available to the application using the WebService's methods.

有实际上的数据库只有一个用户,一个WebService使用,如果你希望应用程序的用户有不同的数据库的权限,你从WebService层摘要

There is practically only one user for the database, the one the WebService uses and if you want the application user to have different db privileges you abstract it from the WebService layer

在最后,WinForms应用程序只知道它要求的WebService的位置通过WebService的的方法,你可以申请这两个端点之间任何需要的安全措施的数据。

In the end, the WinForms application is only aware of the location of the WebService where it requests data through the WebService's methods and you can apply any required security measure between these two endpoints.

有关离线功能它全部归结为使坚持一个安全的方式你数据本地存储,并通过WebService的提供同步方法

For off-line capability it all boils down to making a secure way to persist your data to local storage and providing a synchronization method via the WebService

其实我已经使用了与DB连通的互联网服务和一个WinForm应用程序做到了这一点(.NET Compact Framework中)只有聊到了web服务,并在没有蜂窝网络覆盖的情况下,将序列的变化到存储卡(数据并不重要,这样对于那些没有采取我的情况晦涩/猥亵的保安措施)

I have actually done this using a webservice that communicated with the DB and a WinForm application (.NET Compact Framework) that only talked to the webservice and in case of no cellular network coverage it would serialize the changes to the memory card (the data was not important so for my case obscure/obscene security measures where not taken)

用一个小例子UPDATE的要求(我觉得很奇怪,虽然要求对这样的一个例子)

UPDATE with a small example as requested (i do find it strange though to ask for an example on this)

您已经设置了您的域名类和NHibernate配置和(例如)您在ASP.NET型应用程序的WebService的一个项目库的东西。为了简单起见,我只打算有一个单一的网络服务类(在Foo.asmx.cs)和以及一个酒吧域类

you have set up your domain classes and nhibernate configuration and (for example) your repository stuff in a project of type ASP.NET WebService Application. For the sake of simplicity i'm only going to have a single web-service class Foo (in Foo.asmx.cs) and well as a single Bar domain class

所以你得到这个(实际执行情况而有所不同):

so you get this (actual implementation varies):

namespace FWS
{
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class FooService : WebService
    {
        private readonly ILog errorLogger = LogManager.GetLogger("ErrorRollingLogFileAppender");
        private readonly IDaoFactory daoFactory = new DaoFactory();
        private readonly ISession nhSession =  HibernateSessionManager.Instance.GetSession();
    }

    [WebMethod]
    public Bar[] GetFavoriteBars(string someParam, int? onceMore){
        return daoFactory.GetBarDao().GetFavoriteBars(someParam, onceMore); //returns a Bar[]
    }
}

和我们抽象daobehaviour,或者只是使用直接nhsession,暴露为一个WebMethod。

and we abstract the daobehaviour, or just use the nhsession directly, exposed as a webmethod.

现在从WinForm应用程序,所有你需要做的就是添加Web引用这使得对配置进行必要的更改,而且生成所有必需的类(在这个例子中,作为Web服务暴露了它,它会创建一个酒吧类。)

Now from the WinForm application all you need to do is Add a WebReference which makes all necessary changes to configuration but also generates all necessary classes (in this example, it will create a Bar class as the web-service exposes it).

namespace WinFormK
{
    public class KForm(): System.Windows.Forms.Form
    {
        public void Do()
        {
            var service = new FWS.FooService();
            string filePath = "C:\\temp\FooData.xml";
            Bar[] fetched = service.GetFavoriteBars("yes!", null);

            //lets write this to local storage
            var frosties = new XmlSerializer(typeof(Bar));
            TextReader reader = new StreamReader(filePath);

            try
            {
                var persisted = (T)frosties.Deserialize(reader);
            }
            catch(InvalidOperationException)
            {
                //spock, do something
            }
            finally
            {
                reader.Close();
                reader.Dispose();
            }
        }
    }
}

有你要注意某些事情:


  • 您失去本质的东西懒惰,或者至少你失去它在你的WinForm应用程序。 XML序列化不能序列代理,因此您无论是在这些集合/性能延迟抓取的转弯或使用[XmlIgnore]属性反过来做它暗示了系列化。

  • 您不能将WebMethod签名返回接口。他们必须是具体的类。所以,回到的IList<酒吧GT; 将不得不转化为列表<酒吧GT; 或类似的东西

  • web服务是通过IIS执行,是从Web浏览器中可见。默认情况下,只有本地的浏览器请求将送达(但可以改变),这样你可以单独你的winform做什么测试你的数据访问层。

  • 接收端(winform应用程序)没有NHibernate的知识凡

  • 在上面的例子中我已经把相同的名称为道的方法为网络的方法。只要你没有保持NHibernate的 - 具体的方法在你的DAO的(可以说像 NHibernate.Criterions.Order 参数),你可能会发现没有问题。事实上,你可以有许多的的.asmx 类,只要你想你的web服务,甚至可能是他们的地图到相应的DAO的(比如公共类FooService接口:WebService的公共类BarService:WebService的公共类CheService:WebService的,其中每个对应一个DAO)。

  • 您可能不得不写某种轮询方法的端点,让您的提供的数据新鲜的。

  • 的WebService数据很冗长;极为左右。这是可取之前(也许它们加密以及)发送它们通过线路

  • 的Windows应用程序只知道一个配置项来压缩他们什么: HTTP: //server/FWS/FooService.asmx

  • Web服务已届默认为禁用。请记住,使用会话的用户数据开始之前。

  • 您可能不得不写一些种类的认证为WebService

  • 在上面,我是返回一个酒吧[] 酒吧的例子被映射与NHibernate。更多的往往不是,这可能并非如此,你可能需要写一个辅助类 WSBar 它相适应的原酒吧类,什么互联网服务和WinForm应用程序可以消耗。这个类实际上只是一个数据载体。同样,这也取决于有多少集成与您的域类和NHibernate的存在为muxh如何复杂,你的类是:某些数据结构可以默认情况下不被序列化

  • You essentially lose lazy stuff, or at least you lose it in your winform application. The XML serializer cannot serialize proxies and as such you either turn of lazy fetching on those collections/properties or you use the [XmlIgnore] attribute which in turn do what it implies on serialization.
  • You cannot return interfaces on the WebMethod signatures. They have to be concrete classes. So, returning IList<Bar> will have to be transformed to List<Bar> or something of the like
  • The webservice is executed by IIS and is visible from a web browser. By default, only local browser requests will be served (but that can be changed) so you can test your data access layer separately of what your winform does.
  • The receiving end (winform app) has no knowledge of NHibernate whatsoever.
  • In the example above i've kept the same name for the dao-methods for the web-methods; As long as you didn't keep nhibernate--specific methods in your dao's (lets say like a NHibernate.Criterions.Order parameter) you will probably find no problem. In fact you can have as many .asmx classes in your webservice as you want, probably even 'map' them to the corresponding dao's (like public class FooService : WebService, public class BarService : WebService, public class CheService : WebService where each corresponds to a DAO).
  • You will probably have to write some kind of polling method between your endpoints to keep your presented data fresh.
  • WebService data is verbose; extremely so. It is advisable to zip them or something before sending them over the wire (and maybe encrypt them as well)
  • the win application only knows a configuration entry: http://server/FWS/FooService.asmx
  • Webservices have Session disabled by default. remember that before starting using the session for user data.
  • You will probably have to write some kind of authentication for the webservice
  • In the example above i am returning a Bar[] with Bar being mapped with nhibernate. More often than not this may not be the case and you may be required to write an auxiliary class WSBar where it adapts the original Bar class to what the webservice and the winform application can consume. This class is actually just a data carrier. Again this depends on how much integration exists with your domain classes and nhibernate as well as how muxh complicated your classes are: Certain data structures cannot be serialized by default.

这种模式可能并不适合你已经和你的应用程序

This model may not suit what you have already done with your application

这篇关于如何配置数据库连接牢固的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆