保护网络共享访问数据库

时间:2014-05-09 16:22:42

标签: vb.net ms-access

我正在申请一个小型办公室(最多4个用户),所以我决定设置一个网络共享文件夹并将我的Access数据库放在那里,并为所有用户提供对该网络共享的访问权。

问题是,当我共享数据库文件夹时,它会向所有人公开,因此任何有权访问网络的人都可以打开共享文件夹并删除共享数据库。

问题:如何保护它免受删除?这样即使有人连接到网络并访问数据库文件.accdb,他也无法删除它!

1 个答案:

答案 0 :(得分:3)

非常简单,您可以将共享文件夹的权限设置为all,并且可以设置数据库文件的权限。

这里我将向您展示如何在一个文件夹上执行此操作,并且对于一个文件也是如此,但如果您对该文件夹设置了权限,则不再需要在数据库文件上设置权限,因为它在文件夹中。

我假设您已与"每个人共享该文件夹"

1 - 左键单击文件夹 - >特性

2 - 点击安全标签 - >选择"每个人" - >单击“高级”

3 - 点击更改权限(适用于"所有人"群组)

4 - 点击修改(适用于"所有人"群组)

5 - 检查"允许"中的 所有 框除完全控制,删除子文件和文件并删除

enter image description here

6 - CLick好了,你完成了,每个人都可以从数据库中读/写,但是没有人有权删除它..

要在vb.net中执行所有这些操作,您需要两个命令,在共享文件夹后,您可以使用.RemoveAccessRule删除删除权限

此代码自动执行所有操作,与所有人(或任何您想要的人)共享,并删除文件夹的删除权限

Imports System.Management
Imports System.Security.AccessControl
Imports System.IO
Imports System

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            Dim PermissionsList As New List(Of SharePermissionEntry)

            'Create a new permission entry for the Everyone group and specify that we want to allow them Read access
            Dim PermEveryone As New SharePermissionEntry(String.Empty, "Everyone", SharedFolder.SharePermissions.Read, True)
            Dim PermUser As New SharePermissionEntry("", "Everyone", SharedFolder.SharePermissions.FullControl, True)

            'Add the two entries declared above to our list
            PermissionsList.Add(PermUser)
            PermissionsList.Add(PermEveryone)

            'Share the folder as "Test Share" and pass in the desired permissions list
            Dim Result As SharedFolder.NET_API_STATUS = _
            SharedFolder.ShareExistingFolder("Test Share", "This is a test share", "C:\Folder", PermissionsList)

            Dim FolderPath As String = "C:\Folder" 'Specify the folder here
            Dim UserAccount As String = "Everyone" 'Specify the user here

            Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(FolderPath)
            Dim FolderAcl As New DirectorySecurity
            FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
            FolderAcl.RemoveAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.DeleteSubdirectoriesAndFiles, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
            FolderAcl.RemoveAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.Delete, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
            FolderInfo.SetAccessControl(FolderAcl)

            'Show the result
            If Result = SharedFolder.NET_API_STATUS.NERR_Success Then
                MessageBox.Show("Share created successfully!")
            Else
                MessageBox.Show("Share was not created as the following error was returned: " & Result.ToString)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

不要忘记添加引用System.ManagementSystem.Management.Instrumentation并同时添加这些模块SharePermissionEntry.vbSharedFolder.vb您的项目

这就是它的全部,希望这有助于!!有关详情,请参阅LinkLink