简体   繁体   English

通过C#应用程序在服务器中访问文件

[英]Reaching a file in a server through C# application

I wrote an application in c# & SQLite for storing data of all employees in a company which has around 500 employees. 我在c#和SQLite中编写了一个应用程序,用于存储拥有约500名员工的公司中所有员工的数据。 I want to put the database & the application in a file server/shared folder (MS server). 我想将数据库和应用程序放在文件服务器/共享文件夹(MS服务器)中。 Then all employees will have a shortcut of the application in their desktops. 然后,所有员工都将在其桌面上拥有应用程序的快捷方式。 I want to make some input fields (text box) enabled/disabled based on the permission of the user runs the application. 我想根据用户运行应用程序的权限启用/禁用一些输入字段(文本框)。 Whats the best practice for doing that? 这样做的最佳做法是什么?

I want the user can read/write in the database through my application only (the application is located in the same database folder). 我希望用户只能通过我的应用程序在数据库中读/写(应用程序位于同一个数据库文件夹中)。 I don't want the user to reach the database without my application. 我不希望用户在没有我的应用程序的情况下访问数据库。 How to do that? 怎么做?

If you don't want the users to reach your database you should create a client server architecture. 如果您不希望用户访问您的数据库,则应创建客户端服务器体系结构。

You can run your service on the same machine as the file server (running as a Windows Service) and use WCF for communication between your server and your client. 您可以在与文件服务器相同的计算机上运行服务(作为Windows服务运行),并使用WCF在服务器和客户端之间进行通信。 You access your database from your server and let your server authenticate your users and validate that they have access to the application. 您可以从服务器访问数据库,让服务器对用户进行身份验证,并验证他们是否有权访问该应用程序。

You can cheat and try to "hide" database credentials inside your client application, but that is security by obscurity and any one with some programming skills or similar can find out the credentials to the database and connect directly to the database. 您可以作弊并尝试在客户端应用程序中“隐藏”数据库凭据,但这是默默无闻的安全性,任何具有某些编程技能或类似技能的人都可以找到数据库的凭据并直接连接到数据库。

I don't want the user to reach the database without my application 我不希望用户在没有我的应用程序的情况下访问数据库

If your application will directly access the SQLite database via a Windows file share, this is impossible. 如果您的应用程序将通过Windows文件共享直接访问SQLite数据库,则这是不可能的。 Sure, you can make it inconvenient, but it's not really possible. 当然,你可以让它变得不方便,但它确实不可能。

The only way to achieve this really is by introducing some middleware. 实现这一目标的唯一方法是引入一些中间件。

This would typically be a service (WCF perhaps) that listens for connections from your client application, authenticates them, and manages all access to the underlying database. 这通常是一个服务(可能是WCF),它侦听来自客户端应用程序的连接,对它们进行身份验证,并管理对底层数据库的所有访问。 The database would be stored in a location that is visible to the server only, and not visible through a Windows share to your users. 数据库将存储在仅对服务器可见的位置,并且不会通过Windows共享显示给用户。

Also, SQLite isn't exactly a great choice for a multi-user system. 此外,SQLite不是多用户系统的绝佳选择。 You can kill two birds with one stone here - switch to a DBMS (MS SQL Server Express is free, also MySQL, PostgreSQL are common free choices) that accepts client connections over a network, and build your application to connect directly to the database server (using integrated Windows authentication may also be possible like this, so you can avoid an explicit logon). 你可以在这里一举两得 - 切换到DBMS(MS SQL Server Express是免费的,也是MySQL,PostgreSQL是常见的自由选择),通过网络接受客户端连接,并构建你的应用程序直接连接到数据库服务器(使用集成的Windows身份验证也可能是这样的,因此您可以避免显式登录)。 In a simple scenario this may be adequate and avoid you needing to build an explicit service layer. 在一个简单的场景中,这可能是足够的,并且可以避免您需要构建显式服务层。

In a more complex scenario, it can still make sense to have a middleware layer between the application and the database - this way, you can change the database design without changing the application design and deploying to all of your client machines - instead, just change the middleware layer in one place and your application won't know the difference. 在更复杂的场景中,在应用程序和数据库之间建立中间件层仍然是有意义的 - 这样,您可以在不更改应用程序设计和部署到所有客户端计算机的情况下更改数据库设计 - 而只需更改中间件层在一个地方,你的应用程序将不知道差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM